redirect stdin when using execl

John Woods john at frog.UUCP
Sat Jul 22 11:39:00 AEST 1989


In article <851 at pcsbst.UUCP>, torsten at pcsbst.UUCP (torsten) writes:
> In article <414 at sc50.UUCP> ron at sc50.UUCP ( Ron Winnacott ) writes:
> >Hello net.
> >execl("/bin/mail","mail","ron","<","/tmp/tfile",NULL);
> 	I think this should do it:
> 	execl("/bin/sh","sh","/bin/mail ron < /tmp/tfile",NULL);

Or, you might try something general-purpose like:

fexecve(sin, sout, serr, av, ev)
int sin, sout, serr;
char **av, *ev;
{
	dup2(sin,  0);			/* note: dup2(0, 0) is a no-op */
	dup2(sout, 1);
	dup2(serr, 2);
	if (sin > 2)  close(sin);	/* delete extra file handles */
	if (sout > 2) close(sout);
	if (serr > 2) close(serr);
	return execve(av, ev);
}

Two provisos: System V users will want to use fcntl(, F_DUPFD, ) instead of
dup2() (and note that it is a little tricky to do that correctly), and this
doesn't quite work if you do silly things like

	fexecve(1, 2, 0, argvec, envp);

though it can be made to work if need be.
(Left to the student as an exercise :-)


-- 
John Woods, Charles River Data Systems, Framingham MA, (508) 626-1101
...!decvax!frog!john, john at frog.UUCP, ...!mit-eddie!jfw, jfw at eddie.mit.edu
    People...How you gonna FIGURE 'em?
    Don't bother, S.L.--Just stand back and enjoy the EVOLUTIONARY PROCESS...



More information about the Comp.lang.c mailing list