Help: Returning a return code to a program

Help: Returning a return code to a program

Post by Heiner Stev » Sat, 25 Nov 1995 04:00:00



[...]

 > When displaying the value of ret, I get 512.  The value being
 > returned to programA seems to be bit-shifted left by 1 byte.

That's right.

 > Can anyone explain this to me.

Your manual page can:

    man 3 system

Heiner
--
     -------------------------------------------------------------

   / BinTec Computersysteme GmbH / mine, not BinTec's --       /
  / Willstaetter Str. 30 ------- ...even if they should be ;-)/

------------------------------------------------------------

 
 
 

Help: Returning a return code to a program

Post by Mark-Jason Domin » Mon, 27 Nov 1995 04:00:00




>    > When displaying the value of ret, I get 512.  The value being
>    > returned to programA seems to be bit-shifted left by 1 byte.
>   That's right.
>    > Can anyone explain this to me.

>   Your manual page can:

I agree with you in spirit, but *my* system(3) man page doesn't
explain it at all.  To look it up, you have to know to look under
`wait(2)'.  A beginner isn't going to make this connection.  In fact,
on my system, even `wait(2)' doesn't tell you what's going on; all it
tells you to do is to use the W...() macros to get the status.  This
is pretty unhelpful.

Gary: Yes, it is left-shifted by 8 bits, as you've noticed.  This is
because the low 8 bits contain information about whether the process
was stopped by a signal, and what signal it was, and whether it left a
core file behind.  What you should so is first check if the result is
127, because `system' uses that to indicate that it couldn't run the
command at all; then if it's not 127, rightshift it 8 places to get
the real return status.  

--

  The night is pleasing to us because, like memory, it erases idle details.


 
 
 

1. Help: Returning a return code to a program

: I am using HP-UX version 9.0.4.
: I have 2 programs. programA and programB

: programA

: main(int argc, char *argv[])
: {
:    int ret=0;
:    ret=(system("b"));
:    printf("ret[%d]\n",ret);
:    exit(0);
: }

: programB
: int main(int argc, char *argv[])
: {
:    exit(2);
: }

: programA calls programB and puts the return value into ret.

: When displaying the value of ret, I get 512.
: The value being returned to programA seems to be bit-shifted left by 1 byte.

: Can anyone explain this to me.

man system says:

SYSTEM(3)              C LIBRARY FUNCTIONS              SYSTEM(3)

NAME
     system - issue a shell command

SYNOPSIS
     system(string)
     char *string;

DESCRIPTION
     system() gives the string to sh(1) as input, just as if  the
     string  had  been  typed  as a command from a terminal.  The
     current process performs a wait(2V) system call,  and  waits
     until  the shell terminates.  system() then returns the exit
     status returned by wait(2V).  Unless the  shell  was  inter-
     rupted  by  a signal, its termination status is contained in
     the 8 bits higher up from the low-order 8 bits of the  value
         ^^^^^^^^^^^^^^^^
     returned by wait().

SEE ALSO
     sh(1), execve(2V), wait(2V), popen(3S)

DIAGNOSTICS
     Exit status 127 (may be displayed as "32512") indicates  the
     shell could not be executed.

Sun Release 4.1   Last change: 22 January 1988                  1

Doesn't HP/UX have man pages???

--
  Tad McClellan,      Logistics Specialist (IETMs and SGML guy)

  Interesting trivia: If you took all the sand in North Africa and spread
     it out... it would cover the Sahara desert.

2. USB printer for Linux?

3. help: preserving error return codes thru function codes

4. MRTG to monitor particular PORTS

5. How to get the return code of a program

6. new - open-source XBasic compiler with IDE and GuiDesigner

7. Capturing return code from a C program

8. Zmodem xfers

9. Program return codes

10. How to get the return code of the program which is executed by calling popen() finction ?

11. Accessing program return codes in sh ?

12. Return character in program source code.

13. Returning an exit code with the PID as an exit code