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