Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!jarthur!nntp-server.caltech.edu!eql7.caltech.edu!rankin From: rankin@eql.caltech.edu (Pat Rankin) Newsgroups: comp.lang.c Subject: Re: main return value Message-ID: <13MAY91155901@eql7.caltech.edu> Date: 13 May 91 22:59:01 GMT References: <16136@smoke.brl.mil> Sender: news@nntp-server.caltech.edu Reply-To: rankin@eql.caltech.edu Organization: Environmental Quality Laboratory, Caltech Lines: 59 News-Software: VAX/VMS VNEWS V1.4-a4 In article <16136@smoke.brl.mil>, gwyn@smoke.brl.mil (Doug Gwyn) writes... >In article cschmidt@lynx.northeastern.edu writes: >>What value should the standard function MAIN return? .. >> [end] with the line "return EXIT_SUCCESS". The problem with this is >> that EXIT_SUCCESS is zero, even in the VAX version, and when a VMS >> program terminates and returns zero to VMS, VMS displays the >> system message for status code zero. > > If your VMS C compiler really has this bug, get the vendor to fix it. > The DEC representative to X3J11 agreed than VMS C would map a 0 exit > status value to SYS$SUCCESS (an odd number, probably 1) before > handing in back to the invoking environment. The EXIT_* macros were > introduced primarily as a political compromise to accommodate VMS C. The run-time library routine ``exit()'' does convert 0 to a successful status (SS$_NORMAL :-). However, the compiler--which does not claim to be ANSI compliant yet--does not do the same conversion for ``return'' from main(). Needless to say, having main's return differ from from exit is non- conforming. defines EXIT_SUCCESS as 0, which is a problem considering the compiler's failure to fixup the final return value. Since a new compiler release came out a couple of months ago and didn't fix this problem, it may be around for a while... I suggest the following work-around: #include #ifdef VMS # include # include # undef EXIT_SUCCESS # define EXIT_SUCCESS SS$_NORMAL # undef EXIT_FAILURE # define EXIT_FAILURE (SS$_ABORT | STS$M_INHIB_MSG) #else # ifndef EXIT_SUCCESS # define EXIT_SUCCESS 0 # endif # ifndef EXIT_FAILURE # define EXIT_FAILURE 1 # endif #endif You can hard code 1 and 0x1000002C instead of the SS$_ values if you don't want to #include those VMS-specific headers. You can omit the #undefs for VAXC, but GNUC will issue warnings without them. (The VMS port of gcc doesn't fixup the value returned from main() [yet?] either. As a matter of fact, the latest release still doesn't even have available, even though it predefines __STDC__ as 1 by default.) One thing to note is that main() is called directly from the "image activator" portion of the command interpretor. The equivalent of crt0 is inserted by the compiler prior to the first executable code within main itself rather than linked as something which calls main, so it isn't around to receive and post-process the final value returned by main. The problem should be pretty easy to solve though. Pat Rankin, rankin@eql.caltech.edu