For anyone interested in the answer to this problem, here's what I found 
out.

I ran the script through truss and looked at what was happening. It 
seems that when trying to use fopen("php://stdin", "r") or 
fopen("/dev/fd/0", "r"), that as it's reading from the file with 
fgets(), it does an lseek (or llseek) _to the current location_ after 
each read. It seems a bit strange that it would try to lseek to the 
current location, especially since I didn't open it "r+" or "w+", but 
anyway ... Since php://stdin and /dev/fd/0 are _interactive_, the man 
page in Solaris says that it uses non-buffered I/O -- i.e., you can't 
lseek/fseek it; it will return ESPIPE. However, calling popen("cat 
/dev/fd/0", "r") _does_ use buffered I/O, so it is possible to 
lseek/fseek on this file descriptor that is opened. I don't know what 
other OS'es do, but that's Solaris' way of handling piped data.

Would it be possible for someone to look into or offer an explanation 
for why the code for fgets() does an lseek() after each read?

Thanks.

-bsh

Billy S Halsey wrote:

> Hi all,
>
> I still have been completely unable to get this to work like it 
> should. I have a simple script:
>
> #!/usr/local/bin/php -q
> <?php
>    $fp = fopen("php://stdin", "r");
>    while (($buf = fgets($fp, 512)) != false) {
>        $input .= $buf;
>    }
>    echo "$input";
> ?>
>
> So if I call this echo.php, then if I try something like
>
>    cat /etc/hosts | ./echo.php
>
> It prints a # (first line of /etc/hosts) followed by two blank lines. 
> Then it quits. /etc/hosts does contain real data. This occurs with ANY 
> file that I try to cat.
>
> If you know what the problem might be, I would LOVE to hear your 
> suggestions. I didn't get any replies the first time I asked, so I'm 
> hoping that someone has something to contribute.
>
> I'm using PHP 4.1.1 on Solaris 8.
>
> Thanks in advance,
>
> -bsh
>
> Billy S Halsey wrote:
>
>> Hi all,
>>
>> I've been trying for the last three hours to do something like this:
>>
>>    #!/usr/local/bin/php -q
>>    <?php
>>        $fp = fopen("php://stdin", "r");
>>        while (!feof($fp)) {
>>            $line = fgets($fp, 4096);
>>            print $line;
>>        }
>>        fclose($fp);
>>    ?>
>>
>> And then calling it with something like:
>>
>>    cat foo.txt | ./echofile.php
>>
>> The problem is, it will print the first line of foo.txt, and then 
>> exit. No matter what I do, I can't get it to read the next line. I've 
>> even added a test after the print command to see if it's at EOF, and 
>> it's not. I've also changed it to do this:
>>
>>    while (($buf = fgets($fp, 4096)) != FALSE) {
>>        print $buf;
>>    }
>>
>> Still won't read more than one line of the file. The only thing I've 
>> been able to do that seems to work is this:
>>
>>    $fp = fopen("/dev/fd/0", "r");
>>    while (!feof($fp)) ......
>>
>> [The code snippets I've quoted above are just rough sketches ... I 
>> really am checking the return value from fopen() to make sure I open 
>> the file, etc.]
>>
>> I'm using PHP 4.1.1 on Solaris 8. Can somebody PLEASE tell me what 
>> the problem is? What stupid mistake am I making?
>>
>> Thanks.
>>
>> -bsh :-)
>>
>

-- 

=======================================================================
Billy S Halsey                              Software Problem Resolution
Phone x55403/(858) 526-9403                        ESP Solaris Software
Email [EMAIL PROTECTED]                        Sun Microsystems, Inc
                           -NO DAY BUT TODAY-
=======================================================================




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to