forkpty and fdopen

Andrew Barton andrevv at users.sourceforge.net
Wed Jul 23 17:34:22 PDT 2003


I'm trying to make a program that uses forkpty(), then runs sh in the
child process, and then writes to the child using streams. But it
doesn't work! It hangs on the wait() call; apparently the child never
sees the "exit\n".

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

main()
{
	int fd;
	pid_t pid;

	pid = forkpty (&fd, 0, 0, 0);
	if (pid == 0) {
		execlp ("sh", "sh", (void *)0);
		_exit (1);
	} else if (pid == -1) {
		exit (1);
	} else {
		FILE *F;

		F = fdopen (fd, "w");
		fprintf (F, "exit\n");
		fflush (F);
		wait (0);
	}
	exit (0);
}




More information about the freebsd-questions mailing list