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