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