]>
Commit | Line | Data |
---|---|---|
19361cb7 | 1 | /* Error handler for noninteractive utilities |
bfff8b1b | 2 | Copyright (C) 1990-2017 Free Software Foundation, Inc. |
10ffcd52 | 3 | This file is part of the GNU C Library. |
19361cb7 UD |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
19361cb7 UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
19361cb7 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
196980f5 RM |
18 | |
19 | /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ | |
20 | ||
888c679b | 21 | #if !_LIBC |
a44d2393 | 22 | # include <config.h> |
196980f5 RM |
23 | #endif |
24 | ||
888c679b SP |
25 | #include "error.h" |
26 | ||
240c62a7 | 27 | #include <stdarg.h> |
196980f5 | 28 | #include <stdio.h> |
240c62a7 UD |
29 | #include <stdlib.h> |
30 | #include <string.h> | |
31 | ||
888c679b SP |
32 | #if !_LIBC && ENABLE_NLS |
33 | # include "gettext.h" | |
34 | # define _(msgid) gettext (msgid) | |
35 | #endif | |
36 | ||
1fc0e331 | 37 | #ifdef _LIBC |
240c62a7 | 38 | # include <libintl.h> |
ce14a814 RM |
39 | # include <stdbool.h> |
40 | # include <stdint.h> | |
1fc0e331 | 41 | # include <wchar.h> |
6293b803 | 42 | # define mbsrtowcs __mbsrtowcs |
888c679b SP |
43 | # define USE_UNLOCKED_IO 0 |
44 | # define _GL_ATTRIBUTE_FORMAT_PRINTF(a, b) | |
45 | # define _GL_ARG_NONNULL(a) | |
1fc0e331 | 46 | #endif |
196980f5 | 47 | |
888c679b SP |
48 | #if USE_UNLOCKED_IO |
49 | # include "unlocked-io.h" | |
50 | #endif | |
0d204b0a | 51 | |
19bc17a9 | 52 | #ifndef _ |
a44d2393 | 53 | # define _(String) String |
19bc17a9 RM |
54 | #endif |
55 | ||
196980f5 RM |
56 | /* If NULL, error will flush stdout, then print on stderr the program |
57 | name, a colon and a space. Otherwise, error will call this | |
58 | function without parameters instead. */ | |
240c62a7 | 59 | void (*error_print_progname) (void); |
bbed653c | 60 | |
888c679b | 61 | /* This variable is incremented each time 'error' is called. */ |
bbed653c | 62 | unsigned int error_message_count; |
196980f5 RM |
63 | |
64 | #ifdef _LIBC | |
bbed653c RM |
65 | /* In the GNU C library, there is a predefined variable for this. */ |
66 | ||
a44d2393 UD |
67 | # define program_name program_invocation_name |
68 | # include <errno.h> | |
10ffcd52 | 69 | # include <limits.h> |
37de950b | 70 | # include <libio/libioP.h> |
bbed653c | 71 | |
888c679b | 72 | /* In GNU libc we want do not want to use the common name 'error' directly. |
2f6d1f1b | 73 | Instead make it a weak alias. */ |
104d0bd3 UD |
74 | extern void __error (int status, int errnum, const char *message, ...) |
75 | __attribute__ ((__format__ (__printf__, 3, 4))); | |
49f3a758 UD |
76 | extern void __error_at_line (int status, int errnum, const char *file_name, |
77 | unsigned int line_number, const char *message, | |
104d0bd3 UD |
78 | ...) |
79 | __attribute__ ((__format__ (__printf__, 5, 6)));; | |
a44d2393 UD |
80 | # define error __error |
81 | # define error_at_line __error_at_line | |
2f6d1f1b | 82 | |
e5e45b53 | 83 | # include <libio/iolibio.h> |
d18ea0c5 | 84 | # define fflush(s) _IO_fflush (s) |
e5e45b53 | 85 | # undef putc |
d18ea0c5 | 86 | # define putc(c, fp) _IO_putc (c, fp) |
50304ef0 | 87 | |
ec999b8e | 88 | # include <libc-lock.h> |
8c620ae0 | 89 | |
a44d2393 | 90 | #else /* not _LIBC */ |
196980f5 | 91 | |
888c679b SP |
92 | # include <fcntl.h> |
93 | # include <unistd.h> | |
94 | ||
95 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
96 | /* Get declarations of the native Windows API functions. */ | |
97 | # define WIN32_LEAN_AND_MEAN | |
98 | # include <windows.h> | |
99 | /* Get _get_osfhandle. */ | |
100 | # include "msvc-nothrow.h" | |
101 | # endif | |
102 | ||
103 | /* The gnulib override of fcntl is not needed in this file. */ | |
104 | # undef fcntl | |
105 | ||
106 | # if !HAVE_DECL_STRERROR_R | |
a5d1e89b RM |
107 | # ifndef HAVE_DECL_STRERROR_R |
108 | "this configure-time declaration test was not run" | |
109 | # endif | |
888c679b | 110 | # if STRERROR_R_CHAR_P |
a5d1e89b | 111 | char *strerror_r (); |
888c679b SP |
112 | # else |
113 | int strerror_r (); | |
114 | # endif | |
a5d1e89b RM |
115 | # endif |
116 | ||
196980f5 RM |
117 | /* The calling program should define program_name and set it to the |
118 | name of the executing program. */ | |
119 | extern char *program_name; | |
120 | ||
a5d1e89b | 121 | # if HAVE_STRERROR_R || defined strerror_r |
a44d2393 | 122 | # define __strerror_r strerror_r |
a5d1e89b | 123 | # endif /* HAVE_STRERROR_R || defined strerror_r */ |
a44d2393 | 124 | #endif /* not _LIBC */ |
196980f5 | 125 | |
888c679b SP |
126 | #if !_LIBC |
127 | /* Return non-zero if FD is open. */ | |
128 | static int | |
129 | is_open (int fd) | |
130 | { | |
131 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | |
132 | /* On native Windows: The initial state of unassigned standard file | |
133 | descriptors is that they are open but point to an INVALID_HANDLE_VALUE. | |
134 | There is no fcntl, and the gnulib replacement fcntl does not support | |
135 | F_GETFL. */ | |
136 | return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE; | |
137 | # else | |
138 | # ifndef F_GETFL | |
139 | # error Please port fcntl to your platform | |
140 | # endif | |
141 | return 0 <= fcntl (fd, F_GETFL); | |
142 | # endif | |
143 | } | |
144 | #endif | |
145 | ||
146 | static void | |
147 | flush_stdout (void) | |
148 | { | |
149 | #if !_LIBC | |
150 | int stdout_fd; | |
151 | ||
152 | # if GNULIB_FREOPEN_SAFER | |
153 | /* Use of gnulib's freopen-safer module normally ensures that | |
154 | fileno (stdout) == 1 | |
155 | whenever stdout is open. */ | |
156 | stdout_fd = STDOUT_FILENO; | |
157 | # else | |
158 | /* POSIX states that fileno (stdout) after fclose is unspecified. But in | |
159 | practice it is not a problem, because stdout is statically allocated and | |
160 | the fd of a FILE stream is stored as a field in its allocated memory. */ | |
161 | stdout_fd = fileno (stdout); | |
162 | # endif | |
163 | /* POSIX states that fflush (stdout) after fclose is unspecified; it | |
164 | is safe in glibc, but not on all other platforms. fflush (NULL) | |
165 | is always defined, but too draconian. */ | |
166 | if (0 <= stdout_fd && is_open (stdout_fd)) | |
167 | #endif | |
168 | fflush (stdout); | |
169 | } | |
170 | ||
a5d1e89b RM |
171 | static void |
172 | print_errno_message (int errnum) | |
173 | { | |
174 | char const *s; | |
175 | ||
176 | #if defined HAVE_STRERROR_R || _LIBC | |
177 | char errbuf[1024]; | |
888c679b | 178 | # if _LIBC || STRERROR_R_CHAR_P |
a5d1e89b RM |
179 | s = __strerror_r (errnum, errbuf, sizeof errbuf); |
180 | # else | |
181 | if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0) | |
182 | s = errbuf; | |
183 | else | |
184 | s = 0; | |
185 | # endif | |
186 | #else | |
187 | s = strerror (errnum); | |
188 | #endif | |
189 | ||
190 | #if !_LIBC | |
191 | if (! s) | |
192 | s = _("Unknown system error"); | |
193 | #endif | |
194 | ||
e5e45b53 | 195 | #if _LIBC |
8a259a23 | 196 | __fxprintf (NULL, ": %s", s); |
10ffcd52 | 197 | #else |
a334319f | 198 | fprintf (stderr, ": %s", s); |
10ffcd52 | 199 | #endif |
a5d1e89b | 200 | } |
1fc0e331 | 201 | |
888c679b | 202 | static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3)) |
1fc0e331 UD |
203 | error_tail (int status, int errnum, const char *message, va_list args) |
204 | { | |
240c62a7 | 205 | #if _LIBC |
1fc0e331 UD |
206 | if (_IO_fwide (stderr, 0) > 0) |
207 | { | |
1fc0e331 UD |
208 | size_t len = strlen (message) + 1; |
209 | wchar_t *wmessage = NULL; | |
210 | mbstate_t st; | |
211 | size_t res; | |
212 | const char *tmp; | |
10ffcd52 | 213 | bool use_malloc = false; |
1fc0e331 | 214 | |
10ffcd52 | 215 | while (1) |
1fc0e331 | 216 | { |
10ffcd52 | 217 | if (__libc_use_alloca (len * sizeof (wchar_t))) |
1fc0e331 UD |
218 | wmessage = (wchar_t *) alloca (len * sizeof (wchar_t)); |
219 | else | |
220 | { | |
10ffcd52 | 221 | if (!use_malloc) |
1fc0e331 UD |
222 | wmessage = NULL; |
223 | ||
f9a06dc1 UD |
224 | wchar_t *p = (wchar_t *) realloc (wmessage, |
225 | len * sizeof (wchar_t)); | |
226 | if (p == NULL) | |
1fc0e331 | 227 | { |
f9a06dc1 | 228 | free (wmessage); |
34ef548a | 229 | fputws_unlocked (L"out of memory\n", stderr); |
1fc0e331 UD |
230 | return; |
231 | } | |
f9a06dc1 | 232 | wmessage = p; |
10ffcd52 | 233 | use_malloc = true; |
1fc0e331 UD |
234 | } |
235 | ||
236 | memset (&st, '\0', sizeof (st)); | |
f9a06dc1 | 237 | tmp = message; |
10ffcd52 UD |
238 | |
239 | res = mbsrtowcs (wmessage, &tmp, len, &st); | |
240 | if (res != len) | |
241 | break; | |
242 | ||
888c679b | 243 | if (__builtin_expect (len >= SIZE_MAX / sizeof (wchar_t) / 2, 0)) |
10ffcd52 | 244 | { |
a7ed1b0c | 245 | /* This really should not happen if everything is fine. */ |
10ffcd52 UD |
246 | res = (size_t) -1; |
247 | break; | |
248 | } | |
249 | ||
250 | len *= 2; | |
1fc0e331 | 251 | } |
1fc0e331 UD |
252 | |
253 | if (res == (size_t) -1) | |
10ffcd52 UD |
254 | { |
255 | /* The string cannot be converted. */ | |
256 | if (use_malloc) | |
9a528884 UD |
257 | { |
258 | free (wmessage); | |
259 | use_malloc = false; | |
260 | } | |
10ffcd52 UD |
261 | wmessage = (wchar_t *) L"???"; |
262 | } | |
1fc0e331 | 263 | |
51028f34 | 264 | __vfwprintf (stderr, wmessage, args); |
10ffcd52 UD |
265 | |
266 | if (use_malloc) | |
267 | free (wmessage); | |
1fc0e331 UD |
268 | } |
269 | else | |
240c62a7 | 270 | #endif |
1fc0e331 | 271 | vfprintf (stderr, message, args); |
1fc0e331 UD |
272 | va_end (args); |
273 | ||
274 | ++error_message_count; | |
275 | if (errnum) | |
a5d1e89b | 276 | print_errno_message (errnum); |
240c62a7 | 277 | #if _LIBC |
8a259a23 | 278 | __fxprintf (NULL, "\n"); |
240c62a7 | 279 | #else |
10ffcd52 | 280 | putc ('\n', stderr); |
240c62a7 | 281 | #endif |
1fc0e331 UD |
282 | fflush (stderr); |
283 | if (status) | |
284 | exit (status); | |
285 | } | |
1fc0e331 UD |
286 | |
287 | ||
196980f5 RM |
288 | /* Print the program name and error message MESSAGE, which is a printf-style |
289 | format string with optional args. | |
290 | If ERRNUM is nonzero, print its corresponding system error message. | |
291 | Exit with status STATUS if it is nonzero. */ | |
196980f5 | 292 | void |
196980f5 | 293 | error (int status, int errnum, const char *message, ...) |
196980f5 | 294 | { |
196980f5 | 295 | va_list args; |
196980f5 | 296 | |
8c620ae0 UD |
297 | #if defined _LIBC && defined __libc_ptf_call |
298 | /* We do not want this call to be cut short by a thread | |
299 | cancellation. Therefore disable cancellation for now. */ | |
300 | int state = PTHREAD_CANCEL_ENABLE; | |
e5d19c08 L |
301 | __libc_ptf_call (__pthread_setcancelstate, |
302 | (PTHREAD_CANCEL_DISABLE, &state), 0); | |
8c620ae0 UD |
303 | #endif |
304 | ||
888c679b | 305 | flush_stdout (); |
1fc0e331 | 306 | #ifdef _LIBC |
6293b803 | 307 | _IO_flockfile (stderr); |
1fc0e331 | 308 | #endif |
196980f5 RM |
309 | if (error_print_progname) |
310 | (*error_print_progname) (); | |
311 | else | |
1fc0e331 | 312 | { |
e5e45b53 | 313 | #if _LIBC |
8a259a23 | 314 | __fxprintf (NULL, "%s: ", program_name); |
10ffcd52 UD |
315 | #else |
316 | fprintf (stderr, "%s: ", program_name); | |
1fc0e331 | 317 | #endif |
1fc0e331 | 318 | } |
196980f5 | 319 | |
240c62a7 | 320 | va_start (args, message); |
1fc0e331 | 321 | error_tail (status, errnum, message, args); |
6293b803 UD |
322 | |
323 | #ifdef _LIBC | |
6293b803 | 324 | _IO_funlockfile (stderr); |
8c620ae0 | 325 | # ifdef __libc_ptf_call |
e5d19c08 | 326 | __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0); |
8c620ae0 | 327 | # endif |
6293b803 | 328 | #endif |
19bc17a9 RM |
329 | } |
330 | \f | |
331 | /* Sometimes we want to have at most one error per line. This | |
332 | variable controls whether this mode is selected or not. */ | |
333 | int error_one_per_line; | |
334 | ||
335 | void | |
19bc17a9 RM |
336 | error_at_line (int status, int errnum, const char *file_name, |
337 | unsigned int line_number, const char *message, ...) | |
19bc17a9 | 338 | { |
19bc17a9 | 339 | va_list args; |
19bc17a9 RM |
340 | |
341 | if (error_one_per_line) | |
342 | { | |
343 | static const char *old_file_name; | |
344 | static unsigned int old_line_number; | |
196980f5 | 345 | |
1fc0e331 UD |
346 | if (old_line_number == line_number |
347 | && (file_name == old_file_name | |
15cc7dd1 UD |
348 | || (old_file_name != NULL |
349 | && file_name != NULL | |
350 | && strcmp (old_file_name, file_name) == 0))) | |
888c679b | 351 | |
19bc17a9 RM |
352 | /* Simply return and print nothing. */ |
353 | return; | |
354 | ||
355 | old_file_name = file_name; | |
356 | old_line_number = line_number; | |
357 | } | |
358 | ||
8c620ae0 UD |
359 | #if defined _LIBC && defined __libc_ptf_call |
360 | /* We do not want this call to be cut short by a thread | |
361 | cancellation. Therefore disable cancellation for now. */ | |
362 | int state = PTHREAD_CANCEL_ENABLE; | |
e5d19c08 L |
363 | __libc_ptf_call (__pthread_setcancelstate, |
364 | (PTHREAD_CANCEL_DISABLE, &state), | |
8c620ae0 UD |
365 | 0); |
366 | #endif | |
367 | ||
888c679b | 368 | flush_stdout (); |
1fc0e331 | 369 | #ifdef _LIBC |
6293b803 | 370 | _IO_flockfile (stderr); |
1fc0e331 | 371 | #endif |
19bc17a9 RM |
372 | if (error_print_progname) |
373 | (*error_print_progname) (); | |
374 | else | |
1fc0e331 | 375 | { |
e5e45b53 | 376 | #if _LIBC |
8a259a23 | 377 | __fxprintf (NULL, "%s:", program_name); |
10ffcd52 UD |
378 | #else |
379 | fprintf (stderr, "%s:", program_name); | |
1fc0e331 | 380 | #endif |
1fc0e331 | 381 | } |
19bc17a9 | 382 | |
e5e45b53 | 383 | #if _LIBC |
cd8e60ce UD |
384 | __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ", |
385 | file_name, line_number); | |
10ffcd52 | 386 | #else |
cd8e60ce UD |
387 | fprintf (stderr, file_name != NULL ? "%s:%d: " : " ", |
388 | file_name, line_number); | |
1fc0e331 | 389 | #endif |
19bc17a9 | 390 | |
240c62a7 | 391 | va_start (args, message); |
1fc0e331 | 392 | error_tail (status, errnum, message, args); |
6293b803 UD |
393 | |
394 | #ifdef _LIBC | |
6293b803 | 395 | _IO_funlockfile (stderr); |
8c620ae0 | 396 | # ifdef __libc_ptf_call |
e5d19c08 | 397 | __libc_ptf_call (__pthread_setcancelstate, (state, NULL), 0); |
8c620ae0 | 398 | # endif |
6293b803 | 399 | #endif |
196980f5 | 400 | } |
2f6d1f1b UD |
401 | |
402 | #ifdef _LIBC | |
403 | /* Make the weak alias. */ | |
a44d2393 UD |
404 | # undef error |
405 | # undef error_at_line | |
2f6d1f1b UD |
406 | weak_alias (__error, error) |
407 | weak_alias (__error_at_line, error_at_line) | |
408 | #endif |