system() always returns 0? Yep...
RAMontante
bobmon at iuvax.cs.indiana.edu
Wed Jan 25 14:18:49 AEST 1989
[ The "system()" returns 0 after invoking COMMAND.COM,
in MSC and TC on MSDOS machines ]
<646 at sactoh0.UUCP> bkbarret at sactoh0.UUCP (Brent K. Barrett):
>
> That may not be entirely correct, at least as far as TC goes. In
>Turbo C 2.0 (and, I believe, 1.5, even though it was not
>documented), system() will return 0 if command.com was successfully
>loaded, and -1 if command.com was not found. I do not know the ANSI
>standard on this beast (if indeed there is one), but since MSC and
>TC both have it functioning this way, I'm going to assume it's
>ANSI.
"System()" in Turbo C is specifically defined to "...invoke COMMAND.COM
to execute a command...". In TCv1.5 the return value is described as
the exit status of COMMAND.COM. Unfortunately (but not surprisingly)
COMMAND.COM exits with a 0 if it successfully loads and runs, whether
the executable file that it was supposed to execute was successful or
not. So the only information that you can get back from COMMAND.COM is
whether it (loaded and) ran, or crashed the whole dam' machine; nothing
about what it did while it was running. This has nothing to do with
ANSI, which doesn't specify the behavior of MSDOS. The C "system()"
call is doing what it should, but what it receives from the operating
system isn't any use to anyone (except in the case of a missing
COMMAND.COM file).
> You are correct that spawn()/exec() should be used to return the
>proper errorlevels.
No problem here, as COMMAND.COM isn't involved. The executed file is
responsible for returning a meaningful value to spawn() / exec().
More information about the Comp.lang.c
mailing list