NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

fgets() fails to read after fread()



Hi,

I am using the NetBsd library for my linux system. 
While testing my application I have seen one issue in NetBsd.
In my application when I read from "stdin" using fgets() after fread() of 
"stdin". 
I have seen fgets() return NULL, even I am writing on "stdout".

The application flow is as below.

1. The main() function read "stdin" using fread()
2. Then creat pipe and call fork()
3. In Parent process, connect write end of the pipe to "stdout" using pipe2()
4. In the child process, connect  read end of the pipe to "stdin". And read it 
using fgets() as below.

    while(fgets(buf, 10, stdin)) {
         :
    }

5. The parent process write "Hello" to "stdout".

Here my application expect fgets() should read the content written by parent 
(step:5).

But fgets() returns NULL at step:4 and child exit() without reading.

But with glibc my application able to read "Hello" using fgets().

It seems the call to fgets() after fread() having issue in NetBsd. 

Further I have investigated it in netbsd source and seen  the fread() flow as 
below.

{{{
   fread()
     --> __srefill(fp)
            --> fp->_flags |= __SEOF  ------> (A)
}}}

Here, at (A) EOF sticky flag is set by fread().


And then call to fgets() return NULL. Since the flow of fgets() is as below:

$ cat src/lib/libc/stdio/fgets.c
{{{     
 76         while (n != 0) {
 77                 /*
 78                  * If the buffer is empty, refill it.
 79                  */
 80                 if (fp->_r _r = 0;             /* largely a convenience for 
callers */
 87 
 88         /* SysV does not make this test; take it out for compatibility */
 89         if (fp->_flags & __SEOF)
 90                 return (EOF);
}}}

Here, at line 89, it checks for __SEOF (i.e. sticky EOF) flag, and if it set 
then it returns EOF at line 90. (In my scenario this flag was set by fread() at 
(A)). Hence it returns EOF.

Then I decided to comment out this EOF check in refill.c (line:89) and observed 
fgets() is able to read "Hello" without any issue. 

Can you give me some more inputs why this EOF sticky bit check present in 
NetBsd ? Is it required ?

I really appreciate the inputs on it.

Thank You,
Amol Pise 


Home | Main Index | Thread Index | Old Index