Back to index

glibc  2.9
bug-ungetwc1.c
Go to the documentation of this file.
00001 #define _XOPEN_SOURCE 500
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <locale.h>
00005 #include <wchar.h>
00006 
00007 const char write_chars[] = "ABC";      /* Characters on testfile. */
00008 const wint_t unget_wchar = L'A';      /* Ungotten wide character. */
00009 
00010 char *fname;
00011 
00012 
00013 static int do_test (void);
00014 #define TEST_FUNCTION do_test ()
00015 
00016 #include "../test-skeleton.c"
00017 
00018 
00019 static int
00020 do_test (void)
00021 {
00022   wint_t wc;
00023   FILE *fp;
00024   int fd;
00025 
00026   fname = (char *) malloc (strlen (test_dir) + sizeof "/bug-ungetwc1.XXXXXX");
00027   if (fname == NULL)
00028     {
00029       puts ("no memory");
00030       return 1;
00031     }
00032   strcpy (stpcpy (fname, test_dir), "/bug-ungetwc1.XXXXXX");
00033   fd = mkstemp (fname);
00034   if (fd == -1)
00035     {
00036       printf ("cannot open temporary file: %m\n");
00037       return 1;
00038     }
00039   add_temp_file (fname);
00040 
00041   setlocale(LC_ALL, "");
00042 
00043   /* Output to the file. */
00044   if ((fp = fdopen (fd, "w")) == NULL)
00045     {
00046       fprintf (stderr, "Cannot make `%s' file\n", fname);
00047       exit (EXIT_FAILURE);
00048     }
00049 
00050   fprintf (fp, "%s", write_chars);
00051   fclose (fp);
00052 
00053   /* Read from the file. */
00054   fp = fopen (fname, "r");
00055 
00056   while (!feof (fp))
00057     wc = getwc (fp);
00058   printf ("\nThe end-of-file indicator is set.\n");
00059 
00060   /* Unget a wide character. */
00061   ungetwc (unget_wchar, fp);
00062   printf ("< `%lc' is ungotten.\n", unget_wchar);
00063 
00064   /* Check the end-of-file indicator. */
00065   if (feof (fp))
00066     printf ("The end-of-file indicator is still set.\n");
00067   else
00068     printf ("The end-of-file flag is cleared.\n");
00069 
00070   fflush (stdout);
00071   fclose (fp);
00072 
00073   return 0;
00074 }