lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 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 | int |
| 14 | main (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 ("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 (name1); |
| 35 | if (fd == -1) |
| 36 | { |
| 37 | printf ("cannot open temp file: %m\n"); |
| 38 | exit (EXIT_FAILURE); |
| 39 | } |
| 40 | if ((fp = fdopen (fd, "w")) == NULL) |
| 41 | { |
| 42 | printf ("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 (name2); |
| 50 | if (fd == -1) |
| 51 | { |
| 52 | printf ("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 ("--"); |
| 72 | puts ("Read a byte sequence which is invalid as a wide character string."); |
| 73 | puts (" bytes: 0xe3, 0x81, '\\0'"); |
| 74 | |
| 75 | errno = 0; |
| 76 | ret = fgetws (wcs, WIDE_STR_LEN, fp); |
| 77 | |
| 78 | if (ret == NULL) |
| 79 | { |
| 80 | puts ("Return Value: NULL"); |
| 81 | |
| 82 | if (errno == EILSEQ) |
| 83 | puts ("errno = EILSEQ"); |
| 84 | else |
| 85 | { |
| 86 | printf ("errno = %d\n", errno); |
| 87 | result = 1; |
| 88 | } |
| 89 | } |
| 90 | else |
| 91 | { |
| 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; |
| 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 ("--"); |
| 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'"); |
| 109 | |
| 110 | errno = 0; |
| 111 | ret = fgetws (wcs, WIDE_STR_LEN, fp); |
| 112 | |
| 113 | if (ret == NULL) |
| 114 | { |
| 115 | puts ("Return Value: NULL"); |
| 116 | |
| 117 | if (errno == EILSEQ) |
| 118 | puts ("errno = EILSEQ"); |
| 119 | else |
| 120 | printf ("errno = %d\n", errno); |
| 121 | |
| 122 | result = 1; |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | size_t i; |
| 127 | |
| 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"); |
| 132 | |
| 133 | for (i = 0; il_str2[i] != '\n'; ++i) |
| 134 | if ((wchar_t) il_str2[i] != wcs[i]) |
| 135 | { |
| 136 | puts ("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 ("newline missing"); |
| 145 | result = 1; |
| 146 | } |
| 147 | else if (wcs[i + 1] != L'\0') |
| 148 | { |
| 149 | puts ("read string not NUL-terminated"); |
| 150 | result = 1; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | puts ("\nsecond line"); |
| 156 | errno = 0; |
| 157 | ret = fgetws (wcs, WIDE_STR_LEN, fp); |
| 158 | |
| 159 | if (ret == NULL) |
| 160 | { |
| 161 | puts ("Return Value: NULL"); |
| 162 | |
| 163 | if (errno == EILSEQ) |
| 164 | puts ("errno = EILSEQ"); |
| 165 | else |
| 166 | { |
| 167 | printf ("errno = %d\n", errno); |
| 168 | result = 1; |
| 169 | } |
| 170 | } |
| 171 | else |
| 172 | { |
| 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"); |
| 177 | } |
| 178 | |
| 179 | fclose (fp); |
| 180 | |
| 181 | unlink (name1); |
| 182 | unlink (name2); |
| 183 | |
| 184 | return result; |
| 185 | } |