Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!quintus!ok From: ok@quintus.UUCP (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: exit(-1) Message-ID: <502@cresswell.quintus.UUCP> Date: 31 Dec 87 07:17:08 GMT Organization: Quintus Computer Systems, Mountain View, CA Lines: 28 Keywords: negative exit values inadvisable I just installed a program which came over one of the *sources* newsgroups. The accompanying README was extremely funny: it said The program should be owned by root and suid. But that's not a C joke. What is of interest here is its use of exit(). It used exit() in two ways: exit(1) /* can't find datum in file */ exit(-1) /* can't fopen file, either "r" or "w" */ Oddly enough, it did not use exit(0) at the end of main(). Given all the hoo-haa we've been having about exit() recently, I thought it might not be out of place to warn people that the value given to exit() is often truncated to 8 bits on the way out. For example, in UNIX, for which this program was intended, saying this-program some-argument if [ #? -eq -1 ] ; then ... fi will NEVER succeed, because the return value is going to be 255. The VAX-11 C manual explicitly says that the argument of exit() should be 0 or a value errno might have. The Oct86 dpANS said only that 0 arguments indicate success and non-0 arguments indicate failure in some system-dependent way. Except where you are writing code for a specific system (say you have #if cases for MVS, VMS, and UNIX) it seems like a good idea to pass only 0 or 1 to exit().