Re: Reading from a pty after slave exited
Re: Reading from a pty after slave exited
- Subject: Re: Reading from a pty after slave exited
- From: Julian Scheid <email@hidden>
- Date: Sat, 24 Apr 2010 18:14:32 +1200
On Mon, Apr 19, 2010 at 2:55 PM, Julian Scheid <email@hidden> wrote:
> Is it not possible to implement something akin to Screen to work
> reliably on both Linux and OS X using POSIX interfaces, without
> resorting to hacks such as those found in [3]?
It seems I have found a very simple solution: use openpty+fork instead
of forkpty and keep the slave fd open on the master side until after
EOF is hit. Why this would work makes sense after reading your
explanation.
Below is a modified version of the program from my original post. This
works fine on Darwin, I haven't tested it on Linux yet but I don't see
why it wouldn't work.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#ifdef __APPLE__
#include <util.h>
#else
#include <pty.h>
#endif
#include <string.h>
int main() {
const char* test_string = "foo";
int pid, master_fd, slave_fd;
openpty(&master_fd, &slave_fd, NULL, NULL, NULL);
pid = fork();
assert(pid >= 0);
if (pid == 0) {
/* slave */
dup2(slave_fd, fileno(stdout));
int nwritten = write(fileno(stdout), test_string, sizeof test_string);
assert(nwritten == sizeof test_string);
exit(0);
}
else {
/* master */
waitpid(pid, NULL, 0);
char buf[32];
int nread = read(master_fd, buf, sizeof buf);
assert(nread > 0);
assert(0 == strcmp(buf, test_string));
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden