[Gambas-user] Pipe doesn't receive contents

Doriano Blengino doriano.blengino at ...1909...
Sat May 8 07:41:20 CEST 2010


Benoît Minisini ha scritto:
>>>> Trying the example code for pipes, I only get a waiting prompt, and
>>>> then it jumps out of the loop.
>>>>
>>>> I added two little statements to have some output:
>>>>
>>>> DIM hFile AS File
>>>> DIM sLine AS String
>>>>
>>>> hFile = PIPE "/tmp/FIFO1" FOR INPUT
>>>>
>>>> PRINT "Now let's start:"
>>>>
>>>> WHILE NOT Eof(hFile)
>>>>
>>>>    LINE INPUT #hFile, sLine
>>>>    PRINT sLine
>>>>
>>>> WEND
>>>>
>>>> PRINT "That's it."
>>>>
>>>> When I start this, it prints the start message and waits. It's in a
>>>> Konsole window within KDE. I open another terminal and type
>>>>
>>>> ls > /tmp/FIFO1
>>>>
>>>> Instead of giving the file list, it just prints the finish message and
>>>> quits.
>>>>
>>>> Did I do something wrong?
>>>>
>>>> Rolf
>>>>         
>>> I just tried, and it works with "ls -la > /tmp/FIFO1", but not with "ls >
>>> /tmp/FIFO1", as you described. Very strange...
>>>       
>> It is actually random: sometimes it works as expected, sometimes Eof(hFile)
>> returns True the first time it is called, and the pipe is not read. Very
>> strange yet!
>>     
>
> OK, I'm stupid. It's just silly to use Eof() with a pipe, because it just tell 
> you that the pipe is void, and can't know that some data may come in the 
> future. So if the program is too fast for the data to come, the first Eof() 
> returns TRUE.
>   
No, you are not stupid - but it is false that it is stupid to use EOF 
with pipes.
Just to show, I wrote a stupid short program in C (the word stupid used 
three times...):

    #include <stdio.h>

    int main(argc, argv) {
      FILE* f;
      int   c;

      f = fopen("/tmp/fifo", "r");
      do {
            c = fgetc(f);
            if (c == EOF) break;
            putchar(c);
      } while (1);
      fclose(f);
    }

This is the simplest (yet clear) program to test how to read and check 
for EOF in a pipe.
Compiled, ran ./a.out, and seen that it works perfectly, both with "ls 
 >..." and "ls -la >...".
After that, I tried even with "cat >/tmp/fifo", to be sure (I am far 
slower than /bin/ls). Unsurprisingly, it works; after all, it is a 
strong point of unix to have a uniform interface to files, sockets, 
pipes and fifos (and more), and a simple test using two cat(1) in two 
different terminal would be enough. But as I never looked at the cat.c 
source, I wanted to see by myself what you was saying.

I don't know what the problem is with gambas, so I can't help; but I do 
know that EOF with pipes and FIFOs work - I use both to control gdb in 
one of my freepascal projects; so, when I see someone on this list 
saying that it is a nonsense to check EOF in pipes, because all depends 
on speed, I must note that this is wrong. Of course, I may be wrong - if 
you do know something that I do not, please let me know: I am happy to 
learn more.

Regards,
Doriano




More information about the User mailing list