1/* Taken from the Li18nux base test suite. */
2
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>
10
11#define WIDE_STR_LEN 32
12
13int
14main (int argc, char *argv[])
15{
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;
25
26 puts (s: "This program runs on de_DE.UTF-8 locale.");
27 if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL)
28 {
29 fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale");
30 exit (EXIT_FAILURE);
31 }
32
33 /* Make a file `il_str1'. */
34 fd = mkstemp (template: name1);
35 if (fd == -1)
36 {
37 printf (format: "cannot open temp file: %m\n");
38 exit (EXIT_FAILURE);
39 }
40 if ((fp = fdopen (fd, "w")) == NULL)
41 {
42 printf (format: "Can't open %s.\n", argv[1]);
43 exit (EXIT_FAILURE);
44 }
45 fwrite (il_str1, sizeof (char), sizeof (il_str1), fp);
46 fclose (fp);
47
48 /* Make a file `il_str2'. */
49 fd = mkstemp (template: name2);
50 if (fd == -1)
51 {
52 printf (format: "cannot open temp file: %m\n");
53 exit (EXIT_FAILURE);
54 }
55 if ((fp = fdopen (fd, "w")) == NULL)
56 {
57 fprintf (stderr, "Can't open %s.\n", argv[1]);
58 exit (EXIT_FAILURE);
59 }
60 fwrite (il_str2, sizeof (char), sizeof (il_str2), fp);
61 fclose (fp);
62
63
64 /* Test for il_str1. */
65 if ((fp = fopen (name1, "r")) == NULL)
66 {
67 fprintf (stderr, "Can't open %s.\n", argv[1]);
68 exit (EXIT_FAILURE);
69 }
70
71 puts (s: "--");
72 puts (s: "Read a byte sequence which is invalid as a wide character string.");
73 puts (s: " bytes: 0xe3, 0x81, '\\0'");
74
75 errno = 0;
76 ret = fgetws (ws: wcs, WIDE_STR_LEN, stream: fp);
77
78 if (ret == NULL)
79 {
80 puts (s: "Return Value: NULL");
81
82 if (errno == EILSEQ)
83 puts (s: "errno = EILSEQ");
84 else
85 {
86 printf (format: "errno = %d\n", errno);
87 result = 1;
88 }
89 }
90 else
91 {
92 printf (format: "Return Value: %p\n", ret);
93 for (i = 0; i < wcslen (s: wcs) + 1; i++)
94 printf (format: " wcs[%zd] = %04x", i, (unsigned int)wcs[i]);
95 printf (format: "\n");
96 result = 1;
97 }
98
99 /* Test for il_str2. */
100 if ((fp = fopen (name2, "r")) == NULL)
101 {
102 fprintf (stderr, "Can't open %s.\n", argv[1]);
103 exit (EXIT_FAILURE);
104 }
105
106 puts (s: "--");
107 puts (s: "Read a byte sequence which is invalid as a wide character string.");
108 puts (s: " bytes: '0', '\\n', 'A', 'B', 0xe3, 0x81, 'c', '\\0'");
109
110 errno = 0;
111 ret = fgetws (ws: wcs, WIDE_STR_LEN, stream: fp);
112
113 if (ret == NULL)
114 {
115 puts (s: "Return Value: NULL");
116
117 if (errno == EILSEQ)
118 puts (s: "errno = EILSEQ");
119 else
120 printf (format: "errno = %d\n", errno);
121
122 result = 1;
123 }
124 else
125 {
126 size_t i;
127
128 printf (format: "Return Value: %p\n", ret);
129 for (i = 0; i < wcslen (s: wcs) + 1; i++)
130 printf (format: " wcs[%zd] = 0x%04x", i, (unsigned int)wcs[i]);
131 printf (format: "\n");
132
133 for (i = 0; il_str2[i] != '\n'; ++i)
134 if ((wchar_t) il_str2[i] != wcs[i])
135 {
136 puts (s: "read string not correct");
137 result = 1;
138 break;
139 }
140 if (il_str2[i] == '\n')
141 {
142 if (wcs[i] != L'\n')
143 {
144 puts (s: "newline missing");
145 result = 1;
146 }
147 else if (wcs[i + 1] != L'\0')
148 {
149 puts (s: "read string not NUL-terminated");
150 result = 1;
151 }
152 }
153 }
154
155 puts (s: "\nsecond line");
156 errno = 0;
157 ret = fgetws (ws: wcs, WIDE_STR_LEN, stream: fp);
158
159 if (ret == NULL)
160 {
161 puts (s: "Return Value: NULL");
162
163 if (errno == EILSEQ)
164 puts (s: "errno = EILSEQ");
165 else
166 {
167 printf (format: "errno = %d\n", errno);
168 result = 1;
169 }
170 }
171 else
172 {
173 printf (format: "Return Value: %p\n", ret);
174 for (i = 0; i < wcslen (s: wcs) + 1; i++)
175 printf (format: " wcs[%zd] = 0x%04x", i, (unsigned int)wcs[i]);
176 printf (format: "\n");
177 }
178
179 fclose (fp);
180
181 unlink (name: name1);
182 unlink (name: name2);
183
184 return result;
185}
186

source code of glibc/libio/tst-fgetws.c