Debian Codegraph @

lib/error.c

raw
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 /* Written by David MacKenzie <[email protected]>. */
20
21 #if !_LIBC
22 # include <config.h>
23 # define _GL_NO_INLINE_ERROR
24 # define __error_internal(status, err, fmt, args, flags) \
25 verror (status, err, fmt, args)
26 # define __error_at_line_internal(status, err, file, line, fmt, args, flags) \
27 verror_at_line (status, err, file, line, fmt, args)
28 # define error_tail(status, err, fmt, args, flags) \
29 error_tail (status, err, fmt, args)
30 #endif
31
32 #include <error.h>
33
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #if !_LIBC && ENABLE_NLS
40 # include "gettext.h"
41 # define _(msgid) gettext (msgid)
42 #endif
43
44 #ifdef _LIBC
45 # include <libintl.h>
46 # include <stdbool.h>
47 # include <stdint.h>
48 # include <wchar.h>
49 # define mbsrtowcs __mbsrtowcs
50 # define USE_UNLOCKED_IO 0
51 # define _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD(a, b)
52 # define _GL_ARG_NONNULL(a)
53 #endif
54
55 #if USE_UNLOCKED_IO
56 # include "unlocked-io.h"
57 #endif
58
59 #ifndef _
60 # define _(String) String
61 #endif
62
63 /* If NULL, error will flush stdout, then print on stderr the program
64 name, a colon and a space. Otherwise, error will call this
65 function without parameters instead. */
66 void (*error_print_progname) (void);
67
68 /* This variable is incremented each time 'error' is called. */
69 unsigned int error_message_count;
70
71 #ifdef _LIBC
72 /* In the GNU C library, there is a predefined variable for this. */
73
74 # define program_name program_invocation_name
75 # include <errno.h>
76 # include <limits.h>
77 # include <libio/libioP.h>
78
79 /* In GNU libc we want do not want to use the common name 'error' directly.
80 Instead make it a weak alias. */
81 extern void __error (int status, int errnum, const char *message, ...)
82 __attribute__ ((__format__ (__printf__, 3, 4)));
83 extern void __error_at_line (int status, int errnum, const char *file_name,
84 unsigned int line_number, const char *message,
85 ...)
86 __attribute__ ((__format__ (__printf__, 5, 6)));
87 # define error __error
88 # define error_at_line __error_at_line
89
90 # include <libio/iolibio.h>
91 # define fflush(s) _IO_fflush (s)
92 # undef putc
93 # define putc(c, fp) _IO_putc (c, fp)
94
95 # include <libc-lock.h>
96
97 #else /* not _LIBC */
98
99 # include <fcntl.h>
100 # include <unistd.h>
101
102 # if defined _WIN32 && ! defined __CYGWIN__
103 /* Get declarations of the native Windows API functions. */
104 # define WIN32_LEAN_AND_MEAN
105 # include <windows.h>
106 /* Get _get_osfhandle. */
107 # if GNULIB_MSVC_NOTHROW
108 # include "msvc-nothrow.h"
109 # else
110 # include <io.h>
111 # endif
112 # endif
113
114 /* The gnulib override of fcntl is not needed in this file. */
115 # undef fcntl
116
117 # if !(GNULIB_STRERROR_R_POSIX || HAVE_DECL_STRERROR_R)
118 # ifndef HAVE_DECL_STRERROR_R
119 "this configure-time declaration test was not run"
120 # endif
121 # if STRERROR_R_CHAR_P
122 char *strerror_r (int errnum, char *buf, size_t buflen);
123 # else
124 int strerror_r (int errnum, char *buf, size_t buflen);
125 # endif
126 # endif
127
128 # define program_name getprogname ()
129
130 # if GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r
131 # define __strerror_r strerror_r
132 # endif /* GNULIB_STRERROR_R_POSIX || HAVE_STRERROR_R || defined strerror_r */
133
134 # if GNULIB_defined_verror
135 # undef verror
136 # endif
137 # if GNULIB_defined_verror_at_line
138 # undef verror_at_line
139 # endif
140 #endif /* not _LIBC */
141
142 #if !_LIBC
143 /* Return non-zero if FD is open. */
144 static int
145 is_open (int fd)
146 {
147 # if defined _WIN32 && ! defined __CYGWIN__
148 /* On native Windows: The initial state of unassigned standard file
149 descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
150 There is no fcntl, and the gnulib replacement fcntl does not support
151 F_GETFL. */
152 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
153 # else
154 # ifndef F_GETFL
155 # error Please port fcntl to your platform
156 # endif
157 return 0 <= fcntl (fd, F_GETFL);
158 # endif
159 }
160 #endif
161
162 static void
163 flush_stdout (void)
164 {
165 #if !_LIBC
166 int stdout_fd;
167
168 # if GNULIB_FREOPEN_SAFER || GNULIB_XSTDOPEN
169 /* Gnulib's freopen-safer and/or xstdopen modules normally ensure that
170 fileno (stdout) == 1
171 whenever stdout is open. */
172 stdout_fd = STDOUT_FILENO;
173 # else
174 /* POSIX states that fileno (stdout) after fclose is unspecified. But in
175 practice it is not a problem, because stdout is statically allocated and
176 the fd of a FILE stream is stored as a field in its allocated memory. */
177 stdout_fd = fileno (stdout);
178 # endif
179 /* POSIX states that fflush (stdout) after fclose is unspecified; it
180 is safe in glibc, but not on all other platforms. fflush (NULL)
181 is always defined, but too draconian. */
182 if (0 <= stdout_fd && is_open (stdout_fd))
183 #endif
184 fflush (stdout);
185 }
186
187 static void
188 print_errno_message (int errnum)
189 {
190 char const *s;
191
192 #if _LIBC || GNULIB_STRERROR_R_POSIX || defined HAVE_STRERROR_R
193 char errbuf[1024];
194 # if _LIBC || (!GNULIB_STRERROR_R_POSIX && STRERROR_R_CHAR_P)
195 s = __strerror_r (errnum, errbuf, sizeof errbuf);
196 # else
197 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
198 s = errbuf;
199 else
200 s = 0;
201 # endif
202 #else
203 s = strerror (errnum);
204 #endif
205
206 #if !_LIBC
207 if (! s)
208 s = _("Unknown system error");
209 #endif
210
211 #if _LIBC
212 __fxprintf (NULL, ": %s", s);
213 #else
214 fprintf (stderr, ": %s", s);
215 #endif
216 }
217
218 static void _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (3, 0) _GL_ARG_NONNULL ((3))
219 error_tail (int status, int errnum, const char *message, va_list args,
220 unsigned int mode_flags)
221 {
222 #if _LIBC
223 int ret = __vfxprintf (stderr, message, args, mode_flags);
224 if (ret < 0 && errno == ENOMEM && _IO_fwide (stderr, 0) > 0)
225 /* Leave a trace in case the heap allocation of the message string
226 failed. */
227 fputws_unlocked (L"out of memory\n", stderr);
228 #else
229 vfprintf (stderr, message, args);
230 #endif
231
232 ++error_message_count;
233 if (errnum)
234 print_errno_message (errnum);
235 #if _LIBC
236 __fxprintf (NULL, "\n");
237 #else
238 putc ('\n', stderr);
239 #endif
240 fflush (stderr);
241 if (status)
242 exit (status);
243 }
244
245
246 /* Print the program name and error message MESSAGE, which is a printf-style
247 format string with optional args.
248 If ERRNUM is nonzero, print its corresponding system error message.
249 Exit with status STATUS if it is nonzero. */
250 void
251 __error_internal (int status, int errnum, const char *message,
252 va_list args, unsigned int mode_flags)
253 {
254 #if defined _LIBC
255 /* We do not want this call to be cut short by a thread
256 cancellation. Therefore disable cancellation for now. */
257 int state = PTHREAD_CANCEL_ENABLE;
258 __pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
259 #endif
260
261 flush_stdout ();
262 #ifdef _LIBC
263 _IO_flockfile (stderr);
264 #endif
265 if (error_print_progname)
266 (*error_print_progname) ();
267 else
268 {
269 #if _LIBC
270 __fxprintf (NULL, "%s: ", program_name);
271 #else
272 fprintf (stderr, "%s: ", program_name);
273 #endif
274 }
275
276 error_tail (status, errnum, message, args, mode_flags);
277
278 #ifdef _LIBC
279 _IO_funlockfile (stderr);
280 __pthread_setcancelstate (state, NULL);
281 #endif
282 }
283
284 void
285 error (int status, int errnum, const char *message, ...)
286 {
287 va_list ap;
288 va_start (ap, message);
289 __error_internal (status, errnum, message, ap, 0);
290 va_end (ap);
291 }
292
293 /* Sometimes we want to have at most one error per line. This
294 variable controls whether this mode is selected or not. */
295 int error_one_per_line;
296
297 void
298 __error_at_line_internal (int status, int errnum, const char *file_name,
299 unsigned int line_number, const char *message,
300 va_list args, unsigned int mode_flags)
301 {
302 if (error_one_per_line)
303 {
304 static const char *old_file_name;
305 static unsigned int old_line_number;
306
307 if (old_line_number == line_number
308 && (file_name == old_file_name
309 || (old_file_name != NULL
310 && file_name != NULL
311 && strcmp (old_file_name, file_name) == 0)))
312
313 /* Simply return and print nothing. */
314 return;
315
316 old_file_name = file_name;
317 old_line_number = line_number;
318 }
319
320 #if defined _LIBC
321 /* We do not want this call to be cut short by a thread
322 cancellation. Therefore disable cancellation for now. */
323 int state = PTHREAD_CANCEL_ENABLE;
324 __pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
325 #endif
326
327 flush_stdout ();
328 #ifdef _LIBC
329 _IO_flockfile (stderr);
330 #endif
331 if (error_print_progname)
332 (*error_print_progname) ();
333 else
334 {
335 #if _LIBC
336 __fxprintf (NULL, "%s:", program_name);
337 #else
338 fprintf (stderr, "%s:", program_name);
339 #endif
340 }
341
342 #if _LIBC
343 __fxprintf (NULL, file_name != NULL ? "%s:%u: " : " ",
344 file_name, line_number);
345 #else
346 fprintf (stderr, file_name != NULL ? "%s:%u: " : " ",
347 file_name, line_number);
348 #endif
349
350 error_tail (status, errnum, message, args, mode_flags);
351
352 #ifdef _LIBC
353 _IO_funlockfile (stderr);
354 __pthread_setcancelstate (state, NULL);
355 #endif
356 }
357
358 void
359 error_at_line (int status, int errnum, const char *file_name,
360 unsigned int line_number, const char *message, ...)
361 {
362 va_list ap;
363 va_start (ap, message);
364 __error_at_line_internal (status, errnum, file_name, line_number,
365 message, ap, 0);
366 va_end (ap);
367 }
368
369 #ifdef _LIBC
370 /* Make the weak alias. */
371 # undef error
372 # undef error_at_line
373 weak_alias (__error, error)
374 weak_alias (__error_at_line, error_at_line)
375 #endif