LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-03-2009, 01:20 PM   #1
nemobluesix
LQ Newbie
 
Registered: May 2008
Distribution: fedora 9
Posts: 13

Rep: Reputation: 0
script exit code to system() c function


Hi guys,

Consider the following code:
Code:
...
int ret=0;
ret=system("exit 1");
...
ret should become 1 but instead it gets 256.

This is a cgi script if it matters...

What am I missing?
Thx
 
Old 07-03-2009, 02:35 PM   #2
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 61

Rep: Reputation: 22
A quick google turned up as far as I can tell that you should run the result through WEXITSTATUS

Code:
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>

int main()
{
	int ret = 0;
	ret = WEXITSTATUS(system("exit 1"));
	printf("%d\n", ret);

}
 
Old 07-05-2009, 07:28 AM   #3
nemobluesix
LQ Newbie
 
Registered: May 2008
Distribution: fedora 9
Posts: 13

Original Poster
Rep: Reputation: 0
thanks aspire1!
It works, can's say "as expected", but it works .

thanks again
 
Old 07-05-2009, 09:12 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,298

Rep: Reputation: 175Reputation: 175
look at man waitpid maybe.

the lower 8 bits from a system is a bitmask.
these signify how the process exited e.g. a signal.
the actual return code you need to right shift by 8.

this may be of interest to you,
observe, simplified from aspire:
Code:
#include <sys/wait.h>

main(int argc, char ** argv)
{
    WEXITSTATUS(256);
}

if i put the above through the preprocessor:

Code:
gcc -E 1.c
<cut>
main(int argc, char ** argv)
{
    ((256) >> 8);
}
 
Old 07-06-2009, 06:56 AM   #5
nemobluesix
LQ Newbie
 
Registered: May 2008
Distribution: fedora 9
Posts: 13

Original Poster
Rep: Reputation: 0
thanks bigearsbilly, it worked as aspire suggested.

when i said "not as expected" i meant that i asumed system returns the unaltered exit status of the command. meanwhile i read the man page of system and got a better understanding of it.

your comment is very interesting. i never used gcc to study code. i'll keep that in mind.

Last edited by nemobluesix; 07-06-2009 at 07:01 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Log what exits in bash script. What causes exit code thats not 0? Trailsmoke Programming 2 09-25-2008 04:07 AM
In perl is there a way when i call exit function to execute a subroutine alix123 Programming 3 06-01-2007 06:38 AM
Exit Code: 7 dbzw Suse/Novell 0 12-21-2004 02:06 PM
pppd exit code 16 ksgill Linux - Newbie 8 07-12-2003 11:20 PM


All times are GMT -5. The time now is 02:27 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration