www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - std.process.system() returns wrong value on linux

reply Sam McCall <tunah.d tunah.net> writes:
 From the phobos docs:
int system(char[] command)
     Execute command in a command shell. Returns exit status of command.

The system() function just calls the corresponding C function:
int system(char[] command) {
     return std.c.process.system(toStringz(command));
}

I'm not sure about windows, but on unix system(char*) returns an opaque 
int that can only be portably accessed using macros defined in 
sys/wait.h, in particular
WEXITSTATUS(int stat)
gets (the low 8 bits of) the exit status.

This bit me today when I tried to do exit(system(...)) (I didn't know 
about std.c.exec*...
Sam
Jul 04 2004
parent reply Sam McCall <tunah.d tunah.net> writes:
On linux, if you tell dmd to link against a nonexistant file, it will 
call gcc to link it, and gcc will return 1. DMD doesn't properly extract 
the exit status from the result of waitpid, and returns 256 (which is 
equivalent to 0, as only the low byte is significant).
The fix: (near the end of runLINK() in dmd/link.c)

	waitpid(childpid, &status, 0);

	status=WEXITSTATUS(status); // INSERT THIS LINE

	if (status)
		printf("--- errorlevel %d\n", status);
	return status;

Sam
Jul 05 2004
parent "Walter" <newshound digitalmars.com> writes:
Got it, thanks!
Jul 06 2004