Back to index

glibc  2.9
tst-ungetwc2.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;
00016   char fname[] = "/tmp/tst-ungetwc2.out.XXXXXX";
00017   int fd;
00018   long int pos;
00019   int result = 0;
00020 
00021   puts ("This program runs on de_DE.UTF-8 locale.");
00022   if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
00023     {
00024       fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale\n");
00025       exit (EXIT_FAILURE);
00026     }
00027 
00028   /* Write some characters to `testfile'. */
00029   fd = mkstemp (fname);
00030   if (fd == -1)
00031     {
00032       printf ("cannot open temp file: %m\n");
00033       exit (EXIT_FAILURE);
00034     }
00035   if ((fp = fdopen (fd, "w")) == NULL)
00036     {
00037       fprintf (stderr, "Cannot open 'testfile'.\n");
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   /* Get a character. */
00051   wc = getwc (fp);
00052   pos = ftell (fp);
00053   printf ("After get a character: %ld\n", pos);
00054   if (pos != 1)
00055     result = 1;
00056 
00057   /* Unget a character. */
00058   ret = ungetwc (wc, fp);
00059   if (ret == WEOF)
00060     {
00061       fprintf (stderr, "ungetwc() returns NULL.");
00062       exit (EXIT_FAILURE);
00063     }
00064   pos = ftell (fp);
00065   printf ("After unget a character: %ld\n", pos);
00066   if (pos != 0)
00067     result = 1;
00068 
00069   /* Reget a character. */
00070   wc = getwc (fp);
00071   pos = ftell (fp);
00072   printf ("After reget a character: %ld\n", pos);
00073   if (pos != 1)
00074     result = 1;
00075 
00076   fclose (fp);
00077 
00078   unlink (fname);
00079 
00080   return result;
00081 }