2.3.90-3
[glibc.git] / misc / error.c
blob29060e94de382b98e6b5a691119349064d071b46
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2004, 2005 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 <stdio.h>
27 #include <libintl.h>
28 #ifdef _LIBC
29 # include <wchar.h>
30 # define mbsrtowcs __mbsrtowcs
31 #endif
33 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
34 # if __STDC__
35 # include <stdarg.h>
36 # define VA_START(args, lastarg) va_start(args, lastarg)
37 # else
38 # include <varargs.h>
39 # define VA_START(args, lastarg) va_start(args)
40 # endif
41 #else
42 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
43 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
44 #endif
46 #if STDC_HEADERS || _LIBC
47 # include <stdlib.h>
48 # include <string.h>
49 #else
50 void exit ();
51 #endif
53 #include "error.h"
55 #ifndef _
56 # define _(String) String
57 #endif
59 /* If NULL, error will flush stdout, then print on stderr the program
60 name, a colon and a space. Otherwise, error will call this
61 function without parameters instead. */
62 void (*error_print_progname) (
63 #if __STDC__ - 0
64 void
65 #endif
68 /* This variable is incremented each time `error' is called. */
69 unsigned int error_message_count;
71 #ifdef _LIBC
72 /* In the GNU C library, there is a predefined variable for this. */
74 # define program_name program_invocation_name
75 # include <errno.h>
76 # include <limits.h>
77 # include <libio/libioP.h>
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
90 # include <libio/iolibio.h>
91 # define fflush(s) INTUSE(_IO_fflush) (s)
92 # undef putc
93 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
95 # include <bits/libc-lock.h>
97 #else /* not _LIBC */
99 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
100 # ifndef HAVE_DECL_STRERROR_R
101 "this configure-time declaration test was not run"
102 # endif
103 char *strerror_r ();
104 # endif
106 /* The calling program should define program_name and set it to the
107 name of the executing program. */
108 extern char *program_name;
110 # if HAVE_STRERROR_R || defined strerror_r
111 # define __strerror_r strerror_r
112 # else
113 # if HAVE_STRERROR
114 # ifndef HAVE_DECL_STRERROR
115 "this configure-time declaration test was not run"
116 # endif
117 # if !HAVE_DECL_STRERROR
118 char *strerror ();
119 # endif
120 # else
121 static char *
122 private_strerror (int errnum)
124 extern char *sys_errlist[];
125 extern int sys_nerr;
127 if (errnum > 0 && errnum <= sys_nerr)
128 return _(sys_errlist[errnum]);
129 return _("Unknown system error");
131 # define strerror private_strerror
132 # endif /* HAVE_STRERROR */
133 # endif /* HAVE_STRERROR_R || defined strerror_r */
134 #endif /* not _LIBC */
136 static void
137 print_errno_message (int errnum)
139 char const *s;
141 #if defined HAVE_STRERROR_R || _LIBC
142 char errbuf[1024];
143 # if STRERROR_R_CHAR_P || _LIBC
144 s = __strerror_r (errnum, errbuf, sizeof errbuf);
145 # else
146 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
147 s = errbuf;
148 else
149 s = 0;
150 # endif
151 #else
152 s = strerror (errnum);
153 #endif
155 #if !_LIBC
156 if (! s)
157 s = _("Unknown system error");
158 #endif
160 #if _LIBC
161 __fxprintf (NULL, ": %s", s);
162 #else
163 fprintf (stderr, ": %s", s);
164 #endif
167 #ifdef VA_START
168 static void
169 error_tail (int status, int errnum, const char *message, va_list args)
171 # if HAVE_VPRINTF || _LIBC
172 # if _LIBC
173 if (_IO_fwide (stderr, 0) > 0)
175 # define ALLOCA_LIMIT 2000
176 size_t len = strlen (message) + 1;
177 wchar_t *wmessage = NULL;
178 mbstate_t st;
179 size_t res;
180 const char *tmp;
181 bool use_malloc = false;
183 while (1)
185 if (__libc_use_alloca (len * sizeof (wchar_t)))
186 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
187 else
189 if (!use_malloc)
190 wmessage = NULL;
192 wchar_t *p = (wchar_t *) realloc (wmessage,
193 len * sizeof (wchar_t));
194 if (p == NULL)
196 free (wmessage);
197 fputws_unlocked (L"out of memory\n", stderr);
198 return;
200 wmessage = p;
201 use_malloc = true;
204 memset (&st, '\0', sizeof (st));
205 tmp = message;
207 res = mbsrtowcs (wmessage, &tmp, len, &st);
208 if (res != len)
209 break;
211 if (__builtin_expect (len >= SIZE_MAX / 2, 0))
213 /* This reallyy should not happen if everything is fine. */
214 res = (size_t) -1;
215 break;
218 len *= 2;
221 if (res == (size_t) -1)
223 /* The string cannot be converted. */
224 if (use_malloc)
225 free (wmessage);
226 wmessage = (wchar_t *) L"???";
229 __vfwprintf (stderr, wmessage, args);
231 if (use_malloc)
232 free (wmessage);
234 else
235 # endif
236 vfprintf (stderr, message, args);
237 # else
238 _doprnt (message, args, stderr);
239 # endif
240 va_end (args);
242 ++error_message_count;
243 if (errnum)
244 print_errno_message (errnum);
245 # if _LIBC
246 __fxprintf (NULL, "\n");
247 # else
248 putc ('\n', stderr);
249 # endif
250 fflush (stderr);
251 if (status)
252 exit (status);
254 #endif
257 /* Print the program name and error message MESSAGE, which is a printf-style
258 format string with optional args.
259 If ERRNUM is nonzero, print its corresponding system error message.
260 Exit with status STATUS if it is nonzero. */
261 /* VARARGS */
262 void
263 #if defined VA_START && __STDC__
264 error (int status, int errnum, const char *message, ...)
265 #else
266 error (status, errnum, message, va_alist)
267 int status;
268 int errnum;
269 char *message;
270 va_dcl
271 #endif
273 #ifdef VA_START
274 va_list args;
275 #endif
277 #if defined _LIBC && defined __libc_ptf_call
278 /* We do not want this call to be cut short by a thread
279 cancellation. Therefore disable cancellation for now. */
280 int state = PTHREAD_CANCEL_ENABLE;
281 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
283 #endif
285 fflush (stdout);
286 #ifdef _LIBC
287 _IO_flockfile (stderr);
288 #endif
289 if (error_print_progname)
290 (*error_print_progname) ();
291 else
293 #if _LIBC
294 __fxprintf (NULL, "%s: ", program_name);
295 #else
296 fprintf (stderr, "%s: ", program_name);
297 #endif
300 #ifdef VA_START
301 VA_START (args, message);
302 error_tail (status, errnum, message, args);
303 #else
304 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
306 ++error_message_count;
307 if (errnum)
308 print_errno_message (errnum);
309 putc ('\n', stderr);
310 fflush (stderr);
311 if (status)
312 exit (status);
313 #endif
315 #ifdef _LIBC
316 _IO_funlockfile (stderr);
317 # ifdef __libc_ptf_call
318 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
319 # endif
320 #endif
323 /* Sometimes we want to have at most one error per line. This
324 variable controls whether this mode is selected or not. */
325 int error_one_per_line;
327 void
328 #if defined VA_START && __STDC__
329 error_at_line (int status, int errnum, const char *file_name,
330 unsigned int line_number, const char *message, ...)
331 #else
332 error_at_line (status, errnum, file_name, line_number, message, va_alist)
333 int status;
334 int errnum;
335 const char *file_name;
336 unsigned int line_number;
337 char *message;
338 va_dcl
339 #endif
341 #ifdef VA_START
342 va_list args;
343 #endif
345 if (error_one_per_line)
347 static const char *old_file_name;
348 static unsigned int old_line_number;
350 if (old_line_number == line_number
351 && (file_name == old_file_name
352 || strcmp (old_file_name, file_name) == 0))
353 /* Simply return and print nothing. */
354 return;
356 old_file_name = file_name;
357 old_line_number = line_number;
360 #if defined _LIBC && defined __libc_ptf_call
361 /* We do not want this call to be cut short by a thread
362 cancellation. Therefore disable cancellation for now. */
363 int state = PTHREAD_CANCEL_ENABLE;
364 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
366 #endif
368 fflush (stdout);
369 #ifdef _LIBC
370 _IO_flockfile (stderr);
371 #endif
372 if (error_print_progname)
373 (*error_print_progname) ();
374 else
376 #if _LIBC
377 __fxprintf (NULL, "%s:", program_name);
378 #else
379 fprintf (stderr, "%s:", program_name);
380 #endif
383 if (file_name != NULL)
385 #if _LIBC
386 __fxprintf (NULL, "%s:%d: ", file_name, line_number);
387 #else
388 fprintf (stderr, "%s:%d: ", file_name, line_number);
389 #endif
392 #ifdef VA_START
393 VA_START (args, message);
394 error_tail (status, errnum, message, args);
395 #else
396 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
398 ++error_message_count;
399 if (errnum)
400 print_errno_message (errnum);
401 putc ('\n', stderr);
402 fflush (stderr);
403 if (status)
404 exit (status);
405 #endif
407 #ifdef _LIBC
408 _IO_funlockfile (stderr);
409 # ifdef __libc_ptf_call
410 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
411 # endif
412 #endif
415 #ifdef _LIBC
416 /* Make the weak alias. */
417 # undef error
418 # undef error_at_line
419 weak_alias (__error, error)
420 weak_alias (__error_at_line, error_at_line)
421 #endif