Back to index

glibc  2.9
bug-ungetc2.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <sys/types.h>
00003 
00004 
00005 static int
00006 check (FILE *fp, off_t o)
00007 {
00008   int result = 0;
00009   if (feof (fp))
00010     {
00011       puts ("feof !");
00012       result = 1;
00013     }
00014   if (ferror (fp))
00015     {
00016       puts ("ferror !");
00017       result = 1;
00018     }
00019   if (ftello (fp) != o)
00020     {
00021       printf ("position = %lu, not %lu\n", (unsigned long int) ftello (fp),
00022              (unsigned long int) o);
00023       result = 1;
00024     }
00025   return result;
00026 }
00027 
00028 
00029 static int
00030 do_test (void)
00031 {
00032   FILE *fp = tmpfile ();
00033   if (fp == NULL)
00034     {
00035       puts ("tmpfile failed");
00036       return 1;
00037     }
00038   if (check (fp, 0) != 0)
00039     return 1;
00040 
00041   puts ("going to write");
00042   if (fputs ("hello", fp) == EOF)
00043     {
00044       puts ("fputs failed");
00045       return 1;
00046     }
00047   if (check (fp, 5) != 0)
00048     return 1;
00049 
00050   puts ("going to rewind");
00051   rewind (fp);
00052   if (check (fp, 0) != 0)
00053     return 1;
00054 
00055   puts ("going to read char");
00056   int c = fgetc (fp);
00057   if (c != 'h')
00058     {
00059       printf ("read %c, not %c\n", c, 'h');
00060       return 1;
00061     }
00062   if (check (fp, 1) != 0)
00063     return 1;
00064 
00065   puts ("going to put back");
00066   if (ungetc (' ', fp) == EOF)
00067     {
00068       puts ("ungetc failed");
00069       return 1;
00070     }
00071   if (check (fp, 0) != 0)
00072     return 1;
00073 
00074   puts ("going to write again");
00075   if (fputs ("world", fp) == EOF)
00076     {
00077       puts ("2nd fputs failed");
00078       return 1;
00079     }
00080   if (check (fp, 5) != 0)
00081     return 1;
00082 
00083   puts ("going to rewind again");
00084   rewind (fp);
00085   if (check (fp, 0) != 0)
00086     return 1;
00087 
00088   if (fclose (fp) != 0)
00089     {
00090       puts ("fclose failed");
00091       return 1;
00092     }
00093 
00094   return 0;
00095 }
00096 
00097 #define TEST_FUNCTION do_test ()
00098 #include "../test-skeleton.c"