(root)/
glibc-2.38/
libio/
bug-ungetc.c
       1  /* Test program for ungetc/ftell interaction bug.  */
       2  
       3  #include <stdio.h>
       4  
       5  #include <support/xunistd.h>
       6  
       7  static void do_prepare (void);
       8  #define PREPARE(argc, argv) do_prepare ()
       9  static int do_test (void);
      10  #define TEST_FUNCTION do_test ()
      11  #include <test-skeleton.c>
      12  
      13  static const char pattern[] = "12345";
      14  static char *temp_file;
      15  
      16  static void
      17  do_prepare (void)
      18  {
      19    int fd = create_temp_file ("bug-ungetc.", &temp_file);
      20    if (fd == -1)
      21      {
      22        printf ("cannot create temporary file: %m\n");
      23        exit (1);
      24      }
      25    xwrite (fd, pattern, sizeof (pattern));
      26    close (fd);
      27  }
      28  
      29  static int
      30  do_test (void)
      31  {
      32    int i;
      33    FILE *f;
      34    char buf[10];
      35    long offset, diff;
      36    int result = 0;
      37  
      38    f = fopen (temp_file, "rw");
      39  
      40    rewind (f);
      41    for (i = 0; i < 3; i++)
      42      printf ("%c\n", getc (f));
      43    offset = ftell (f);
      44    printf ("offset = %ld\n", offset);
      45    if (ungetc ('4', f) != '4')
      46      {
      47        printf ("ungetc failed\n");
      48        abort ();
      49      }
      50    printf ("offset after ungetc = %ld\n", ftell (f));
      51  
      52    i = fread ((void *) buf, 4, (size_t) 1, f);
      53    printf ("read %d (%c), offset = %ld\n", i, buf[0], ftell (f));
      54    diff = ftell (f) - offset;
      55    if (diff != 3)
      56      {
      57        printf ("ftell did not update properly.  got %ld, expected 3\n", diff);
      58        result = 1;
      59      }
      60    fclose (f);
      61  
      62    return result;
      63  }