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) does not work ??? Message-ID: <560@cresswell.quintus.UUCP> Date: 22 Jan 88 04:39:33 GMT References: <6935@brl-smoke.ARPA> <7208@ki4pv.uucp> <334@splut.UUCP> <5262@cit-vax.Caltech.Edu> Organization: Quintus Computer Systems, Mountain View, CA Lines: 64 Keywords: exit, zero Summary: no, exit(-1) does NOT work. In article <5262@cit-vax.Caltech.Edu>, wen-king@cit-vlsi.Caltech.Edu (Wen-King Su) writes: > In article <555@cresswell.quintus.UUCP> ok@quintus.UUCP (Richard A. O'Keefe) writes: > >sticking to 0 and 1 because they *do* work in UNIX *and* in VMS. > > This seems a little strange to me so I decided to check it out. > Following is a script showing that exit(-1) DOES work in UNIX. To summarise, cat <t.c main(argc, argv) int argc; char **argv; { exit(argc >= 2 ? atoi(argv[1]) : -1); } EOF cc -o t t.c He ran this program on a SUN, and claimed that it works. I too shall run this on a SUN. Behold! Script started on Thu Jan 21 20:09:54 1988 % csh % t ; echo $status -1 % t 254 ; echo $status -2 % exit % sh $ t ; echo $? 255 $ t -2 ; echo $? 254 $ exit % exit script done on Thu Jan 21 20:10:42 1988 To repeat my point, UNIX truncates the argument to 8 bits. The Bourne shell treats this as an unsigned char. The C shell treats this as a signed char. Note that on a SUN, according to /usr/include/sys/wait.h (the include file which describes the termination status result that is returned to a process's parent) the Bourne shell is right (the field is described as unsigned short w_Retcode:8;) and the C shell is wrong. I don't want to argue for one or the other, only to point out that they are different, so exit(-1) isn't even portable between two shells on the same machine! Wen-King Su also says: > success, but I believe application programmers should be free to use > other numbers to their own advantage and OS should provide them with > the maximum flexibility. The issue of portability is something each > programmer has to decide for self, but it is always useful for > those who had the experience to point out trouble spots and recommend > solutions. Well, yes, but before using other numbers to their own advantage, application programmers should at least take the trouble to read the manuals (on a SUN, man 2 exit says explicitly that the low order 8 bits of the status are made available through wait(2), and man 2 wait points you to where those 8 bits are explicitly declared to be UNsigned). I am grateful to Wen-King Su, because I didn't know about this design bug in the C shell before. (The manual doesn't say that status can take negative values, though there is a comment about adding 0200 which may have some faint connexion.) Are there any implementations of C in which exit(0) and exit(1) do NOT work? Presumably there are, or the ANSI committee wouldn't have added the new macros (:-), but which are they?