Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: exit(-1), 0 is sometimes magic Message-ID: <562@cresswell.quintus.UUCP> Date: 23 Jan 88 03:20:54 GMT References: <502@cresswell.quintus.UUCP> <6935@brl-smoke.ARPA> <1179@wjvax.UUCP> <1234@nmtsun.nmt.edu> Organization: Quintus Computer Systems, Mountain View, CA Lines: 60 Keywords: exit, zero, shells Summary: RFTM In article <1234@nmtsun.nmt.edu>, hydrovax@nmtsun.nmt.edu (M. Warner Losh) writes (with spelling unchanged): > As a side note. exit(0) in UNIX isn't quit correct. The UNIX shell, > as far as I know, doesn't give a hill of beans wheather you say exit(0), > exit(1000), exit (magic_number).... It is the SHELL SCRIPTS that people > write that impose this "standard" upon UNIX. I could be wrong about this He is. sh(1): Option: set -e is "exit immediately if a command exits with a non-zero exit status". Control structures: Cmd1 && Cmd2 executes Cmd2 iff Cmd1 returns a zero exit status, Cmd1 || Cmd2 executes Cmd2 iff Cmd1 returns a non-zero exit status. if Cmds then ... treats zero exit status as true, non-zero as false. while Cmds do ... is the same. csh(1): Option: set -e means exit if a command terminates abnormally OR exits with NONZERO. "Builtin commands that fail return exit status `1', all other builtin command set status to `0'." && and || are as in sh(1); if and while take expressions, not commands, as arguments. make(1): "make normally terminates when a command returns non-zero status, unless the -i or -k option is in effect." It is quite definitely the shells which impose the convention 0 == success == true, non-zero ==failure == false, not the scripts. Try the following in a recent Bourne shell: # "foo N" prints true or false foo() { ( exit $1 ) && ( echo true ) || ( echo false ) } # try it foo 0 # true foo 1 # false This is relevant to some C programmers because the 0/non-0 convention is the direct opposite of the C convention. This confused me for a couple of days. It is possible to program around the non-zero-exit- status-means-failure convention, but it definitely is a convention that is supported by UNIX shells (not scripts) and by DEC/Shell in VMS. Note that this convention is reflected back into C by the system(3) command: system "returns the exit status of the shell". This means that the 0==success, non-zero==failure rule is visible to C. (You *do* check the result of calls to system(), don't you?) Alas, all that the Oct86 dpANS says about the result of system() is that system(NULL) returns non-zero to indicate that there is a command interpreter system(non-NULL) is completely implementation-defined. Beware, by the way. Due to byte sex problems, even in UNIX, the result of system() may be shifted left by 8 (it is on a SUN). We still have the question: exit(0) and exit(1) make sense in UNIX, VAX/VMS, IBM-MVS, and IBM-VM. Which implementations of C do they *not* work in? Which implementations of C do *not* use 0==success for system()?