windows exit codes truncated to 1 Byte

Björn Stabel stabelweb@gmx.de
Thu Mar 31 13:31:00 GMT 2016


Am 31.03.2016 um 15:01 schrieb Cristian:
> I noted that exit codes returned by CMD and other apps (msiexec) are truncated to 1 byte (0 .. 255).
>
> Steps to reproduce:
>
> 1) Create a batch with this content:
> $ cat ./test.bat 
> @echo off 
> rem ==================== 
> echo Test exit code 266 
> exit /B 266 
> rem ==================== 
>
> 2) run the batch file:
> $ ./test.bat 
> Test exit code 266 
>
> 3) check exit code
> $ echo $? 
> 10 
>
>
> I would expect 266 but I got 10 instead. I checked for other codes and the result is the same. 
> Windows exit codes are represented using 32 bits, so is this limitation intentional?
POSIX specifies that the lower 8 bits of the exit status be made
available to the caller of wait/waitpid:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html

"WEXITSTATUS(stat_val) - If the value of WIFEXITED(stat_val) is
non-zero, this macro evaluates to the low-order 8 bits of the status
argument that the child process passed to _exit() or exit(), or the
value the child process returned from main()."

Maybe there's another way to get the entire status value, but this looks
like a POSIX limitation to me.
Cygwin is conformant here.

The more interesting problem is what happens if you do "exit /B 256" in
your batch.
In that case, bash would show a return status of zero.
That's what the WIFEXITED macro is used for. Not sure how you'd get that
from bash, though.

>
>
> Best regards,
> Cristi
>
> --
> Problem reports:       http://cygwin.com/problems.html
> FAQ:                   http://cygwin.com/faq/
> Documentation:         http://cygwin.com/docs.html
> Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
>
>


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list