Detecting exec(2) failing after performing fork(2)?

Don Libes libes at cme.nist.gov
Sat Mar 9 00:55:25 AEST 1991


In article <1991Mar7.222033.4711 at nat.vu.nl> maart at nat.vu.nl (Maarten Litmaath) writes:
>	shj at login.dkuug.dk (Stig Jacobsen) writes:
>>Yeah! It was something like this that I wanted, where I get no
>>ugly messages splattering over the parents screen and where I get
>>an error return from the spawn call and not via some signal. [...]

>The interface might be changed to:

>	int	spawn(argv, pidp)
>	char	**argv
>	int	*pidp;
>..and spawn() would return the PID in `*pidp'.

If you have 'expect' (as of 1/91), you can use the library that comes
with it from C or C++.  The following functions exist:

     int
     exp_spawnl(file, arg0 [, arg1, ..., argn] (char *)0);
     char *file;
     char *arg0, *arg1, ... *argn;

     int
     exp_spawnv(file,argv);
     char *file, *argv[ ];

     extern int exp_pid;

Both of these fork a new process so that stdin, stdout, and stderr can
be written and read by the current process.  A file descriptor is
returned which corresponds to the process's stdin, stdout, and stderr.
the new process.  exp_pid is set to the pid of the new process.

An additional interface exists, styled after popen:

     FILE *
     exp_popen(command);
     char *command;

In all of these, exec errors are returned by reading output of the new
process (as the original poster requested).  The library has a number
of other related I/O functions to control interactive processes from C.
See the man page for more info.

Don Libes          libes at cme.nist.gov      ...!uunet!cme-durer!libes



More information about the Comp.unix.programmer mailing list