* rt/aio.h: Add __nonnull attributes.
[glibc.git] / libio / tst-fgetws.c
blob1e2933274315f2f0421a4de143261ee98678766c
1 /* Taken from the Li18nux base test suite. */
3 #define _XOPEN_SOURCE 500
4 #include <errno.h>
5 #include <locale.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <wchar.h>
11 #define WIDE_STR_LEN 32
13 int
14 main (int argc, char *argv[])
16 size_t i;
17 FILE *fp;
18 wchar_t *ret, wcs[WIDE_STR_LEN];
19 int result = 0;
20 const char il_str1[] = {0xe3, 0x81, '\0'};
21 const char il_str2[] = {'0', '\n', 'A', 'B', 0xe3, 0x81, 'E', '\0'};
22 char name1[] = "/tmp/tst-fgetws.out.XXXXXX";
23 char name2[] = "/tmp/tst-fgetws.out.XXXXXX";
24 int fd;
26 puts ("This program runs on de_DE.UTF-8 locale.");
27 if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
29 fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
30 exit (EXIT_FAILURE);
33 /* Make a file `il_str1'. */
34 fd = mkstemp (name1);
35 if (fd == -1)
37 printf ("cannot open temp file: %m\n");
38 exit (EXIT_FAILURE);
40 if ((fp = fdopen (fd, "w")) == NULL)
42 printf ("Can't open %s.\n", argv[1]);
43 exit (EXIT_FAILURE);
45 fwrite (il_str1, sizeof (char), sizeof (il_str1), fp);
46 fclose (fp);
48 /* Make a file `il_str2'. */
49 fd = mkstemp (name2);
50 if (fd == -1)
52 printf ("cannot open temp file: %m\n");
53 exit (EXIT_FAILURE);
55 if ((fp = fdopen (fd, "w")) == NULL)
57 fprintf (stderr, "Can't open %s.\n", argv[1]);
58 exit (EXIT_FAILURE);
60 fwrite (il_str2, sizeof (char), sizeof (il_str2), fp);
61 fclose (fp);
64 /* Test for il_str1. */
65 if ((fp = fopen (name1, "r")) == NULL)
67 fprintf (stderr, "Can't open %s.\n", argv[1]);
68 exit (EXIT_FAILURE);
71 puts ("--");
72 puts ("Read a byte sequence which is invalid as a wide character string.");
73 puts (" bytes: 0xe3, 0x81, '\\0'");
75 errno = 0;
76 ret = fgetws (wcs, WIDE_STR_LEN, fp);
78 if (ret == NULL)
80 puts ("Return Value: NULL");
82 if (errno == EILSEQ)
83 puts ("errno = EILSEQ");
84 else
86 printf ("errno = %d\n", errno);
87 result = 1;
90 else
92 printf ("Return Value: %p\n", ret);
93 for (i = 0; i < wcslen (wcs) + 1; i++)
94 printf (" wcs[%zd] = %04x", i, (unsigned int)wcs[i]);
95 printf ("\n");
96 result = 1;
99 /* Test for il_str2. */
100 if ((fp = fopen (name2, "r")) == NULL)
102 fprintf (stderr, "Can't open %s.\n", argv[1]);
103 exit (EXIT_FAILURE);
106 puts ("--");
107 puts ("Read a byte sequence which is invalid as a wide character string.");
108 puts (" bytes: '0', '\\n', 'A', 'B', 0xe3, 0x81, 'c', '\\0'");
110 errno = 0;
111 ret = fgetws (wcs, WIDE_STR_LEN, fp);
113 if (ret == NULL)
115 puts ("Return Value: NULL");
117 if (errno == EILSEQ)
118 puts ("errno = EILSEQ");
119 else
120 printf ("errno = %d\n", errno);
122 result = 1;
124 else
126 size_t i;
128 printf ("Return Value: %p\n", ret);
129 for (i = 0; i < wcslen (wcs) + 1; i++)
130 printf (" wcs[%zd] = 0x%04x", i, (unsigned int)wcs[i]);
131 printf ("\n");
133 for (i = 0; il_str2[i] != '\n'; ++i)
134 if ((wchar_t) il_str2[i] != wcs[i])
136 puts ("read string not correct");
137 result = 1;
138 break;
140 if (il_str2[i] == '\n')
142 if (wcs[i] != L'\n')
144 puts ("newline missing");
145 result = 1;
147 else if (wcs[i + 1] != L'\0')
149 puts ("read string not NUL-terminated");
150 result = 1;
155 puts ("\nsecond line");
156 errno = 0;
157 ret = fgetws (wcs, WIDE_STR_LEN, fp);
159 if (ret == NULL)
161 puts ("Return Value: NULL");
163 if (errno == EILSEQ)
164 puts ("errno = EILSEQ");
165 else
167 printf ("errno = %d\n", errno);
168 result = 1;
171 else
173 printf ("Return Value: %p\n", ret);
174 for (i = 0; i < wcslen (wcs) + 1; i++)
175 printf (" wcs[%zd] = 0x%04x", i, (unsigned int)wcs[i]);
176 printf ("\n");
179 fclose (fp);
181 unlink (name1);
182 unlink (name2);
184 return result;