Path: utzoo!attcan!uunet!samsung!sdd.hp.com!ucsd!ogicse!plains!bakke From: bakke@plains.UUCP (Jeffrey P. Bakke) Newsgroups: comp.lang.c Subject: Re: popen() can't tell if cmd fails Summary: Used fork'd execl instead Keywords: popen() Message-ID: <5197@plains.UUCP> Date: 2 Jul 90 04:48:08 GMT References: <855@cmsfl> Organization: North Dakota State University, Fargo Lines: 35 In article <855@cmsfl> nigel@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@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@plains.bitnet | - Marvin the PA