redirect stdin when using execl

Jim Vlcek vlcek at mit-caf.MIT.EDU
Sun Jun 11 13:48:31 AEST 1989


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.  Such redirection is, in fact, precisely
the intended usage of this function call.  Is freopen() a Berklism, or
do you have that in Sys V as well?  I'm almost completely unfamiliar
with the latter.

The original example, passing the string "<" and the redirected file,
was so innocently misguided I really got a laugh out of it.
execl()'ing "/bin/sh" to parse the command, or using system(), on the
other hand, were somewhat less amusing.

Jim Vlcek  (vlcek at caf.mit.edu  uunet!mit-caf!vlcek)



More information about the Comp.lang.c mailing list