Back to index

glibc  2.9
bug-ungetwc2.c
Go to the documentation of this file.
00001 #define _XOPEN_SOURCE 500
00002 #include <errno.h>
00003 #include <error.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <locale.h>
00007 #include <wchar.h>
00008 
00009 const char test_locale[] = "de_DE.UTF-8";
00010 const wchar_t write_wchars[] = {L'A', 0x00C4, L'B', L'\0'};
00011                                 /* `0x00C4' is A with diaeresis. */
00012 size_t last_pos = 2;            /* Which character is last one to read. */
00013 wint_t unget_wchar = L'C';      /* Ungotten ide characters. */
00014 
00015 char *fname;
00016 
00017 
00018 static int do_test (void);
00019 #define TEST_FUNCTION do_test ()
00020 
00021 #include "../test-skeleton.c"
00022 
00023 
00024 static int
00025 do_test (void)
00026 {
00027   size_t i;
00028   wint_t wc;
00029   FILE *fp;
00030   int fd;
00031 
00032   fname = (char *) malloc (strlen (test_dir) + sizeof "/bug-ungetwc2.XXXXXX");
00033   if (fname == NULL)
00034     {
00035       puts ("no memory");
00036       return 1;
00037     }
00038   strcpy (stpcpy (fname, test_dir), "/bug-ungetwc2.XXXXXX");
00039   fd = mkstemp (fname);
00040   if (fd == -1)
00041     {
00042       printf ("cannot open temporary file: %m\n");
00043       return 1;
00044     }
00045   add_temp_file (fname);
00046 
00047   printf ("\nNote: This program runs on %s locale.\n\n", test_locale);
00048 
00049   if (setlocale (LC_ALL, test_locale) == NULL)
00050     {
00051       fprintf (stderr, "Cannot use `%s' locale.\n", test_locale);
00052       exit (EXIT_FAILURE);
00053     }
00054 
00055   /* Output to the file. */
00056   if ((fp = fdopen (fd, "w")) == NULL)
00057     {
00058       setlocale (LC_ALL, "C");
00059       fprintf (stderr, "Cannot make `%s' file.\n", fname);
00060       exit (EXIT_FAILURE);
00061     }
00062   fprintf (fp, "%ls", write_wchars);
00063   fclose (fp);
00064 
00065   /* Read from the file. */
00066   fp = fopen (fname, "r");
00067   if (fp == NULL)
00068     {
00069       setlocale (LC_ALL, "C");
00070       error (EXIT_FAILURE, errno, "cannot open %s", fname);
00071     }
00072 
00073   printf ("%s is opened.\n", fname);
00074 
00075   for (i = 0; i < last_pos; i++)
00076     {
00077       wc = getwc (fp);
00078       printf ("> `%lc' is gotten.\n", wc);
00079     }
00080 
00081   /* Unget a wide character. */
00082   ungetwc (unget_wchar, fp);
00083   printf ("< `%lc' is ungotten.\n", unget_wchar);
00084 
00085   /* Reget the wide character. */
00086   wc = getwc (fp);
00087   printf ("> `%lc' is regotten.\n", wc);
00088 
00089   fflush (stdout);
00090   fclose (fp);
00091 
00092   return 0;
00093 }