bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

closein, freadahead and ungetc


From: Bruno Haible
Subject: closein, freadahead and ungetc
Date: Fri, 29 Feb 2008 11:49:55 +0100
User-agent: KMail/1.5.4

Hi Eric,

closein.c assumes that if freadahead() == 0, the stream has no buffered
contents, i.e. an fseek will not have to reposition the underlying file
descriptor with lseek.

This is not the case. Here is a test program:
============================ foo.c ========================================
#include <config.h>
#include <stdio.h>
#include "freadahead.h"
int main ()
{
  int c;

  c = getc (stdin);
  printf ("first: '%c', %d\n", c, freadahead (stdin));

  c = getc (stdin);
  printf ("second: '%c', %d\n", c, freadahead (stdin));

  ungetc (c, stdin);
  printf ("after normal ungetc: %d\n", freadahead (stdin));

  c = getc (stdin);
  printf ("after reading normal ungetc: %d\n", freadahead (stdin));

  ungetc ('@', stdin);
  printf ("after random ungetc: %d\n", freadahead (stdin));

  c = getc (stdin);
  printf ("after reading random ungetc: %d\n", freadahead (stdin));

  c = getc (stdin);
  printf ("after reading one more byte: %d\n", freadahead (stdin));

  return 0;
}
===========================================================================

If you run it on Linux:
$ gcc foo.c freadahead.o -Wall -I.. -I.
$ ./a.out < foo.c
first: '#', 686
second: 'i', 685
after normal ungetc: 686
after reading normal ungetc: 685
after random ungetc: 1
after reading random ungetc: 0
after reading one more byte: 684

With strace you can see that there is only a single read() from the file
descriptor. I.e. at the moment when freadahead() returns 0, it is only
the ungetc-backup-buffer which has been emptied; the main buffer still
contains 685 bytes.

Where to fix this? In closein.c? Or in freadahead.c?

Bruno





reply via email to

[Prev in Thread] Current Thread [Next in Thread]