1 | /* Taken from the Li18nux base test suite. */ |
2 | |
3 | #define _XOPEN_SOURCE 500 |
4 | #include <locale.h> |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <unistd.h> |
8 | #include <wchar.h> |
9 | |
10 | static int |
11 | do_test (void) |
12 | { |
13 | FILE *fp; |
14 | const char *str = "abcdef" ; |
15 | wint_t ret, wc; |
16 | char fname[] = "/tmp/tst-ungetwc2.out.XXXXXX" ; |
17 | int fd; |
18 | long int pos; |
19 | int result = 0; |
20 | |
21 | puts (s: "This program runs on de_DE.UTF-8 locale." ); |
22 | if (setlocale (LC_ALL, "de_DE.UTF-8" ) == NULL) |
23 | { |
24 | fprintf (stderr, "Err: Cannot run on the de_DE.UTF-8 locale\n" ); |
25 | exit (EXIT_FAILURE); |
26 | } |
27 | |
28 | /* Write some characters to `testfile'. */ |
29 | fd = mkstemp (template: fname); |
30 | if (fd == -1) |
31 | { |
32 | printf (format: "cannot open temp file: %m\n" ); |
33 | exit (EXIT_FAILURE); |
34 | } |
35 | if ((fp = fdopen (fd, "w" )) == NULL) |
36 | { |
37 | fprintf (stderr, "Cannot open 'testfile'.\n" ); |
38 | exit (EXIT_FAILURE); |
39 | } |
40 | fputs (str, fp); |
41 | fclose (fp); |
42 | |
43 | /* Open `testfile'. */ |
44 | if ((fp = fopen (fname, "r" )) == NULL) |
45 | { |
46 | fprintf (stderr, "Cannot open 'testfile'." ); |
47 | exit (EXIT_FAILURE); |
48 | } |
49 | |
50 | /* Get a character. */ |
51 | wc = getwc (stream: fp); |
52 | pos = ftell (stream: fp); |
53 | printf (format: "After get a character: %ld\n" , pos); |
54 | if (pos != 1) |
55 | result = 1; |
56 | |
57 | /* Unget a character. */ |
58 | ret = ungetwc (wc: wc, stream: fp); |
59 | if (ret == WEOF) |
60 | { |
61 | fprintf (stderr, "ungetwc() returns NULL." ); |
62 | exit (EXIT_FAILURE); |
63 | } |
64 | pos = ftell (stream: fp); |
65 | printf (format: "After unget a character: %ld\n" , pos); |
66 | if (pos != 0) |
67 | result = 1; |
68 | |
69 | /* Reget a character. */ |
70 | wc = getwc (stream: fp); |
71 | pos = ftell (stream: fp); |
72 | printf (format: "After reget a character: %ld\n" , pos); |
73 | if (pos != 1) |
74 | result = 1; |
75 | |
76 | fclose (fp); |
77 | |
78 | unlink (name: fname); |
79 | |
80 | return result; |
81 | } |
82 | |
83 | #define TEST_FUNCTION do_test () |
84 | #include "../test-skeleton.c" |
85 | |