[comp.sys.ibm.pc] HELP: MSC system call

deln@druhi.ATT.COM (SloaneN) (05/24/89)

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?

eg.  exit_code= system( "cl -E ......" );

if "cl" fails, I still get a zero return code because the "system"
command successfully spawned "cl".  What I want is the exit code
from "cl".

Thanks in advance,

Nikki

kneller@cgl.ucsf.edu (Don Kneller) (05/26/89)

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?
>
>eg.  exit_code= system( "cl -E ......" );
>
>if "cl" fails, I still get a zero return code because the "system"
>command successfully spawned "cl".  What I want is the exit code
>from "cl".

The problem (which has been there forever), is that the system()
call actually spawns the following:

	command /c cl -E ...

That is, command.com is spawned and it in turn spawns cl.  However,
command.com does not return the return code of "cl -E ...", it returns 0.
At one time I thought a version of MSDOS had fixed this, but it is still
a problem in PCDOS 3.3, so maybe it was just a wishful thought.

- don
-----
	Don Kneller
UUCP:		...ucbvax!ucsfcgl!kneller
INTERNET:	kneller@cgl.ucsf.edu
BITNET:		kneller@ucsfcgl.BITNET

bobmon@iuvax.cs.indiana.edu (RAMontante) (05/26/89)

-In article <4267@druhi.ATT.COM> deln@druhi.ATT.COM (SloaneN) writes:
->
->eg.  exit_code= system( "cl -E ......" );
->
->if "cl" fails, I still get a zero return code because the "system"
->command successfully spawned "cl".  What I want is the exit code
->from "cl".

kneller@socrates.ucsf.edu.UUCP (Don Kneller) <11624@cgl.ucsf.EDU> :
-
-The problem (which has been there forever), is that the system()
-call actually spawns the following:
-
-	command /c cl -E ...
-
-That is, command.com is spawned and it in turn spawns cl.  However,

Depending on the application, you may be able to just spawn() the "cl"
program directly.  If possible, this is faster and uses less system
resource anyway.  spawn() returns the child's exit value (>= 0, of course)
or -1 if the child process didn't occur (e.g., not enough memory).

spawn() is actually a family of functions that create and run child
processes.  Like a "fork()/exec()" sequence under Unix.  Turbo C has
spawn(), I assume that MSC has something like it too.

stephen@ziebmef.uucp (Stephen M. Dunn) (05/28/89)

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?

   If you're trying to spawn some other program through a system call that
first invokes COMMAND.COM, you're using the wrong system call.  Don't ask
me what the name of the one you want is, but there should be a call that
directly runs your other program.  If you're running a .COM or an .EXE,
there is no need to go through COMMAND.COM.  Besides which, COMMAND uses
a few K of memory, so if you've only just got enough memory left, this 
might become important.
   Try looking through your C manual for another call (try FORK, it may be
the one) that doesn't bother with COMMAND.COM.


-- 
-------------------------------------------------------------------------------
! Stephen M. Dunn              stephen@ziebmef.UUCP ! DISCLAIMER:  Who'd ever !
! Take off to the Great White North eh, ya hosehead ! claim such dumb ideas?  !
-------------------------------------------------------------------------------

erbo@pear.ucsb.edu (Eric J. Bowersox) (05/30/89)

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 -----   \\