A simple code snippet of WIFSIGNALED and WTERMSIG

This is a discussion on A simple code snippet of WIFSIGNALED and WTERMSIG within the C++ Programming forums, part of the General Programming Boards category; Here is the code: Code: int wrapSystem(const std::string& cmd){ int ret = system(cmd.c_str()); if (WIFSIGNALED(ret) && (WTERMSIG(ret) == SIGINT || ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21