[derekn@foolabs.com: 1.3.3-2: fseek fails on multiples of 1024 (binary mode)]

Christopher Faylor cgf@redhat.com
Sun Oct 21 18:19:00 GMT 2001

AFAICT, this seems to be a bug in newlib's stdio.  If I replace the
fseeks in the code below with lseeks things seem to work better.


----- Forwarded message from "Derek B. Noonburg" <derekn@foolabs.com> -----

From: "Derek B. Noonburg" <derekn@foolabs.com>
To: cygwin@cygwin.com
Subject: 1.3.3-2: fseek fails on multiples of 1024 (binary mode)
Date: Sun, 21 Oct 2001 15:56:29 -0700 (PDT)

The fseek function appears to behave strangely when both the offset and
file size are multiples of 1024.  This is with binary mode files -- I'm
calling fopen with "rb", and my test file has no control characters (CR,
LF, ^Z) at all.

I'm attaching a test program that should read and print eight bytes at
offset 1024 from a file called "foo" (after doing another fread and
fseek first - these are necessary to trigger the problem).

I used a file "foo" with 1024 'x's, then "abc", then 1021 'y's.  So the
file is exactly 2048 bytes long, and the program should print
"abcyyyyy".  Except that it prints "xxxxxxxx", apparently reading from
offset 0 instead of 1024.

If I call rewind before calling fseek (this is commented out in the
attached code), it appears to work around the bug.

I'm using the latest version of cygwin as of this afternoon (DLL
1.3.3-2, gcc 2.95.3-5), on Windows 98.

- Derek

#include <stdio.h>

int main() {
  FILE *f;
  char buf[1025];

  if (!(f = fopen("foo", "rb"))) {
    fprintf(stderr, "Couldn't open 'foo'\n");

  fread(buf, 1, 1024, f);

  fseek(f, 0, SEEK_END);
  printf("size = %ld\n", ftell(f));

#if 0
  fseek(f, 1024, SEEK_SET);
  printf("pos = %ld\n", ftell(f));
  fread(buf, 1, 8, f);
  buf[8] ='\0';
  printf("data = '%s'\n", buf);


  return 0;

Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

----- End forwarded message -----

cgf@cygnus.com                        Red Hat, Inc.
http://sources.redhat.com/            http://www.redhat.com/

More information about the Newlib mailing list