[comp.sys.ibm.pc] Setting DOS errorlevel in C

murillo@sigi.Colorado.EDU (Rodrigo Murillo) (03/14/88)

Does someone out there know how to set the DOS errorlevel number
in C?  I want to write a batch file that can branch according to the
value of this number:

     if errorlevel 1 echo Big...
     if errorlevel 2 echo Bigger...
     if errorlevel 3 echo Biggest.

Please send code fragments if possible.
-- 
_______________________________________________________________________________
 Rodrigo Murillo, University of Colorado - Boulder  (303) 761-0410 
 murillo@boulder.colorado.edu | ..{hao|nbires}!boulder!murillo
 ( Machines have less problems.  I'd like to be a machine. -- Andy Worhol )

wew@naucse.UUCP (Bill Wilson) (03/16/88)

That will depend upon your C compiler.  Normally all you have
to do is return an integer number in the exit function.  i.e.

   exit(1);

will return a 1 for the error level upon completion of the 
program.

Bill Wilson.

everett@hpcvlx.HP.COM (Everett Kaser) (03/17/88)

It's set automatically by the 'exit' function; i.e.,

        exit(0);         exits and sets ERRORLEVEL to 0
        exit(1);         exits and sets ERRORLEVEL to 1
        exit(2);         exits and sets ERRORLEVEL to 2

I hope you see the trend here. :-)

berger@clio.las.uiuc.edu (03/17/88)

This depends on what C compiler you're using.  In DeSmet C, you just
invoke exit(errorlevel) to exit the program and set errorlevel.  I
think it works the same in Computer Innovations C-86.

			Mike Berger
			Department of Statistics 
			Science, Technology, and Society
			University of Illinois 

			berger@clio.las.uiuc.edu
			{ihnp4 | convex | pur-ee}!uiucuxc!clio!berger

madd@bu-cs.BU.EDU (Jim Frost) (03/17/88)

In article <4831@sigi.Colorado.EDU> murillo@boulder.Colorado.EDU (Rodrigo Murillo) writes:
>Does someone out there know how to set the DOS errorlevel number
>in C?  I want to write a batch file that can branch according to the
>value of this number:
>
>     if errorlevel 1 echo Big...
>     if errorlevel 2 echo Bigger...
>     if errorlevel 3 echo Biggest.
>
>Please send code fragments if possible.

You're going to get a million of these, but I thought I'd add to your
mail.  The answer is so obvious that it's often missed.

DOS ERRORLEVEL is nothing but a check of the return code for a program
(as specified when function 4CH Terminate a program (EXIT) is called).

The IBM Disk Operating System Technical Manual says:

	Purpose:  Terminates the current process and transfers control
		  to the invoking process.

	On Entry	Register Contents
	AH		4CH
	AL		Return code

	On Return	Register Contents
			NONE

	Remarks:  In addition, a return code can be sent.  The return
		  code can be interrogated by the batch subcommands IF
		  and ERRORLEVEL and by the wait function call 4DH.
		  All files opened by the process are closed.

In C on the PC, the return code is an optional argument to the exit()
command.  Thus, exit(2) will return ERRORLEVEL 2.

Happy hacking,

jim frost
madd@bu-it.bu.edu

bobmon@iuvax.cs.indiana.edu (RAMontante) (03/17/88)

I observe that in Turbo C (v1.5), under MSDOS 3.1, I can set the errorlevel
either with
	exit( errorlevel );
or with
	return( errorlevel );

Just out of curiousity, are there any [good] stylistic, philosophical, or
other reasons for choosing one form over the other?  (Such as compatibility?)

ljz@fxgrp.fx.com (Lloyd Zusman) (03/18/88)

In article <4831@sigi.Colorado.EDU> murillo@sigi.Colorado.EDU (Rodrigo Murillo) writes:

   Does someone out there know how to set the DOS errorlevel number
   in C?  I want to write a batch file that can branch according to the
   value of this number:

	if errorlevel 1 echo Big...
	if errorlevel 2 echo Bigger...
	if errorlevel 3 echo Biggest.


In most (all?) MSDOS C compilers, the exit() function will set errorlevel:

	main()
	{
		...
		...
		exit(3);
	}

The above will set errorlevel to 3.  Using "exit(N)" will set errorlevel
to N.

--
---
    Lloyd Zusman		    Ma Bell:	(415) 961-5183
    FX Development Group, Inc.	    Internet:	fxgrp!ljz@ames.arpa
    Mountain View, California	    UUCP:	...!ames!fxgrp!ljz

abell@polyslo.UUCP (abell) (03/18/88)

Taking this question one step further, is it possible to get this DOS "error
level" into a DBASE III application?

Alan

abell@polyslo.UUCP (abell) (03/18/88)

Forgot to include my signature with my question.  Just in case, I'll repeat
the question:

Is it possible to return this DOS "error level" from a C program to a
DBASE III application?

-------------------------------------------------------------------------------
| Alan Bell			|    ...!ihnp4!csun!polyslo!abell	      |
| Computer Systems Lab		|    ...!{csustan,csun,sdsu}!polyslo!abell    |
| Cal Poly State Univ.		|    ...!ucbvax!voder!polyslo!abell	      |
| San Luis Obispo, CA  93407	|    					      |
-------------------------------------------------------------------------------

jio@cpsc55.ATT.COM (James Odom) (03/29/88)

From article <6985@iuvax.cs.indiana.edu>, by bobmon@iuvax.cs.indiana.edu (RAMontante):
> I observe that in Turbo C (v1.5), under MSDOS 3.1, I can set the errorlevel
> either with
> 	exit( errorlevel );
> or with
> 	return( errorlevel );
> 
> Just out of curiousity, are there any [good] stylistic, philosophical, or
> other reasons for choosing one form over the other?  (Such as compatibility?)

Normally, "exit" is used to stop the program and exit to the operating 
system.  "return" is used to report status back to a calling function.
If "return" is used in the main function, it is treated the same as "exit"



+------------------------------------------------------------------------+
|James I. Odom                                                           |
|AT&T-IS CPSC         Denver, Co               Voice:   (303) 889-0211   |
|ATTMAIL:  JODOM      Compuserve: 70070,137    uucp:    ihnp4!cpsc55!jio |
|------------------------------------------------------------------------|
|Disclaimer: Any opinions expressed are my own etc.                      |
+------------------------------------------------------------------------+