[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Redirecting stdin



Hi!

On Fri, Apr 25, 1997 at 12:44:33AM +0200, you wrote:
> What´s the proper way of redirecting a child´s stdin under MiNT? I guess I
> could use Fforce() and map stdin to a file (Which is basically what I
> need.), but does this work under MiNT? What´s the "unix-way" of doing
> this?
If you're using the MiNTLib (and it supports what I describe now ...), 
the easiest way would be popen(), which returns a FILE * to a pipe opened 
to a child. This pipe can either be stdin or stdout of the child.

So, popen("/usr/lib/sendmail", "w") will open a pipe and run sendmail 
with its stdin connected to the pipe. It returns a file descriptor 
(or NULL on failure) to the pipe. Use fwrite() to feed sendmail, and call 
pclose() to end the connection and kill the child.

popen(path, "r") will open a pipe and run the specified program with its 
stdout connect to the pipe (so "r" or "w" tell you what the calling 
program may do to the file descriptor returned).

Note that popen() has some security risks as it uses system() to execute 
the program. So popen("/usr/lib/sendmail address@abuse.com;rm -rf /") 
will cause a complete deletion of your filesystem (assumed the calling 
process runs as root).

A different and more secure (but also more difficult) approach would be the 
use of pipe(), dup() or dup2() and exec(). If you need more information, 
just contact me, though my implementation may not be very clever ...


Ciao

Thomas


--
Thomas Binder (Gryf @ IRC)     binder@rbg.informatik.th-darmstadt.de
PGP-key available on request!  gryf@hrzpub.th-darmstadt.de