popen() can't tell if cmd fails

Jeffrey P. Bakke bakke at plains.UUCP
Mon Jul 2 14:48:08 AEST 1990


In article <855 at cmsfl> nigel at cmsfl@labtam.oz (Nigel Harwood) writes:
> As a general question, how much use to people make of the popen()
> function in programs where failures must be detected ?
> 
> I am working on a program where I would like to use
> popen() but can see no way of detecting when the command exits
> with a bad status.
> 
> At least not easily anyway.
> 
> Popen() will return a NULL pointer if the creation of the 
> environment to run the command under fail but not if the command
> fails.
> 

I was stuck in a similar situation before and I couldn't figure any
simple way around it.  I talked to the local system admin who suggested 
the solution which I currently use.  

I make a function called 'CallSystem' which is sent the program and
or args to execute.  I then fork the process (fork()) to obtain a
child process, I then create a pipe from the parent to the child and then
once that is complete, I have the child perform an execl - variant function
based on my needs.  when you execl the child is completely replaced by the
program you call so that the pipe from the parent to the child, is actually
a pipe from the program you want to run and the parent.  And of course, 
using this method you can monitor the execution and termination status
of the child function as you would normally.  It takes some messing 
around and reading of the man pages but it does work and work well once
you know what you're doing.


Jeffrey P. Bakke     | Internet: bakke at plains.NoDak.edu | "Life... don't talk
2550 15th Str S #23B | UUCP    : ...!uunet!plains!bakke |  to me about life..."
Fargo, ND  58105     | BITNET  : bakke at plains.bitnet    |    - Marvin the PA



More information about the Comp.lang.c mailing list