1 | #define _XOPEN_SOURCE 500 |
2 | #include <errno.h> |
3 | #include <error.h> |
4 | #include <stdio.h> |
5 | #include <stdlib.h> |
6 | #include <locale.h> |
7 | #include <wchar.h> |
8 | |
9 | const char test_locale[] = "de_DE.UTF-8" ; |
10 | const wchar_t write_wchars[] = {L'A', 0x00C4, L'B', L'\0'}; |
11 | /* `0x00C4' is A with diaeresis. */ |
12 | size_t last_pos = 2; /* Which character is last one to read. */ |
13 | wint_t unget_wchar = L'C'; /* Ungotten ide characters. */ |
14 | |
15 | char *fname; |
16 | |
17 | |
18 | static int do_test (void); |
19 | #define TEST_FUNCTION do_test () |
20 | |
21 | #include "../test-skeleton.c" |
22 | |
23 | |
24 | static int |
25 | do_test (void) |
26 | { |
27 | size_t i; |
28 | wint_t wc; |
29 | FILE *fp; |
30 | int fd; |
31 | |
32 | fname = (char *) malloc (size: strlen (test_dir) + sizeof "/bug-ungetwc2.XXXXXX" ); |
33 | if (fname == NULL) |
34 | { |
35 | puts (s: "no memory" ); |
36 | return 1; |
37 | } |
38 | strcpy (stpcpy (fname, test_dir), "/bug-ungetwc2.XXXXXX" ); |
39 | fd = mkstemp (template: fname); |
40 | if (fd == -1) |
41 | { |
42 | printf (format: "cannot open temporary file: %m\n" ); |
43 | return 1; |
44 | } |
45 | add_temp_file (name: fname); |
46 | |
47 | printf (format: "\nNote: This program runs on %s locale.\n\n" , test_locale); |
48 | |
49 | if (setlocale (LC_ALL, test_locale) == NULL) |
50 | { |
51 | fprintf (stderr, "Cannot use `%s' locale.\n" , test_locale); |
52 | exit (EXIT_FAILURE); |
53 | } |
54 | |
55 | /* Output to the file. */ |
56 | if ((fp = fdopen (fd, "w" )) == NULL) |
57 | { |
58 | setlocale (LC_ALL, "C" ); |
59 | fprintf (stderr, "Cannot make `%s' file.\n" , fname); |
60 | exit (EXIT_FAILURE); |
61 | } |
62 | fprintf (fp, "%ls" , write_wchars); |
63 | fclose (fp); |
64 | |
65 | /* Read from the file. */ |
66 | fp = fopen (fname, "r" ); |
67 | if (fp == NULL) |
68 | { |
69 | setlocale (LC_ALL, "C" ); |
70 | error (EXIT_FAILURE, errno, format: "cannot open %s" , fname); |
71 | } |
72 | |
73 | printf (format: "%s is opened.\n" , fname); |
74 | |
75 | for (i = 0; i < last_pos; i++) |
76 | { |
77 | wc = getwc (stream: fp); |
78 | printf (format: "> `%lc' is gotten.\n" , wc); |
79 | } |
80 | |
81 | /* Unget a wide character. */ |
82 | ungetwc (wc: unget_wchar, stream: fp); |
83 | printf (format: "< `%lc' is ungotten.\n" , unget_wchar); |
84 | |
85 | /* Reget the wide character. */ |
86 | wc = getwc (stream: fp); |
87 | printf (format: "> `%lc' is regotten.\n" , wc); |
88 | |
89 | fflush (stdout); |
90 | fclose (fp); |
91 | |
92 | return 0; |
93 | } |
94 | |