main return value

Pat Rankin rankin at eql.caltech.edu
Tue May 14 08:59:01 AEST 1991


In article <16136 at smoke.brl.mil>, gwyn at smoke.brl.mil (Doug Gwyn) writes...
>In article <memo.1010816 at lynx.northeastern.edu> cschmidt at 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.

     <stdlib.h> 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 <stdlib.h>
#ifdef VMS
# include <ssdef.h>
# include <stsdef.h>
# 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 <stdlib.h> 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 at eql.caltech.edu



More information about the Comp.lang.c mailing list