This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: freopen/fread/popen bug


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

Attachment: pgpmM4DIlQuEb.pgp
Description: PGP signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]