freopen/fread/popen bug

Corinna Vinschen corinna-cygwin@cygwin.com
Fri Feb 27 09:04:00 GMT 2015


On Feb 26 22:05, Ken Brown wrote:
> I'm not sure exactly where the bug is, but here's what happens (STC at the end):
> 
> 1. I use freopen to open a file "foo" and associate it with stdin.
> 
> 2. I use fread to read a byte from foo.
> 
> 3. I call popen, expecting the child process to have foo as its stdin, with
> the file-position indicator pointing to the second byte.  But instead the
> child sees an empty stdin.
> 
> If I omit step 2, the child process does indeed have foo as its stdin.  Are
> my expectations wrong, or is this a bug?

Wrong expectations.  Keep in mind that the default read mode using
stdio functions is buffered.  So your fread fills the buffer in f.
The buffer is typically something like 1K or 4K.  If the file is
shorter than that, the file pointer will be set to EOF when calling
popen.  Try this before calling fread:

  setvbuf(f, NULL, _IONBF, 0);


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://cygwin.com/pipermail/cygwin/attachments/20150227/7355b1c1/attachment.sig>


More information about the Cygwin mailing list