How to get system() function executed cmd return value ?

Hi,

How I can get system function executed command return value ? I want to know mv command success or not ?

#include <stdio.h>
main()
{
int ret;

ret = system( "mv x.dat y.dat" );
printf( "system ret:[%d]\n", ret );
}

The return value of the command is in the upper 8 bits of the return value. So you bitwise shift the return value by 8.

#include<stdio.h>

int main() {
        int ret;
        ret=system("mv x.dat y.dat");
        fprintf(stdout,"system ret:[%d]\n",ret>>8);
}

Normally, the return status is decoded by the "wait" macros - WIFEXITED etc.
A lot of them work in pairs - if WIFEXITED returns a non-zero, then you use WIFEXITSTATUS to determine the exit status.

The problem with bit shifting is that you can't always determine if the child exited due to a signal like SIGSEGV-- because a signal can look like a weird exit status. And your core dump can go unnoticed.

man waitpid

has a nice explanation of exit status - and usually #include <stdlib.h>
has those macros in support support of the system() call -- which stdlib declares.

Thanks Lot