* elf/dl-load.c (open_verify): In case VALID_ELF_HEADER is
[glibc.git] / misc / error.c
blob85d1cffbf931536978a0076d275d1a160b695ab9
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2005, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #ifdef _LIBC
32 # include <libintl.h>
33 # include <stdbool.h>
34 # include <stdint.h>
35 # include <wchar.h>
36 # define mbsrtowcs __mbsrtowcs
37 #endif
39 #include "error.h"
41 #ifndef _
42 # define _(String) String
43 #endif
45 /* If NULL, error will flush stdout, then print on stderr the program
46 name, a colon and a space. Otherwise, error will call this
47 function without parameters instead. */
48 void (*error_print_progname) (void);
50 /* This variable is incremented each time `error' is called. */
51 unsigned int error_message_count;
53 #ifdef _LIBC
54 /* In the GNU C library, there is a predefined variable for this. */
56 # define program_name program_invocation_name
57 # include <errno.h>
58 # include <limits.h>
59 # include <libio/libioP.h>
61 /* In GNU libc we want do not want to use the common name `error' directly.
62 Instead make it a weak alias. */
63 extern void __error (int status, int errnum, const char *message, ...)
64 __attribute__ ((__format__ (__printf__, 3, 4)));
65 extern void __error_at_line (int status, int errnum, const char *file_name,
66 unsigned int line_number, const char *message,
67 ...)
68 __attribute__ ((__format__ (__printf__, 5, 6)));;
69 # define error __error
70 # define error_at_line __error_at_line
72 # include <libio/iolibio.h>
73 # define fflush(s) INTUSE(_IO_fflush) (s)
74 # undef putc
75 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
77 # include <bits/libc-lock.h>
79 #else /* not _LIBC */
81 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
82 # ifndef HAVE_DECL_STRERROR_R
83 "this configure-time declaration test was not run"
84 # endif
85 char *strerror_r ();
86 # endif
88 /* The calling program should define program_name and set it to the
89 name of the executing program. */
90 extern char *program_name;
92 # if HAVE_STRERROR_R || defined strerror_r
93 # define __strerror_r strerror_r
94 # endif /* HAVE_STRERROR_R || defined strerror_r */
95 #endif /* not _LIBC */
97 static void
98 print_errno_message (int errnum)
100 char const *s;
102 #if defined HAVE_STRERROR_R || _LIBC
103 char errbuf[1024];
104 # if STRERROR_R_CHAR_P || _LIBC
105 s = __strerror_r (errnum, errbuf, sizeof errbuf);
106 # else
107 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
108 s = errbuf;
109 else
110 s = 0;
111 # endif
112 #else
113 s = strerror (errnum);
114 #endif
116 #if !_LIBC
117 if (! s)
118 s = _("Unknown system error");
119 #endif
121 #if _LIBC
122 __fxprintf (NULL, ": %s", s);
123 #else
124 fprintf (stderr, ": %s", s);
125 #endif
128 static void
129 error_tail (int status, int errnum, const char *message, va_list args)
131 #if _LIBC
132 if (_IO_fwide (stderr, 0) > 0)
134 # define ALLOCA_LIMIT 2000
135 size_t len = strlen (message) + 1;
136 wchar_t *wmessage = NULL;
137 mbstate_t st;
138 size_t res;
139 const char *tmp;
140 bool use_malloc = false;
142 while (1)
144 if (__libc_use_alloca (len * sizeof (wchar_t)))
145 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
146 else
148 if (!use_malloc)
149 wmessage = NULL;
151 wchar_t *p = (wchar_t *) realloc (wmessage,
152 len * sizeof (wchar_t));
153 if (p == NULL)
155 free (wmessage);
156 fputws_unlocked (L"out of memory\n", stderr);
157 return;
159 wmessage = p;
160 use_malloc = true;
163 memset (&st, '\0', sizeof (st));
164 tmp = message;
166 res = mbsrtowcs (wmessage, &tmp, len, &st);
167 if (res != len)
168 break;
170 if (__builtin_expect (len >= SIZE_MAX / 2, 0))
172 /* This really should not happen if everything is fine. */
173 res = (size_t) -1;
174 break;
177 len *= 2;
180 if (res == (size_t) -1)
182 /* The string cannot be converted. */
183 if (use_malloc)
185 free (wmessage);
186 use_malloc = false;
188 wmessage = (wchar_t *) L"???";
191 __vfwprintf (stderr, wmessage, args);
193 if (use_malloc)
194 free (wmessage);
196 else
197 #endif
198 vfprintf (stderr, message, args);
199 va_end (args);
201 ++error_message_count;
202 if (errnum)
203 print_errno_message (errnum);
204 #if _LIBC
205 __fxprintf (NULL, "\n");
206 #else
207 putc ('\n', stderr);
208 #endif
209 fflush (stderr);
210 if (status)
211 exit (status);
215 /* Print the program name and error message MESSAGE, which is a printf-style
216 format string with optional args.
217 If ERRNUM is nonzero, print its corresponding system error message.
218 Exit with status STATUS if it is nonzero. */
219 void
220 error (int status, int errnum, const char *message, ...)
222 va_list args;
224 #if defined _LIBC && defined __libc_ptf_call
225 /* We do not want this call to be cut short by a thread
226 cancellation. Therefore disable cancellation for now. */
227 int state = PTHREAD_CANCEL_ENABLE;
228 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
230 #endif
232 fflush (stdout);
233 #ifdef _LIBC
234 _IO_flockfile (stderr);
235 #endif
236 if (error_print_progname)
237 (*error_print_progname) ();
238 else
240 #if _LIBC
241 __fxprintf (NULL, "%s: ", program_name);
242 #else
243 fprintf (stderr, "%s: ", program_name);
244 #endif
247 va_start (args, message);
248 error_tail (status, errnum, message, args);
250 #ifdef _LIBC
251 _IO_funlockfile (stderr);
252 # ifdef __libc_ptf_call
253 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
254 # endif
255 #endif
258 /* Sometimes we want to have at most one error per line. This
259 variable controls whether this mode is selected or not. */
260 int error_one_per_line;
262 void
263 error_at_line (int status, int errnum, const char *file_name,
264 unsigned int line_number, const char *message, ...)
266 va_list args;
268 if (error_one_per_line)
270 static const char *old_file_name;
271 static unsigned int old_line_number;
273 if (old_line_number == line_number
274 && (file_name == old_file_name
275 || strcmp (old_file_name, file_name) == 0))
276 /* Simply return and print nothing. */
277 return;
279 old_file_name = file_name;
280 old_line_number = line_number;
283 #if defined _LIBC && defined __libc_ptf_call
284 /* We do not want this call to be cut short by a thread
285 cancellation. Therefore disable cancellation for now. */
286 int state = PTHREAD_CANCEL_ENABLE;
287 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
289 #endif
291 fflush (stdout);
292 #ifdef _LIBC
293 _IO_flockfile (stderr);
294 #endif
295 if (error_print_progname)
296 (*error_print_progname) ();
297 else
299 #if _LIBC
300 __fxprintf (NULL, "%s:", program_name);
301 #else
302 fprintf (stderr, "%s:", program_name);
303 #endif
306 #if _LIBC
307 __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
308 file_name, line_number);
309 #else
310 fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
311 file_name, line_number);
312 #endif
314 va_start (args, message);
315 error_tail (status, errnum, message, args);
317 #ifdef _LIBC
318 _IO_funlockfile (stderr);
319 # ifdef __libc_ptf_call
320 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
321 # endif
322 #endif
325 #ifdef _LIBC
326 /* Make the weak alias. */
327 # undef error
328 # undef error_at_line
329 weak_alias (__error, error)
330 weak_alias (__error_at_line, error_at_line)
331 #endif