LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2007, 02:53 PM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
PHP: fgets works like a readline in 5.2.x but not in 5.1.2


If I use fgets on STDIN in php in version 5.2.3 (ubuntu gutsy... but so did in edgy and feisty), it waits for a line of input before proceding, but in version 5.1.2 it just goes ahead without stopping for input (not even a character). How can I avoid that?

Follow this example:
Code:
<?
if (! defined(STDIN)) {
        define("STDIN", fopen("php://stdin", "r"));
}
while (!feof(STDIN)) {
        $line = trim(fgets(STDIN));
        $fields = explode(' ', $line);
        $username = rawurldecode($fields[0]); //1738
        $password = rawurldecode($fields[1]); //1738
        if ($username == 'hello' 
            and $password == 'world') {
                fwrite(STDOUT, "OK\n");
        } else if ($username == 'fo'
            and $password == 'bar') {
                fwrite(STDOUT, "OK\n");
        } else {
                // failed miserably
                fwrite(STDOUT, "ERR\n");
        }
}
?>
How can I force it to wait for a line of input? Can I just use readline()?
 
Old 07-20-2007, 02:56 PM   #2
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
I just tried with readline and the function is not defined... I guess there must be a process to make it available. :-S As I said before... is there a wait to force fgets to wait for a whole line of input?
 
Old 07-20-2007, 03:20 PM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
Solved it!

The problem is the define call. I have the fopen() right in the define, and it fails that way. If you do the fopen and save the result in a temporary variable, it works.

Try with this:

Code:
if (! defined(STDIN)) {
    $temp = fopen("php://stdin", "r");
    define("STDIN", $temp);
}
It worked with version 5.1.2 that way.

It worked with 5.2.3 the original way... I guess it's a bug in 5.1.2
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fgets on socket nc3b Programming 2 05-14-2007 07:54 AM
php fgets really slow pasholy2001 Programming 0 04-22-2005 02:17 AM
fgets() problems in C AMMullan Programming 4 03-12-2004 04:39 AM
fgets h/w Programming 23 12-21-2003 06:20 PM
fgets vs gets cxel91a Programming 2 12-01-2003 12:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration