|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2019-09-05 15:05 UTC] nikic@php.net
-Status: Open
+Status: Closed
-Assigned To:
+Assigned To: nikic
[2019-09-05 15:05 UTC] nikic@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 07:00:02 2025 UTC |
Description: ------------ I am having an issue with PHP (cli) when using /dev/tty as the file. To run PHP code from STDIN, I am able to run the command as follows, $ php <?php echo "hello world\n"; ?> <CTRL-D> hello world $ However, when run through /dev/tty, the following happens, $ php /dev/tty <?php echo "hello world\n"; ?> <CTRL-D> ?php echo "hello world\n"; ?> $ NOTE: The first character < is simply lost and php processes the input as plain text. Problem:- The problem lies in sapi/cli/php_cli.c where interpreter directive is removed, /* #!php support */ c = fgetc(file_handle->handle.fp); if (c == '#' && (c = fgetc(file_handle->handle.fp)) == '!') { while (c != '\n' && c != '\r' && c != EOF) { c = fgetc(file_handle->handle.fp); /* skip to end of line */ } /* handle situations where line is terminated by \r\n */ if (c == '\r') { if (fgetc(file_handle->handle.fp) != '\n') { long pos = ftell(file_handle->handle.fp); fseek(file_handle->handle.fp, pos - 1, SEEK_SET); } } *lineno = 2; } else { rewind(file_handle->handle.fp); } rewind() on /dev/tty or any named pipe would fail, causing the first 1-2 bytes to be lost. Solution:- We should test if the file is seekable, if it is not, we can process it similar to a pipe. --- php_cli.c.orig 2015-10-15 14:39:33.875560414 +0530 +++ php_cli.c 2015-10-15 15:01:38.422751462 +0530 @@ -634,6 +634,10 @@ } file_handle->filename = script_file; + if (fseek(file_handle->handle.fp, 0L, SEEK_SET) < 0) { + return SUCCESS; + } + /* #!php support */ c = fgetc(file_handle->handle.fp); if (c == '#' && (c = fgetc(file_handle->handle.fp)) == '!') { Additional Note:- PHP Versions: 5.3.3 (cli) and 5.5.20 (cli) OS: Linux (CentOS 6.4 and Fedora 19)