Back to index

glibc  2.9
tst-ungetwc1.c
Go to the documentation of this file.
00001 /* Taken from the Li18nux base test suite.  */
00002 
00003 #define _XOPEN_SOURCE 500
00004 #include <locale.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 #include <wchar.h>
00009 
00010 int
00011 main (void)
00012 {
00013   FILE *fp;
00014   const char *str = "abcdef";
00015   wint_t ret, wc, ungetone = 0x00E4;      /* 0x00E4 means `a umlaut'. */
00016   char fname[] = "/tmp/tst-ungetwc1.out.XXXXXX";
00017   int fd;
00018   int result = 0;
00019 
00020   puts ("This program runs on de_DE.UTF-8 locale.");
00021   if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
00022     {
00023       fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
00024       exit (EXIT_FAILURE);
00025     }
00026 
00027   fd = mkstemp (fname);
00028   if (fd == -1)
00029     {
00030       printf ("cannot open temp file: %m\n");
00031       exit (EXIT_FAILURE);
00032     }
00033 
00034   /* Write some characters to `testfile'. */
00035   if ((fp = fdopen (fd, "w")) == NULL)
00036     {
00037       fprintf (stderr, "Cannot open 'testfile'.");
00038       exit (EXIT_FAILURE);
00039     }
00040   fputs (str, fp);
00041   fclose (fp);
00042 
00043   /* Open `testfile'. */
00044   if ((fp = fopen (fname, "r")) == NULL)
00045     {
00046       fprintf (stderr, "Cannot open 'testfile'.");
00047       exit (EXIT_FAILURE);
00048     }
00049 
00050   /* Unget a character. */
00051   ret = ungetwc (ungetone, fp);
00052   printf ("Unget a character (0x%04x)\n", (unsigned int) ungetone);
00053   fflush (stdout);
00054   if (ret == WEOF)
00055     {
00056       puts ("ungetwc() returns NULL.");
00057       exit (EXIT_SUCCESS);
00058     }
00059 
00060   /* Reget a character. */
00061   wc = getwc (fp);
00062   printf ("Reget a character (0x%04x)\n", (unsigned int) wc);
00063   fflush (stdout);
00064   if (wc == ungetone)
00065     {
00066       puts ("The ungotten character is equal to the regotten character.");
00067       fflush (stdout);
00068     }
00069   else
00070     {
00071       puts ("The ungotten character is not equal to the regotten character.");
00072       printf ("ungotten one: %04x, regetone: %04x", ungetone, wc);
00073       fflush (stdout);
00074       result = 1;
00075     }
00076   fclose (fp);
00077 
00078   unlink (fname);
00079 
00080   return result;
00081 }