This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/5994] New: fflush after ungetc on seekable input stream


Consider this program:

$ cat foo.c
#include <stdio.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
  /* Check that fflush after a non-backup ungetc() call discards the ungetc
     buffer.  */
  int c;

  c = fgetc (stdin);
  printf ("c = '%c'\n", c);

  c = fgetc (stdin);
  printf ("c = '%c'\n", c);

  c = ungetc (argc > 1 ? c : '@', stdin);
  printf ("ungetc result = '%c'\n", c);

  fflush (stdin);
  printf ("after fflush, location = %d\n",
	  (int) lseek (fileno (stdin), 0, SEEK_CUR));

  c = fgetc (stdin);
  printf ("c = '%c'\n", c);

  c = fgetc (stdin);
  printf ("c = '%c'\n", c);

  return 0;
}
$ ./foo < foo.c
c = '#'
c = 'i'
ungetc result = '@'
after fflush, location = 582
c = 'n'
c = 'c'
$ ./foo 1 < foo.c
c = '#'
c = 'i'
ungetc result = 'i'
after fflush, location = 1
c = 'i'
c = 'n'


I found it inconsistent that fflush on seekable input streams sets the position
of the underlying file descriptor to a different location based on whether the
byte that was pushed back was what was originally read.  And I'm not the only
one - the Austin Group admitted that Interp-002 is fishy, and that an aardvark
will be needed after POSIX 200x is released to require that fflush discards all
ungetc data and sets the offset of the file descriptor to the stream position
(ie. like the 2nd run, when pushing back the byte that was read, and NOT like
the behavior of the 1st run when pushing back random bytes).

https://www.opengroup.org/sophocles/show_mail.tpl?CALLER=index.tpl&source=L&listname=austin-group-l&id=11398

-- 
           Summary: fflush after ungetc on seekable input stream
           Product: glibc
           Version: 2.3.4
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: drepper at redhat dot com
        ReportedBy: ebb9 at byu dot net
                CC: glibc-bugs at sources dot redhat dot com


http://sourceware.org/bugzilla/show_bug.cgi?id=5994

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]