redirect stdin when using execl

Dave Jones djones at megatest.UUCP
Sat Jun 17 08:21:46 AEST 1989


>From article <2489 at mit-caf.MIT.EDU>, by vlcek at mit-caf.MIT.EDU (Jim Vlcek):
> In article <5587 at goofy.megatest.UUCP> djones at megatest.UUCP (Dave Jones) writes:
>>> In article <414 at sc50.UUCP> ron at sc50.UUCP ( Ron Winnacott ) writes:
>>>>Can anyone tell me how to redirect stdin when I use execl to 
>>>>start a new program.
>>
>>Under BSD Unix, you use dup2. 
> 
> plus a bit of code showing how to fork(), dup2(), and then execl() to
> get the desired effect.
> 
> I think a much better way, under BSD, is to use freopen() to attach
> stdin to the redirected file.


Maybe. But that wasn't the question. The question was how to redirect stdin when
using execl, not how to rebind stdin after the execl.

Anyway, using freopen is only possible if you have access to the source code
of all the programs which will be execled, are already responsible for their
content, and can easily modify and dependably distribute the modified version
to all sites which will use the execling program.  It may be much much easier to
redirect it using dup2 on the execling side -- I presume that's what the
various shells do.


             -- Dave



More information about the Comp.unix.wizards mailing list