Back to index

glibc  2.9
bug-ungetc.c
Go to the documentation of this file.
00001 /* Test program for ungetc/ftell interaction bug.  */
00002 
00003 #include <stdio.h>
00004 
00005 static void do_prepare (void);
00006 #define PREPARE(argc, argv) do_prepare ()
00007 static int do_test (void);
00008 #define TEST_FUNCTION do_test ()
00009 #include <test-skeleton.c>
00010 
00011 static const char pattern[] = "12345";
00012 static char *temp_file;
00013 
00014 static void
00015 do_prepare (void)
00016 {
00017   int fd = create_temp_file ("bug-ungetc.", &temp_file);
00018   if (fd == -1)
00019     {
00020       printf ("cannot create temporary file: %m\n");
00021       exit (1);
00022     }
00023   write (fd, pattern, sizeof (pattern));
00024   close (fd);
00025 }
00026 
00027 static int
00028 do_test (void)
00029 {
00030   int i;
00031   FILE *f;
00032   char buf[10];
00033   long offset, diff;
00034   int result = 0;
00035 
00036   f = fopen (temp_file, "rw");
00037 
00038   rewind (f);
00039   for (i = 0; i < 3; i++)
00040     printf ("%c\n", getc (f));
00041   offset = ftell (f);
00042   printf ("offset = %ld\n", offset);
00043   if (ungetc ('4', f) != '4')
00044     {
00045       printf ("ungetc failed\n");
00046       abort ();
00047     }
00048   printf ("offset after ungetc = %ld\n", ftell (f));
00049 
00050   i = fread ((void *) buf, 4, (size_t) 1, f);
00051   printf ("read %d (%c), offset = %ld\n", i, buf[0], ftell (f));
00052   diff = ftell (f) - offset;
00053   if (diff != 3)
00054     {
00055       printf ("ftell did not update properly.  got %ld, expected 3\n", diff);
00056       result = 1;
00057     }
00058   fclose (f);
00059 
00060   return result;
00061 }