Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!ucsd!ucsbcsl!pear!erbo From: erbo@pear.ucsb.edu (Eric J. Bowersox) Newsgroups: comp.sys.ibm.pc Subject: Re: HELP: MSC system call Summary: spawn() description (kind of long) Keywords: system call spawn Message-ID: <1880@hub.ucsb.edu> Date: 30 May 89 03:06:41 GMT References: <4267@druhi.ATT.COM> <1989May27.142613.21449@ziebmef.uucp> Sender: news@hub.ucsb.edu Reply-To: erbo%cornu@hub.ucsb.edu (Eric J. Bowersox) Followup-To: comp.sys.ibm.pc Distribution: comp Organization: UC Santa Barbara, Campus Club for Computer People (CCCP) Lines: 80 In article <1989May27.142613.21449@ziebmef.uucp> stephen@ziebmef.UUCP (Stephen M. Dunn) writes: >In article <4267@druhi.ATT.COM> deln@druhi.ATT.COM (SloaneN) writes: >$I'm having a problem with the return code from system(). What >$appears to happen is that the return code (as the manual states) >$indicates whether command.com could successfully execute the command >$or not. How do I get the exit code of the spawned command? > > [...] > > Try looking through your C manual for another call (try FORK, it may be >the one) that doesn't bother with COMMAND.COM. This may be of general interest to MS-DOS C programmers, so I'm posting it. [Note: The following is applicable under Turbo C; your mileage may vary.] The set of functions beginning with "spawn..." create and run child processes. There are eight in all: int spawnl(int mode,char *path,char *arg0,arg1,...,argn,NULL); int spawnle(int mode,char *path,char *arg0,arg1,...,argn,NULL,char *envp[]); int spawnlp(int mode,char *path,char *arg0,arg1,...,argn,NULL); int spawnlpe(int mode,char *path,char *arg0,arg1,...,argn,NULL,char *envp[]); int spawnv(int mode,char *path,char *argv[]); int spawnve(int mode,char *path,char *argv[],char *envp[]); int spawnvp(int mode,char *path,char *argv[]); int spawnvpe(int mode,char *path,char *argv[],char *envp[]); The suffix 'l' indicates that arguments are passed as a list, terminated with a NULL argument, while 'v' indicates that the arguments are passed as an array argv[] of strings. (Hence 'l' is good when you're passing a fixed number of arguments, or none, and 'v' is good when you're passing a variable number.) Those with the suffix 'p' will search the PATH for your command; without it, only the current directory and root directory of the default drive will be searched. Those with the suffix 'e' allow you to pass an array envp[] of environment variables (in the form "NAME=value"); without it, the child inherits the parent's environment. (Note that if you use argv[] and/or envp[], the last element in each must be NULL.) 'mode' specifies how the process is to be run, and it may be one of three values (defined in process.h): P_WAIT Parent process waits for child's termination. This is usually (almost 100%) what you want. P_NOWAIT Parent process runs simultaneously with child process (not implemented). P_OVERLAY Overlays parent process with child process; similar to the exec... calls. 'path' specifies the location of your program file; if you don't specify an extension, or an ending period (the 'null' extension), it will look first for that exact file, then that file with a .COM extension, then that file with an .EXE extension (like DOS would do). If you do specify the extension, it looks for that file only. The return code of spawn... is the exit code of the child process, or -1 if there's an error. In addition, if -1 is returned, 'errno' is set to one of the following: E2BIG Argument list too long (it must be < 128 bytes) EINVAL Invalid argument (like using mode=P_NOWAIT :-) ) ENOENT Path or file not found ENOEXEC Exec format error (not a program file, or a munged .EXE file) ENOMEM Not enough memory to load program Note that any files you have open will stay open in the child process no matter which version of spawn... you use. Also, you must pass at least one argument (arg0 or argv[0]); by convention, this is a copy of 'path', but it may be anything else and won't give an error if it is. Its value will only be accessible to the child process if you're using MSDOS v.3.0 or higher. -- Taken from _Turbo C v.1.5 Reference Manual_, p. 224-7. Hope this helps! :-) As usual, flames to /dev/null, please. ///////// Disclaimer: Uh, well, Huttenback didn't need one... \\\\\\\\ ////// * Eric J. Bowersox (ERBO) * VP, UCSB Campus Computer Club \\\\\\ //// erbo%cornu@hub.ucsb.edu ...!{ucbvax,ucsd}!ucsbcsl!hub!cornu!erbo \\\\ // ----- Vertical is horizontal turned 90 degrees. -- M. Gegus ----- \\