delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2007/04/27/12:41:10

X-Spam-Check-By: sourceware.org
To: cygwin AT cygwin DOT com
From: Eric Blake <ebb9 AT byu DOT net>
Subject: ftello/fgetpos bug
Date: Fri, 27 Apr 2007 16:40:33 +0000 (UTC)
Lines: 48
Message-ID: <loom.20070427T183056-468@post.gmane.org>
Mime-Version: 1.0
User-Agent: Loom/3.14 (http://gmane.org/)
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019