Need a 2-way alternative to popen()

Michael Frank bugboy at portia.Stanford.EDU
Thu Mar 1 11:47:31 AEST 1990


Thanks to everyone who sent me help on ways to set up a 2-way
alternative to popen().  Some of you didn't hear this directly from
me, because the email bounced.  Anyway, I've put together here the 
core elements of the way to do it with pipe() instead of popen():

	int p1[2], p2[2];		/* Two pairs of fd's */
	pipe(p1); pipe(p2);		/* Create the two pipes */
	if (fork() == 0) {		/* - in child process - */
	  char * args[2];		/* array of args for command */
	  args[0] = "/bin/csh";		/* set up the args for the call */
	  args[1] = NULL;		/* (array is NULL-terminated) */
	  dup2(p1[0],0);		/* copy p1's read end to stdin */
	  dup2(p2[1],1);		/* copy p2's write end to stdout */
	  execv(args[0], args);		/* call the csh command */
	}
	tochild = p1[1]; fromchild = p2[0];	/* Parent's links to it */

Of course, to be neat about it you should also close the unused fd's
and check the system calls for errors.  But these are the central
parts.

Thaks again!
Mike



More information about the Comp.unix.questions mailing list