Thread: A simple code snippet of WIFSIGNALED and WTERMSIG

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A simple code snippet of WIFSIGNALED and WTERMSIG

    Here is the code:
    Code:
    int wrapSystem(const std::string& cmd){
    
     	int ret = system(cmd.c_str());
     	if (WIFSIGNALED(ret) &&
    	(WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))
    		return -1;
     	else
    		return ret;
    }
    Why should we add the line

    if (WIFSIGNALED(ret) &&
    (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT))

    What does that do?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well do you want to know whether the program you ran exited normally, or was killed off by the user ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The WIFSIGNALED() returns true if the application stopped due to a "signal".

    If it did, the next part checks if the signal itself was SIGINT or SIGQUIT, in which case -1 is returned. SIGINT is "CTRL-C" and SIGQUIT is some other "user-input to abort", often CTRL-\ on various systems.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    often CTRL-\ on various systems.
    I learn something new every day.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    I learn something new every day.
    I didn't know that either until I saw it on:
    http://en.wikipedia.org/wiki/SIGQUIT

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed