Back to index

glibc  2.9
tst-ungetc.c
Go to the documentation of this file.
00001 /* Test for ungetc bugs.  */
00002 
00003 #include <stdio.h>
00004 #include <unistd.h>
00005 
00006 #undef assert
00007 #define assert(x) \
00008   if (!(x)) \
00009     { \
00010       fputs ("test failed: " #x "\n", stderr); \
00011       retval = 1; \
00012       goto the_end; \
00013     }
00014 
00015 int
00016 main (int argc, char *argv[])
00017 {
00018   char *name;
00019   FILE *fp = NULL;
00020   int retval = 0;
00021   int c;
00022   char buffer[64];
00023 
00024   name = tmpnam (NULL);
00025   fp = fopen (name, "w");
00026   assert (fp != NULL)
00027   fputs ("bla", fp);
00028   fclose (fp);
00029   fp = NULL;
00030 
00031   fp = fopen (name, "r");
00032   assert (fp != NULL);
00033   assert (ungetc ('z', fp) == 'z');
00034   assert (getc (fp) == 'z');
00035   assert (getc (fp) == 'b');
00036   assert (getc (fp) == 'l');
00037   assert (ungetc ('m', fp) == 'm');
00038   assert (getc (fp) == 'm');
00039   assert ((c = getc (fp)) == 'a');
00040   assert (getc (fp) == EOF);
00041   assert (ungetc (c, fp) == c);
00042   assert (feof (fp) == 0);
00043   assert (getc (fp) == c);
00044   assert (getc (fp) == EOF);
00045   fclose (fp);
00046   fp = NULL;
00047 
00048   fp = fopen (name, "r");
00049   assert (fp != NULL);
00050   assert (getc (fp) == 'b');
00051   assert (getc (fp) == 'l');
00052   assert (ungetc ('b', fp) == 'b');
00053   assert (fread (buffer, 1, 64, fp) == 2);
00054   assert (buffer[0] == 'b');
00055   assert (buffer[1] == 'a');
00056 
00057 the_end:
00058   if (fp != NULL)
00059     fclose (fp);
00060   unlink (name);
00061 
00062   return retval;
00063 }