揮発性のメモ2

http://d.hatena.ne.jp/iww/

system関数の戻り値

system()の戻り値は、所詮exit()の引数なのでstrerror()とかにつっこんでもぜんっぜん役に立たない。
でも、シグナルのときはちゃんとシグナル番号が入るのでstrsignal()が役に立つ。

#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
    int result;
    printf("COMMAND:%s\n",argv[1]);
    printf("------------------------------\n");
    result = system( argv[1] );
    
    printf("------------------------------\n");
    printf("RESULT:%d\n",result);
    if( WIFEXITED(result) ){
        printf("ERROR:%d\n", WEXITSTATUS(result));
    }
    if( WIFSIGNALED(result) ){
        int sig = WTERMSIG(result);
        printf("SIGNAL:%d (%s)\n", sig, strsignal(sig));
    }

    return 0;
}
$ ./jikken "sleep 30"
COMMAND:sleep 30
------------------------------
^C------------------------------
RESULT:2
SIGNAL:2 (Interrupt)