[comp.lang.c] 1 Turbo C question

bonak%cs.uiowa.edu@RELAY.CS.NET (08/30/87)

            Thanks to all the people who helped me with my query
            regarding how to specify a path in the function fopen.
            
            Nobody responded to the second question so I'll try again:
            Does anybody know a way of getting MS-DOS to use the return
            value of an exit(value) that comes from a C program
            termination?  I.e. I'd like to know (at a DOS level) whether
            a program terminated with exit(0) or exit(1).
            
            esmail

 
            p.s.: I am using Turbo C and DOS 3.1

toma@killer.UUCP (Tom Armistead) (08/31/87)

In article <9061@brl-adm.ARPA>, bonak%cs.uiowa.edu@RELAY.CS.NET (Esmail Bonakdarian) writes:
> 
>             Does anybody know a way of getting MS-DOS to use the return
>             value of an exit(value) that comes from a C program
>             termination?  I.e. I'd like to know (at a DOS level) whether
>             a program terminated with exit(0) or exit(1).

You have something in dos called errorlevel that will give you the value
you exit()ed with. This could be used in a batch file like this...

.	REM RUN MAIN PROGRAM
.	MAINPGM
.	IF ERRORLEVEL 0 GOTO GOOD
.	IF ERRORLEVEL 1 GOTO BAD
.	IF ERRORLEVEL 2 GOTO HAWAII
.	:GOOD
.	ECHO WE GOT A GOOD RETURN
.	GOTO END
.	:BAD
.	ECHO WE GOT A BAD RETURN
.	GOTO END
.	:HAWAII
.	ECHO WE ARE GOING TO HAWII
.	:END
.	EXIT

the number following 'ERRORLEVEL' will be the number you exited with.

Hope this will help...
toma
UUCP:           ihnp4\
                      \killer!toma
  infoswx!convex!dj3b1/
Tom Armistead
-- 
UUCP:           ihnp4\
                      \killer!toma
  infoswx!convex!dj3b1/
Tom Armistead

PEPRBV%CFAAMP.BITNET@wiscvm.wisc.EDU (Bob Babcock) (08/31/87)

>>    Does anybody know a way of getting MS-DOS to use the return
>>    value of an exit(value) that comes from a C program
>>    termination?  I.e. I'd like to know (at a DOS level) whether
>>    a program terminated with exit(0) or exit(1).

If you are running the program in question  from another program,
you can get the return  code with INT 21h, function  4dh.  If you
are running  the program  from the DOS prompt,  I think the error
code  is lost  by the time  you  get  back  to the  prompt.   The
solution  is to run the program  from a batch  file and use an IF
ERRORLEVEL  test.   You could  have  a BAT file called  RUN which
looked something like this:

    ECHO OFF
    %1
    IF ERRORLEVEL 1 ECHO %1 RETURN CODE WAS NOT ZERO

Then say RUN program_name  instead of program_name.   It would be
nice if DOS told you the return  code, but I guess the philosophy
is that the program  should give a useful error message,  and the
only use for the return  code is controlling  the flow of a batch
file or letting the invoking  program know (since it doesn't  see
screen messages).

jvc@mirror.UUCP (09/01/87)

/* Written  7:35 pm  Aug 30, 1987 by toma@killer.UUCP in comp.lang.c */
>You have something in dos called errorlevel that will give you the value
>you exit()ed with. This could be used in a batch file like this...
>
>.	REM RUN MAIN PROGRAM
>.	MAINPGM
>.	IF ERRORLEVEL 0 GOTO GOOD
>.	IF ERRORLEVEL 1 GOTO BAD
>.	IF ERRORLEVEL 2 GOTO HAWAII
>.	:GOOD
>.	ECHO WE GOT A GOOD RETURN
>.	GOTO END
>.	:BAD
>.	ECHO WE GOT A BAD RETURN
>.	GOTO END
>.	:HAWAII
>.	ECHO WE ARE GOING TO HAWII
>.	:END
>.	EXIT
>
>the number following 'ERRORLEVEL' will be the number you exited with.

He's got the right idea but it won't work as coded.  Refer to page
7-34 of DOS 3.2 Reference, paragraph 2 (approx.):
	"ERRORLEVEL number is true if the previous program
         had an exit code of number or higher.  The number 
	 is specified as a decimal value."

In order for the above code segment to work you must reverse the 
order of the IF statements.  It would also be wise to cover the 
case in which the program might return a 3 or greater since in this
example you only want to GOTO HAWAII if you get a return of 2.
 
Also note that EXIT won't terminate a batch file and therefore
should not be in this code segment. (DOS 3.1, 3.2)

Corrected code segment:

	REM RUN MAIN PROGRAM
	MAINPGM
	REM Look at return codes
	REM A return of 3 or greater is unexpected
	IF ERRORLEVEL 3 GOTO UNEXPECTED
	REM Now we can test for 2 (or greater but we've already 
	REM  handled values greater than 2)
	IF ERRORLEVEL 2 GOTO HAWAII
	REM Test for return of 1
	IF ERRORLEVEL 1 GOTO BAD
	REM Test for return of 0
	IF ERRORLEVEL 0 GOTO GOOD
	:GOOD
	ECHO WE GOT A GOOD RETURN
	GOTO END
	:BAD
	ECHO WE GOT A BAD RETURN
	GOTO END
	:HAWAII
	ECHO WE ARE GOING TO HAWII
	GOTO END
	:UNEXPECTED
	ECHO Unexpected return value ( >=3 )
	:END
	REM No more commands after this so execution terminates

-------------------------------------------------------------------------
Jim Champeaux	jvc@mirror.TMC.COM
		{mit-eddie, ihnp4, wjh12, cca, cbosgd, seismo}!mirror!jvc
Mirror Systems,	2067 Massachusetts Avenue, Cambridge, MA 02140
Telephone:	(617) 661-0777

V4007%TEMPLEVM.BITNET@wiscvm.wisc.EDU (Mike Brower) (09/01/87)

Dear Bob
     Account V4007 of TempleVm is no longer Mike Brower's account.  The
 new user is a consultant named Franky Choi.

marc@mfbbs.UUCP (09/12/87)

Followup-To:

In article <1445@killer.UUCP> toma@killer.UUCP (Tom Armistead) writes:
>In article <9061@brl-adm.ARPA>, bonak%cs.uiowa.edu@RELAY.CS.NET writes:
>>             value of an exit(value) that comes from a C program
>>             termination?
>
>.	MAINPGM
>.	IF ERRORLEVEL 0 GOTO GOOD
>.	IF ERRORLEVEL 1 GOTO BAD
>.	IF ERRORLEVEL 2 GOTO HAWAII
> [correct stuff deleted]
>---
>UUCP: killer!toma
>Tom Armistead

  That batch file won't work (don't worry, I consider it Microsofts fault!).
DOS tests errorlevels by equal to or greater than the number you are testing
against.  This means in your batch file, EVERY errorlevel above and equal to 0
would 'GOTO GOOD'.  So, the correct batch file would be:

IF ERRORLEVEL 2 GOTO HAWAII
IF ERRORLEVEL 1 GOTO BAD
IF ERRORLEVEL 0 GOTO GOOD
[etc]
-- 
 Marc Randolph     UUCP: ...!rutgers!pbox!svo!mfbbs!marc
                   FidoNet: 170/329 or 170/220