Return value of system

V

vashwath

Hi all,
To test the return value of system I wrote 2 programs which are shown
below.

Program 1:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int ret;

ret = system("./TEST");
printf("Return value = %d\n",ret);

return ret;
}

Program 2:

int main()
{
return 50;
}

The executable name of program2 is TEST. I thought system in Program1
will return whatever program2 will return, but It didn't. What
exactly system will return?What should I do if I want to get the exact
return value of program2?

Thanks
 
J

Jack Klein

Hi all,
To test the return value of system I wrote 2 programs which are shown
below.

Program 1:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int ret;

ret = system("./TEST");
printf("Return value = %d\n",ret);

return ret;
}

Program 2:

int main()
{
return 50;
}

The executable name of program2 is TEST. I thought system in Program1
will return whatever program2 will return, but It didn't. What
exactly system will return?What should I do if I want to get the exact
return value of program2?

Thanks

Doesn't your compiler provide documentation, online help, or man
pages?

Did you try typing

"system()" function

into Google?

The system() function is almost completely implementation-defined. The
strings they pass to it, what they mean, how they are interpreted, and
what happens as a result, are all implementation-defined.

With one exception, that is. If you call system() with a null pointer
as an argument, the return value tells you whether or not there is a
command shell available. If system(NULL) returns 0, there is no
command shell that you can use. If system(NULL) returns a non-zero
value, there is a command shell.

Beyond that, since it is completely implementation-defined, you need
to consult the documentation for your implementation (compiler), which
is required to document what it will return when you call it.
 
V

vashwath

Is there any way to get the value returned from Program2 and print it
in program1?
 
A

ankisharma

change the line:
printf("Return value = %d\n",ret); => printf("Return value =
%d\n",ret%255);
 
L

Logan Shaw

Is there any way to get the value returned from Program2 and print it
in program1?

Can you answer a question for me first? Do you know if my car is
front-wheel drive or rear-wheel drive? I would really like to know,
thanks.

- Logan
 
V

vashwath

Can you answer a question for me first? Do you know if my car is
front-wheel drive or rear-wheel drive? I would really like to know,
Yes, I can ask get that information from you.
 
V

vashwath

Can you answer a question for me first? Do you know if my car is
front-wheel drive or rear-wheel drive? I would really like to know,
Yes, I can get that information from you.
 
K

Keith Thompson

change the line:
printf("Return value = %d\n",ret); => printf("Return value =
%d\n",ret%255);

Why would you want to do that?

Since I just read the article to which you're replying, I happen to
know that ret is the value returned by a call to system(). Please
read <http://cfaj.freeshell.org/google/> to learn how to make sure
everyone knows what you're talking about.

The value returned by system(), with one exception, is entirely
implementation-defined. There is no reason to think that ret%255 is
going to be meaningful; on any implementation I know about, it isn't.
(I think I know what you're referring to, but you've gotten it wrong.)

For information on what system() returns on a given implementation,
consult the documentation for that implementation.
 
R

Robert Gamble

change the line:
printf("Return value = %d\n",ret); => printf("Return value =
%d\n",ret%255);

First, please go to <http://cfaj.freeshell.org/google/> and follow the
instructions there the next time you post a followup via Google Groups.

Second, the return value of system() is implementation defined. I
don't know of any system where your suggestion would be useful and if
there was one it wouldn't be on-topic here.
<OT> There are some systems where the return value of the command is an
8-bit value stored in the high byte of a 16-bit integer where dividing
by 256 will yield the original return value, maybe this is what you
were thinking of.</OT>

Robert Gamble
 
F

Fred Kleinschmidt

Hi all,
To test the return value of system I wrote 2 programs which are shown
below.

Program 1:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int ret;

ret = system("./TEST");
printf("Return value = %d\n",ret);

return ret;
}

Program 2:

int main()
{
return 50;
}

The executable name of program2 is TEST. I thought system in Program1
will return whatever program2 will return, but It didn't. What
exactly system will return?What should I do if I want to get the exact
return value of program2?

Thanks

Program 1: (fscanf might not be the best thing to use to read the pipe, but
it demonstrates the principal.
Also needs more error checks:

#include <stdio.h>
int main(void) {
int n;
FILE *fp = popen( "./TEST;echo $?", "r" );
if ( fp ) {
fscanf( fp, "%d", &n );
printf( "%d\n", n );
pclose(fp);
}
}
 
M

Mark B

Fred Kleinschmidt said:
Program 1: (fscanf might not be the best thing to use to read the pipe,
but it demonstrates the principal.
Also needs more error checks:

#include <stdio.h>
int main(void) {
int n;
FILE *fp = popen( "./TEST;echo $?", "r" );
if ( fp ) {
fscanf( fp, "%d", &n );
printf( "%d\n", n );
pclose(fp);
}
}

Wow... that's the dumbest thing I've ever seen!
Please tell me you're kidding.

Surely a system which supports popen() also
provides the WIFEXITED() and WEXITSTATUS()
macros. Wouldn't it be easier to use those on
the value returned by system()?
Unfortunately, clc is not the appropriate forum
to discuss these POSIX features...
perhaps comp.unix.programmer would be a
more appropriate forum.

Mark
 
M

Mark McIntyre

On 17 Jan 2006 21:35:48 -0800, in comp.lang.c ,
Is there any way to get the value returned from Program2 and print it
in program1?

Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
 
A

ankisharma

Robert said:
First, please go to <http://cfaj.freeshell.org/google/> and follow the
instructions there the next time you post a followup via Google Groups. Thanks for the pointer
Second, the return value of system() is implementation defined.

agreed
I
I don't know of any system where your suggestion would be useful and if
there was one it wouldn't be on-topic here.

linux and solaris. As i don't have access to others
<OT> There are some systems where the return value of the command is an
8-bit value stored in the high byte of a 16-bit integer where dividing
by 256 will yield the original return value, maybe this is what you
were thinking of.</OT>

yup


anki
 
K

Keith Thompson

linux and solaris. As i don't have access to others

<OT>
ret%255 is not useful on Linux or Solaris. ret%256 might be, but
ret/256 is probably what you're looking for -- but only if the program
terminated normally. "man system" for details.
</OT>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top