Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird return values from system() calls if negative

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
I seem to be getting incorrect return values from system() calls if they're negative. If a value is positive, things work as expected (return value * 256). If it's negative, it's unpredictable. Observe:
------ simpletest.cpp compiles into simpletest.exe
//simpletest.cpp
int main(int argc, char** argv) { return -215; }
------ returntest.pl
#!/usr/bin/perl -w
$returnvalue = system("test");
print "It returned: $returnvalue";
------ And the results:
./returntest.pl
It returned: 256


No hits on google... what gives?

 
'system' should not return any value.
perldoc -f open
 
Actually the return value is the exit status of the program. (Not really, but to get the actual return status you divide by 256) If you want to actually capture the output of a program you should use backticks:
$value = `<some program>`;

HTH,
Johnny
 
yauncin is correct. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I understand the difference between exit status (or return value of the program) vs output... but the thing is a negative exit status is coming out oddly. The worst I would expect is a signed/unsigned problem (so a negative number like -212 would come out as something like 65324 depending on the variable size). But all negative numbers seem to spit out 255 (I actually had a few typos in the original post because it run on a different machine than the post and I just typed it out). I don't want output, I want an accurate representation of the return value. For reference, I'm doing the tests on a win2k advanced server box.
 
Instead of dividing the return value by 256, try shifting it left 8 bits. That may remove the sign bit that's making it negative, and give you a normal result.
Code:
$returnvalue <<= 8;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Ok i understand that the problem of return error codes could get solved if we shift the data bits or if we divide it by 256 BUT why is in the first place '1' converted to '256' or other integer values multiplied with 256 for return code.

Could anybody help. Thx in advance.
 
From the man pages:
[tt]
system PROGRAM LIST
Does exactly the same thing as &quot;exec LIST&quot;, except
that a fork is done first, and the parent process
waits for the child process to complete. Note that
argument processing varies depending on the number
of arguments. If there is more than one argument in
LIST, or if LIST is an array with more than one
value, starts the program given by the first element
of the list with arguments given by the rest of the
list. If there is only one scalar argument, the
argument is checked for shell metacharacters, and if
there are any, the entire argument is passed to the
system's command shell for parsing (this is /bin/sh
-c on Unix platforms, but varies on other
platforms). If there are no shell metacharacters in
the argument, it is split into words and passed
directly to execvp(), which is more efficient.

The return value is the exit status of the program
as returned by the wait() call. To get the actual
exit value divide by 256. See also the exec entry
elsewhere in this document.
This is NOT what you
want to use to capture the output from a command,
for that you should use merely backticks or qx//, as
described in the section on `STRING` in the perlop
manpage.

Like exec(), system() allows you to lie to a program
about its name if you use the &quot;system PROGRAM LIST&quot;
syntax. Again, see the exec entry elsewhere in this
document.

Because system() and backticks block SIGINT and
SIGQUIT, killing the program they're running doesn't
actually interrupt your program.

@args = (&quot;command&quot;, &quot;arg1&quot;, &quot;arg2&quot;);
system(@args) == 0
or die &quot;system @args failed: $?&quot;

You can check all the failure possibilities by
inspecting $? like this:

$exit_value = $? >> 8;
$signal_num = $? & 127;
$dumped_core = $? & 128;

When the arguments get executed via the system
shell, results and return codes will be subject to
its quirks and capabilities. See the section on
`STRING` in the perlop manpage and the exec entry
elsewhere in this documentfor details.

[/tt]
Hope this helps. Cheers, Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top