how to check exit status of a command within a C++ application

how to check exit status of a command within a C++ application

Post by pate » Thu, 14 Jan 1999 04:00:00



hi,

would like to know what should i do if i want to know the exit
status of a command that was executed by a C++ application using
system or fork/execlp ! the system and execlp returns the error
code while executing the command but not the exit/status code returned/set
by the command itself.

thanks in advance

chirag

 
 
 

how to check exit status of a command within a C++ application

Post by Madhav Jagannatha » Thu, 14 Jan 1999 04:00:00


....
#include <sys/wait.h>
....
int    rc, status;
status = system ( "your_command_here" );
rc = WEXITSTATUS ( status ); // rc contains the exit status
....

When you say you want the exit status of a process you initiate with execlp (
), I assume that you perform a species of wait ( ) on it.

....
int     rc, status;
....
wait ( &status );
rc = WEXITSTATUS ( status ); // rc contains the exit status
....

system ( ) returns the ``process'' status of the child process that it
launches. wait ( &status ) places the ``process'' status of the process being
waited on in ``status''. The process status is an integer that contains the
following tidbits of information ORed together:
a) Did the process perform an exit ( ) or was it terminated by a signal
b) If terminated by a signal, the terminating signal number
c) If it performed an exit ( ), the exit status (i.e. argument to exit ( )).
d) Whether the process dumped core (this is not stipulated by POSIX
guidelines, however)
In order to access these bits of information separately, you have to use the
macros defined in <sys/wait.h>. The macro to extract the exit status from the
process status is WEXITSTATUS ( ).


> hi,

> would like to know what should i do if i want to know the exit
> status of a command that was executed by a C++ application using
> system or fork/execlp ! the system and execlp returns the error
> code while executing the command but not the exit/status code returned/set
> by the command itself.

> thanks in advance

> chirag


 
 
 

1. checking the status after executing a command from a C++ application using system

hi,

might have been successful posting this earlier - but i do not
see this posting coming up in the list of messages.  hence, this
posting.
i wantes to see what calls or mechanism should i use to get the
exit status of the last command executed by my C++ application
using the system of the fork/exec combo.  if iwas at the command
prompt - i would have used the '$?' to get the status.  but don't know
what is required to get this from an application.

thanks in advance.
chirag

2. Desktop PCMCIA freezes

3. Linux with Intel EtherExpress 16 ???

4. ASUS P2L97-DS

5. alt.os.linux ?

6. Checking status of command within ftp.

7. Test exit status of command prior to last command

8. How do I check the exit code of an execurtable run from within a script.

9. Automatic Checking for Exit Status in ksh

10. Diagnostics: Checking exit status