php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31269 fgets/fread functions not working properly when used with STDIN
Submitted: 2004-12-23 15:07 UTC Modified: 2004-12-23 21:47 UTC
From: npeelman at cfl dot rr dot com Assigned:
Status: Not a bug Package: CGI/CLI related
PHP Version: Irrelevant OS: Windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: npeelman at cfl dot rr dot com
New email:
PHP Version: OS:

 

 [2004-12-23 15:07 UTC] npeelman at cfl dot rr dot com
Description:
------------
 According to the manual/docs, fgets/fread or any of these functions that specify a 'length in bytes' to be read will return after 'length bytes' have been read, newline, or EOF.
This is not true when reading from STDIN; newline is the only reason that these functions will return. 

Reproduce code:
---------------
<?php
$handle = fopen("php://stdin", "r");
if($handle)
{
   $buffer = fgets($handle, 11);
   echo $buffer;
}
fclose($handle);
?>

or for ST??? constants:

<?php
$buffer = fgets(STDIN, 11);
echo $buffer;
?>



Expected result:
----------------
  In either case I would expect 10 (length - 1) characters to be read from STDIN at which point fgets/fread would return and populate $buffer.

If I type: Won't work darnit!

I expect the script to terminate (fgets returns) after I type: Won't work (which is 10 characters) and to see: Won't work output to the screen.


Actual result:
--------------
  What happens is <RETURN> must be pressed before the functions return. The functions do return length-1 bytes at this point (but more than length characters can be typed).

If I type: Won't work darnit! then I see (as I type) onscreen: Won't work darnit! and now must press <RETURN>.
Won't work is now output to the screen (10 characters).

  The problem is that it is not consistant with the documentation (which reads these functions will return when length-1 bytes have been read). This really makes it very difficult to write CLI based scripts. I know there is ncurses but i'm sure that many of us are not into compiling on Win32. Also this relates to some of the stream functions that seem to only work on unix but are not documented as such. Many hours have been wasted searching for answers. I know this is similar bug report but I feel at least the documentation should be fixed if not PHP and maybe an explanation as to why it can't/won't be fixed would be nice.

Thank you

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-23 19:11 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

In order to recieve user input <enter> must pressed. If you need input as it is being typed consider looking into the ncurses extension.
 [2004-12-23 21:47 UTC] npeelman at cfl dot rr dot com
Thank you for your quick reply but was my report even read? If it's not a bug then what is it? Aren't the functions suppossed to act as described? Why can't this be fixed? I would hope that in some future version this is fixed as there is no workaround for Windows users.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 29 10:01:30 2024 UTC