1 | /* Test of perror. |
2 | To be used only for testing glibc. */ |
3 | |
4 | #include <errno.h> |
5 | #include <error.h> |
6 | #include <stdio.h> |
7 | #include <stdlib.h> |
8 | #include <string.h> |
9 | #include <unistd.h> |
10 | #include <wchar.h> |
11 | |
12 | #include <support/xunistd.h> |
13 | |
14 | #define MB_EXP \ |
15 | "null mode test 1: Invalid or incomplete multibyte or wide character\n" \ |
16 | "multibyte string\n" \ |
17 | "<0 mode test: Invalid argument\n" |
18 | #define MB_EXP_LEN (sizeof (MB_EXP) - 1) |
19 | |
20 | #define WC_EXP \ |
21 | "null mode test 2: Invalid or incomplete multibyte or wide character\n" \ |
22 | "wide string\n" \ |
23 | ">0 mode test: Invalid argument\n" |
24 | #define WC_EXP_LEN (sizeof (WC_EXP) - 1) |
25 | |
26 | |
27 | static int |
28 | do_test (void) |
29 | { |
30 | int fd; |
31 | char fname[] = "/tmp/tst-perror.XXXXXX" ; |
32 | int result = 0; |
33 | char buf[200]; |
34 | ssize_t n; |
35 | |
36 | fd = mkstemp (template: fname); |
37 | if (fd == -1) |
38 | error (EXIT_FAILURE, errno, format: "cannot create temporary file" ); |
39 | |
40 | /* Make sure the file gets removed. */ |
41 | unlink (name: fname); |
42 | |
43 | fclose (stderr); |
44 | |
45 | if (dup2 (fd: fd, fd2: 2) == -1) |
46 | { |
47 | printf (format: "cannot create file descriptor 2: %m\n" ); |
48 | exit (EXIT_FAILURE); |
49 | } |
50 | |
51 | stderr = fdopen (2, "w" ); |
52 | if (stderr == NULL) |
53 | { |
54 | printf (format: "fdopen failed: %m\n" ); |
55 | exit (EXIT_FAILURE); |
56 | } |
57 | |
58 | if (fwide (stderr, mode: 0) != 0) |
59 | { |
60 | printf (format: "stderr not initially in mode 0\n" ); |
61 | exit (EXIT_FAILURE); |
62 | } |
63 | |
64 | errno = EILSEQ; |
65 | perror ("null mode test 1" ); |
66 | |
67 | if (fwide (stderr, mode: 0) != 0) |
68 | { |
69 | puts (s: "perror changed the mode from 0" ); |
70 | result = 1; |
71 | } |
72 | |
73 | fputs ("multibyte string\n" , stderr); |
74 | |
75 | if (fwide (stderr, mode: 0) >= 0) |
76 | { |
77 | puts (s: "fputs didn't set orientation to narrow" ); |
78 | result = 1; |
79 | } |
80 | |
81 | errno = EINVAL; |
82 | perror ("<0 mode test" ); |
83 | |
84 | fclose (stderr); |
85 | |
86 | lseek (fd: fd, offset: 0, SEEK_SET); |
87 | n = read (fd, buf, sizeof (buf)); |
88 | if (n != MB_EXP_LEN || memcmp (buf, MB_EXP, MB_EXP_LEN) != 0) |
89 | { |
90 | printf (format: "multibyte test failed. Expected:\n%s\nGot:\n%.*s\n" , |
91 | MB_EXP, (int) n, buf); |
92 | result = 1; |
93 | } |
94 | else |
95 | puts (s: "multibyte test succeeded" ); |
96 | |
97 | lseek (fd: fd, offset: 0, SEEK_SET); |
98 | xftruncate (fd, length: 0); |
99 | |
100 | if (dup2 (fd: fd, fd2: 2) == -1) |
101 | { |
102 | printf (format: "cannot create file descriptor 2: %m\n" ); |
103 | exit (EXIT_FAILURE); |
104 | } |
105 | stderr = fdopen (2, "w" ); |
106 | if (stderr == NULL) |
107 | { |
108 | printf (format: "fdopen failed: %m\n" ); |
109 | exit (EXIT_FAILURE); |
110 | } |
111 | |
112 | if (fwide (stderr, mode: 0) != 0) |
113 | { |
114 | printf (format: "stderr not initially in mode 0\n" ); |
115 | exit (EXIT_FAILURE); |
116 | } |
117 | |
118 | errno = EILSEQ; |
119 | perror ("null mode test 2" ); |
120 | |
121 | if (fwide (stderr, mode: 0) != 0) |
122 | { |
123 | puts (s: "perror changed the mode from 0" ); |
124 | result = 1; |
125 | } |
126 | |
127 | fputws (ws: L"wide string\n" , stderr); |
128 | |
129 | if (fwide (stderr, mode: 0) <= 0) |
130 | { |
131 | puts (s: "fputws didn't set orientation to wide" ); |
132 | result = 1; |
133 | } |
134 | |
135 | errno = EINVAL; |
136 | perror (">0 mode test" ); |
137 | |
138 | fclose (stderr); |
139 | |
140 | lseek (fd: fd, offset: 0, SEEK_SET); |
141 | n = read (fd, buf, sizeof (buf)); |
142 | if (n != WC_EXP_LEN || memcmp (buf, WC_EXP, WC_EXP_LEN) != 0) |
143 | { |
144 | printf (format: "wide test failed. Expected:\n%s\nGot:\n%.*s\n" , |
145 | WC_EXP, (int) n, buf); |
146 | result = 1; |
147 | } |
148 | else |
149 | puts (s: "wide test succeeded" ); |
150 | |
151 | close (fd: fd); |
152 | |
153 | return result; |
154 | } |
155 | |
156 | #define TEST_FUNCTION do_test () |
157 | #include "../test-skeleton.c" |
158 | |