Back to index

glibc  2.9
bug-ungetc3.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_one_test (int mode)
00029 {
00030   FILE *f;
00031 
00032   f = fopen (temp_file, "r");
00033   if (f == NULL)
00034     {
00035       printf ("could not open temporary file: %m\n");
00036       return 1;
00037     }
00038 
00039   if (mode == 1 && ftell (f) != 0)
00040     {
00041       printf ("first ftell returned wrong position %ld\n", ftell (f));
00042       return 1;
00043     }
00044 
00045   if (fgetc (f) != '1' || fgetc (f) != '2')
00046     {
00047       puts ("fgetc failed");
00048       return 1;
00049     }
00050 
00051   if (mode == 2 && ftell (f) != 2)
00052     {
00053       printf ("second ftell returned wrong position %ld\n", ftell (f));
00054       return 1;
00055     }
00056 
00057   if (ungetc ('6', f) != '6')
00058     {
00059       puts ("ungetc failed");
00060       return 1;
00061     }
00062 
00063   if (ftell (f) != 1)
00064     {
00065       printf ("third ftell returned wrong position %ld\n", ftell (f));
00066       return 1;
00067     }
00068 
00069   if (fgetc (f) != '6')
00070     {
00071       puts ("fgetc failed");
00072       return 1;
00073     }
00074 
00075   if (ftell (f) != 2)
00076     {
00077       printf ("fourth ftell returned wrong position %ld\n", ftell (f));
00078       return 1;
00079     }
00080 
00081   fclose (f);
00082 
00083   return 0;
00084 }
00085 
00086 static int
00087 do_test (void)
00088 {
00089   int mode;
00090   for (mode = 0; mode <= 2; mode++)
00091     if (do_one_test (mode))
00092       return 1;
00093   return 0;
00094 }