Need a 2-way alternative to popen()

Michael Frank bugboy at portia.Stanford.EDU
Sun Feb 25 22:32:51 AEST 1990


I'm writing a program, and I want it to be able to do two-way
communication with a csh process; it will occasionally type commands
to the csh and read csh's output.  I thought that

	popen("csh", "r+") 

might work, but I tried it, and it doesn't allow writing.  Well,

	popen("csh","w") 

allows me to write commands to the csh, but how can I
get its output to come to me instead of going to stdout?  I could do

	popen("csh > out","w"),

and read the output file as it's being written, but then I have to
worry about the file growing indefinitely.  I'd like to be able to
just have the output available in a pipe.  Well, popen() uses sh, and
the sh doc says something about redirecting to a file descriptor using
>&, so I thought I could do

	popen("csh >&4", "w");

where 4 was the file descriptor of one end of a pipe in my program,
and then I could read() from the other end.  But it didn't work.
Perhaps the sh process doesn't inherit my file descriptors.  I suppose
I *could* do

	popen("csh | sendoutputback","w")

where sendoutputback would be a separate program that would feed
me its standard input using IPC.  But that's ugly and a pain.
Besides, none of this is giving me access to csh's stderr either.

(Alternatively, I wish I could just have my program pretend to be me
logged in on some tty; that would accomplish all of this in one step.)

Can anybody help me?  This shouldn't be that hard to do!!!

Thanks,
Mike Frank



More information about the Comp.unix.questions mailing list