WEXITSTATUS returns 1 (in parent) , although child return 0
<p>I am using the following code in order to run some commands using parent/child:</p>

<pre><code>int nStatus = 0;
int nRet = 0;
pid_t pid = -1;

char *envp[] =
    {
        "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/tmp",
        0
    };



if(pnChildExitStatus == NULL || pcBuf == NULL)
{
    nRet = -EINVAL;
    goto returnHandler;
}
*pnChildExitStatus = 0;
pid = fork();
switch(pid)
{
    case -1:
        nRet = -errno;                           //can't fork
        break;
    case 0: //child
        nRet = execl("/bin/sh","sh","-c", pcBuf, envp,NULL);
        if (-1 == nRet) {
            nRet = -errno;
            goto returnHandler;
        }
        break;
    default: //parent
            /*After this call 'nStatus' is an encoded exit value. WIF macros will extract how it exited*/
            if( waitpid(pid, &amp;nStatus, 0) &lt; 0 ) 
            {
                nRet = -errno;
                *pnChildExitStatus = errno;
            }
            if(WIFEXITED(nStatus))
            {
                nRet = EXIT_SUCCESS;
                *pnChildExitStatus = WEXITSTATUS(nStatus);
            }
            goto returnHandler;
        break;
}
returnHandler:
    return nRet;
}
</code></pre>

<p>My problem is as follows:</p>

<p>In some cases, Parent receives WEXITSTATUS(nStatus) != 0, although my child process always return 0 (I verified it with debug prints).</p>

<p>Can someone offer any idea on how is this possible?</p>

<p>Thank you all in advance.</p>