ftello/fgetpos bug

Eric Blake ebb9@byu.net
Fri Apr 27 16:40:00 GMT 2007


I'm not sure if this is a newlib bug, or a cygwin bug in how the 32- vs. 64-bit 
versions of stdio functions are initialized for std{in,out,err}.  For some 
reason, ftello and fgetpos fail to work on the default stdin, but have no 
problem once freopen or fopen has been in the loop.

I'm still trying to track it down, but thought I'd bring attention to the 
problem.

$ echo hello > blah
$ cat foo.c
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
   fpos_t pos;
   FILE *f;
   if (argc > 2)
      f = freopen(argv[1], "r", stdin);
   else if (argc > 1)
      f = fopen(argv[1], "r");
   else
      f = stdin;
   getc(f);
   printf("ftell:%d ", (int) ftell(f));
   printf("ftello:%d ", (int) ftello(f));
   fgetpos(f, &pos);
   printf("fgetpos:%d ", (int) pos);
   printf("lseek:%d ", (int) lseek(fileno(f), 0, SEEK_CUR));
   return 0;
}

$ echo hi | ./foo
ftell:-1 ftello:-1 fgetpos:-1 lseek:-1 
$ # Good - a pipe is not seekable, so there is no position
$ ./foo blah a
ftell:1 ftello:1 fgetpos:1 lseek:1 
$ # Good - freopen makes stdin track seekable position
$ ./foo blah
ftell:1 ftello:1 fgetpos:1 lseek:1 
$ # Good - fopen makes f track seekable position
$ ./foo < blah
ftell:1 ftello:-1 fgetpos:-1 lseek:1 
$ # Ouch - ftell and ftello disagree!

-- 
Eric Blake



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list