Re: why is this not a bug? (popen, pclose)
| From: | Bill Rausch | Date: | Thu, 13 Jul 2000 20:00:54 +0000 |
| Subject: | Re: why is this not a bug? (popen, pclose) | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-6534@lists.php.net to get a copy of this message | ||
Michael Teter <michael_teter@yahoo.com> said:
> > ret = pclose(sendmail);
> > if (ret == -1) {
> > return 0;
> > } else {
> > return 1;
> > }
...
>I don't know what's going on in Rasmus's example.
>
>But I do know that Unix return code 0 is taken to mean
>success. Non-zero return is failure. I presume the
>reason for this system of returning success as 0 is so
>you can return an error code if it fails.
Yes, UNIX programs exit with zero on success and an error code on failure.
C functions internally however use zero as FALSE and non-zero (typically 1
but any non-zero will do) as TRUE. Don't confuse what goes on inside the
language with the user interface of the program.
>So, it seems that popen is returning success of the
>activity of opening the pipe and forking. It's not
>waiting for the command to execute. It is merely
>handing you the file pointer to the pipe it opened.
Yes, popen returns a valid file pointer (like fopen does) if it was able to
launch the program you requested. This file pointer is how you send input
to the other program (if you used a "w") or how you read its output (if you
used an "r"). If popen was unable to execute your command, it returns a
NULL just like fopen does when it fails.
In C, when you are done with the program you've launched, you then do a
pclose on it and that returns the UNIX exit code of the program you ran.
In PHP the pclose is slightly different in what it returns but I believe
that Rasmus's example is mostly correct. He's assuming that the pclose
return value will be -1 if there was a problem or it will be zero. From
the online documentation there appear to be other possible values. A
better approach might be to reverse his test to something like:
ret = pclose(sendmail);
if (ret == 0) {
return 1; // pclose succeeded and so did the command
} else {
return 0;
}
---
Bill Rausch, Software Development, Unix, Mac, Windows
Numerical Applications, Inc. 509-943-0861 bill@numerical.com