php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76321 ftell(STDIN) returns 0 when no stdin is supplied
Submitted: 2018-05-09 21:50 UTC Modified: 2018-05-11 23:16 UTC
From: gadelat at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 7.2.5 OS: Windows 10
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: gadelat at gmail dot com
New email:
PHP Version: OS:

 

 [2018-05-09 21:50 UTC] gadelat at gmail dot com
Description:
------------
I'm trying to make my application work in Windows. Important part of that is to detect if STDIN was provided. I can't figure out how to do that in Windows.

On Linux OS, following returns bool(false), in Windows int(0)

var_dump(ftell(STDIN));

Other approaches have same problem, or indefinitely block application if no stdin was provided. 

Is there no way to reliably detect STDIN in Windows?

Test script:
---------------
var_dump(ftell(STDIN));

Expected result:
----------------
FALSE

Actual result:
--------------
0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-10 16:32 UTC] requinix@php.net
What are you doing to not "provide" stdin?
 [2018-05-10 21:34 UTC] gadelat at gmail dot com
Running the script via `php test.php` from cmd.exe, powershell or git bash. Tried both thread safe and non-thread safe php 7.2.5 versions. I'm doing it via Virtualbox.
 [2018-05-10 21:56 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-05-10 21:56 UTC] requinix@php.net
That won't prevent there from being a stdin. You aren't using it, but it's still there.
As for Linux, stdin is a FIFO (glorified buffer) and you can't ftell on a FIFO.

Why is having stdin a problem?
 [2018-05-10 22:04 UTC] gadelat at gmail dot com
My application revolves around processing STDIN. As a first step I validate input. I check if user supplied stdin and exit if didn't.

So is there no way to check for STDIN presence on Windows without hanging whole application?
 [2018-05-10 22:30 UTC] requinix@php.net
If your application needs to process stdin and there isn't anything already in the buffer for it, it should wait. It sits and reads and/or waits until EOF. That's how filters normally operate.

I'm not the most familiar with the subject but none of the tactics I just tried work. You might be out of luck.
 [2018-05-11 22:15 UTC] gadelat at gmail dot com
This snippet works as expected on Windows 7. So this is regression and is IMHO worth investigation what's going on in windows 10 so this keep working across windows versions
 [2018-05-11 23:16 UTC] requinix@php.net
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/ftell-ftelli64
> On devices incapable of seeking (such as terminals and printers), or when stream does not refer to an open file, the
> return value [of ftell] is undefined.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Dec 04 03:01:23 2020 UTC