Back to index

glibc  2.9
tst-fgetws.c
Go to the documentation of this file.
00001 /* Taken from the Li18nux base test suite.  */
00002 
00003 #define _XOPEN_SOURCE 500
00004 #include <errno.h>
00005 #include <locale.h>
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <unistd.h>
00009 #include <wchar.h>
00010 
00011 #define WIDE_STR_LEN 32
00012 
00013 int
00014 main (int argc, char *argv[])
00015 {
00016   size_t i;
00017   FILE   *fp;
00018   wchar_t *ret, wcs[WIDE_STR_LEN];
00019   int result = 0;
00020   const char il_str1[] = {0xe3, 0x81, '\0'};
00021   const char il_str2[] = {'0', '\n', 'A', 'B', 0xe3, 0x81, 'E', '\0'};
00022   char name1[] = "/tmp/tst-fgetws.out.XXXXXX";
00023   char name2[] = "/tmp/tst-fgetws.out.XXXXXX";
00024   int fd;
00025 
00026   puts ("This program runs on de_DE.UTF-8 locale.");
00027   if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
00028     {
00029       fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
00030       exit (EXIT_FAILURE);
00031     }
00032 
00033   /* Make a file `il_str1'. */
00034   fd = mkstemp (name1);
00035   if (fd == -1)
00036     {
00037       printf ("cannot open temp file: %m\n");
00038       exit (EXIT_FAILURE);
00039     }
00040   if ((fp = fdopen (fd, "w")) == NULL)
00041     {
00042       printf ("Can't open %s.\n", argv[1]);
00043       exit (EXIT_FAILURE);
00044     }
00045   fwrite (il_str1, sizeof (char), sizeof (il_str1), fp);
00046   fclose (fp);
00047 
00048   /* Make a file `il_str2'. */
00049   fd = mkstemp (name2);
00050   if (fd == -1)
00051     {
00052       printf ("cannot open temp file: %m\n");
00053       exit (EXIT_FAILURE);
00054     }
00055   if ((fp = fdopen (fd, "w")) == NULL)
00056     {
00057       fprintf (stderr, "Can't open %s.\n", argv[1]);
00058       exit (EXIT_FAILURE);
00059     }
00060   fwrite (il_str2, sizeof (char), sizeof (il_str2), fp);
00061   fclose (fp);
00062 
00063 
00064   /* Test for il_str1. */
00065   if ((fp = fopen (name1, "r")) == NULL)
00066     {
00067       fprintf (stderr, "Can't open %s.\n", argv[1]);
00068       exit (EXIT_FAILURE);
00069     }
00070 
00071   puts ("--");
00072   puts ("Read a byte sequence which is invalid as a wide character string.");
00073   puts (" bytes: 0xe3, 0x81, '\\0'");
00074 
00075   errno = 0;
00076   ret = fgetws (wcs, WIDE_STR_LEN, fp);
00077 
00078   if (ret == NULL)
00079     {
00080       puts ("Return Value: NULL");
00081 
00082       if (errno == EILSEQ)
00083        puts ("errno = EILSEQ");
00084       else
00085        {
00086          printf ("errno = %d\n", errno);
00087          result = 1;
00088        }
00089     }
00090   else
00091     {
00092       printf ("Return Value: %p\n", ret);
00093       for (i = 0; i < wcslen (wcs) + 1; i++)
00094        printf (" wcs[%zd] = %04x", i, (unsigned int)wcs[i]);
00095       printf ("\n");
00096       result = 1;
00097     }
00098 
00099   /* Test for il_str2. */
00100   if ((fp = fopen (name2, "r")) == NULL)
00101     {
00102       fprintf (stderr, "Can't open %s.\n", argv[1]);
00103       exit (EXIT_FAILURE);
00104     }
00105 
00106   puts ("--");
00107   puts ("Read a byte sequence which is invalid as a wide character string.");
00108   puts (" bytes: '0', '\\n', 'A', 'B', 0xe3, 0x81, 'c', '\\0'");
00109 
00110   errno = 0;
00111   ret = fgetws (wcs, WIDE_STR_LEN, fp);
00112 
00113   if (ret == NULL)
00114     {
00115       puts ("Return Value: NULL");
00116 
00117       if (errno == EILSEQ)
00118        puts ("errno = EILSEQ");
00119       else
00120        printf ("errno = %d\n", errno);
00121 
00122       result = 1;
00123     }
00124   else
00125     {
00126       size_t i;
00127 
00128       printf ("Return Value: %p\n", ret);
00129       for (i = 0; i < wcslen (wcs) + 1; i++)
00130        printf (" wcs[%zd] = 0x%04x", i, (unsigned int)wcs[i]);
00131       printf ("\n");
00132 
00133       for (i = 0; il_str2[i] != '\n'; ++i)
00134        if ((wchar_t) il_str2[i] != wcs[i])
00135          {
00136            puts ("read string not correct");
00137            result = 1;
00138            break;
00139          }
00140       if (il_str2[i] == '\n')
00141        {
00142          if (wcs[i] != L'\n')
00143            {
00144              puts ("newline missing");
00145              result = 1;
00146            }
00147          else if (wcs[i + 1] != L'\0')
00148            {
00149              puts ("read string not NUL-terminated");
00150              result = 1;
00151            }
00152        }
00153     }
00154 
00155   puts ("\nsecond line");
00156   errno = 0;
00157   ret = fgetws (wcs, WIDE_STR_LEN, fp);
00158 
00159   if (ret == NULL)
00160     {
00161       puts ("Return Value: NULL");
00162 
00163       if (errno == EILSEQ)
00164        puts ("errno = EILSEQ");
00165       else
00166        {
00167          printf ("errno = %d\n", errno);
00168          result = 1;
00169        }
00170     }
00171   else
00172     {
00173       printf ("Return Value: %p\n", ret);
00174       for (i = 0; i < wcslen (wcs) + 1; i++)
00175        printf (" wcs[%zd] = 0x%04x", i, (unsigned int)wcs[i]);
00176       printf ("\n");
00177     }
00178 
00179   fclose (fp);
00180 
00181   unlink (name1);
00182   unlink (name2);
00183 
00184   return result;
00185 }