Update.
[glibc.git] / misc / error.c
blob0e122970015ffa58d5db05702b0c243177cfd5c8
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000, 2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. Its master source is NOT part of
4 the C library, however. The master source lives in /gd/gnu/lib.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include <stdio.h>
28 #include <libintl.h>
29 #ifdef _LIBC
30 # include <wchar.h>
31 # define mbsrtowcs __mbsrtowcs
32 #endif
34 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
35 # if __STDC__
36 # include <stdarg.h>
37 # define VA_START(args, lastarg) va_start(args, lastarg)
38 # else
39 # include <varargs.h>
40 # define VA_START(args, lastarg) va_start(args)
41 # endif
42 #else
43 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
44 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
45 #endif
47 #if STDC_HEADERS || _LIBC
48 # include <stdlib.h>
49 # include <string.h>
50 #else
51 void exit ();
52 #endif
54 #include "error.h"
56 #ifndef _
57 # define _(String) String
58 #endif
60 /* If NULL, error will flush stdout, then print on stderr the program
61 name, a colon and a space. Otherwise, error will call this
62 function without parameters instead. */
63 void (*error_print_progname) (
64 #if __STDC__ - 0
65 void
66 #endif
69 /* This variable is incremented each time `error' is called. */
70 unsigned int error_message_count;
72 #ifdef _LIBC
73 /* In the GNU C library, there is a predefined variable for this. */
75 # define program_name program_invocation_name
76 # include <errno.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 # ifdef USE_IN_LIBIO
91 # include <libio/iolibio.h>
92 # define fflush(s) INTUSE(_IO_fflush) (s)
93 # undef putc
94 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
95 # endif
97 #else /* not _LIBC */
99 /* The calling program should define program_name and set it to the
100 name of the executing program. */
101 extern char *program_name;
103 # ifdef HAVE_STRERROR_R
104 # define __strerror_r strerror_r
105 # else
106 # if HAVE_STRERROR
107 # ifndef strerror /* On some systems, strerror is a macro */
108 char *strerror ();
109 # endif
110 # else
111 static char *
112 private_strerror (errnum)
113 int errnum;
115 extern char *sys_errlist[];
116 extern int sys_nerr;
118 if (errnum > 0 && errnum <= sys_nerr)
119 return _(sys_errlist[errnum]);
120 return _("Unknown system error");
122 # define strerror private_strerror
123 # endif /* HAVE_STRERROR */
124 # endif /* HAVE_STRERROR_R */
125 #endif /* not _LIBC */
128 #ifdef VA_START
129 static void
130 error_tail (int status, int errnum, const char *message, va_list args)
132 # if HAVE_VPRINTF || _LIBC
133 # if _LIBC && USE_IN_LIBIO
134 if (_IO_fwide (stderr, 0) > 0)
136 # define ALLOCA_LIMIT 2000
137 size_t len = strlen (message) + 1;
138 wchar_t *wmessage = NULL;
139 mbstate_t st;
140 size_t res;
141 const char *tmp;
145 if (len < ALLOCA_LIMIT)
146 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
147 else
149 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
150 wmessage = NULL;
152 wmessage = (wchar_t *) realloc (wmessage,
153 len * sizeof (wchar_t));
155 if (wmessage == NULL)
157 fputws_unlocked (L"out of memory\n", stderr);
158 return;
162 memset (&st, '\0', sizeof (st));
163 tmp =message;
165 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
167 if (res == (size_t) -1)
168 /* The string cannot be converted. */
169 wmessage = (wchar_t *) L"???";
171 __vfwprintf (stderr, wmessage, args);
173 else
174 # endif
175 vfprintf (stderr, message, args);
176 # else
177 _doprnt (message, args, stderr);
178 # endif
179 va_end (args);
181 ++error_message_count;
182 if (errnum)
184 #if defined HAVE_STRERROR_R || _LIBC
185 char errbuf[1024];
186 char *s = __strerror_r (errnum, errbuf, sizeof errbuf);
187 # if _LIBC && USE_IN_LIBIO
188 if (_IO_fwide (stderr, 0) > 0)
189 __fwprintf (stderr, L": %s", s);
190 else
191 # endif
192 fprintf (stderr, ": %s", s);
193 #else
194 fprintf (stderr, ": %s", strerror (errnum));
195 #endif
197 #if _LIBC && USE_IN_LIBIO
198 if (_IO_fwide (stderr, 0) > 0)
199 putwc (L'\n', stderr);
200 else
201 #endif
202 putc ('\n', stderr);
203 fflush (stderr);
204 if (status)
205 exit (status);
207 #endif
210 /* Print the program name and error message MESSAGE, which is a printf-style
211 format string with optional args.
212 If ERRNUM is nonzero, print its corresponding system error message.
213 Exit with status STATUS if it is nonzero. */
214 /* VARARGS */
215 void
216 #if defined VA_START && __STDC__
217 error (int status, int errnum, const char *message, ...)
218 #else
219 error (status, errnum, message, va_alist)
220 int status;
221 int errnum;
222 char *message;
223 va_dcl
224 #endif
226 #ifdef VA_START
227 va_list args;
228 #endif
230 fflush (stdout);
231 #ifdef _LIBC
232 # ifdef USE_IN_LIBIO
233 _IO_flockfile (stderr);
234 # else
235 __flockfile (stderr);
236 # endif
237 #endif
238 if (error_print_progname)
239 (*error_print_progname) ();
240 else
242 #if _LIBC && USE_IN_LIBIO
243 if (_IO_fwide (stderr, 0) > 0)
244 __fwprintf (stderr, L"%s: ", program_name);
245 else
246 #endif
247 fprintf (stderr, "%s: ", program_name);
250 #ifdef VA_START
251 VA_START (args, message);
252 error_tail (status, errnum, message, args);
253 #else
254 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
256 ++error_message_count;
257 if (errnum)
258 fprintf (stderr, ": %s", strerror (errnum));
259 putc ('\n', stderr);
260 fflush (stderr);
261 if (status)
262 exit (status);
263 #endif
265 #ifdef _LIBC
266 # ifdef USE_IN_LIBIO
267 _IO_funlockfile (stderr);
268 # else
269 __funlockfile (stderr);
270 # endif
271 #endif
274 /* Sometimes we want to have at most one error per line. This
275 variable controls whether this mode is selected or not. */
276 int error_one_per_line;
278 void
279 #if defined VA_START && __STDC__
280 error_at_line (int status, int errnum, const char *file_name,
281 unsigned int line_number, const char *message, ...)
282 #else
283 error_at_line (status, errnum, file_name, line_number, message, va_alist)
284 int status;
285 int errnum;
286 const char *file_name;
287 unsigned int line_number;
288 char *message;
289 va_dcl
290 #endif
292 #ifdef VA_START
293 va_list args;
294 #endif
296 if (error_one_per_line)
298 static const char *old_file_name;
299 static unsigned int old_line_number;
301 if (old_line_number == line_number
302 && (file_name == old_file_name
303 || strcmp (old_file_name, file_name) == 0))
304 /* Simply return and print nothing. */
305 return;
307 old_file_name = file_name;
308 old_line_number = line_number;
311 fflush (stdout);
312 #ifdef _LIBC
313 # ifdef USE_IN_LIBIO
314 _IO_flockfile (stderr);
315 # else
316 __flockfile (stderr);
317 # endif
318 #endif
319 if (error_print_progname)
320 (*error_print_progname) ();
321 else
323 #if _LIBC && USE_IN_LIBIO
324 if (_IO_fwide (stderr, 0) > 0)
325 __fwprintf (stderr, L"%s: ", program_name);
326 else
327 #endif
328 fprintf (stderr, "%s:", program_name);
331 if (file_name != NULL)
333 #if _LIBC && USE_IN_LIBIO
334 if (_IO_fwide (stderr, 0) > 0)
335 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
336 else
337 #endif
338 fprintf (stderr, "%s:%d: ", file_name, line_number);
341 #ifdef VA_START
342 VA_START (args, message);
343 error_tail (status, errnum, message, args);
344 #else
345 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
347 ++error_message_count;
348 if (errnum)
349 fprintf (stderr, ": %s", strerror (errnum));
350 putc ('\n', stderr);
351 fflush (stderr);
352 if (status)
353 exit (status);
354 #endif
356 #ifdef _LIBC
357 # ifdef USE_IN_LIBIO
358 _IO_funlockfile (stderr);
359 # else
360 __funlockfile (stderr);
361 # endif
362 #endif
365 #ifdef _LIBC
366 /* Make the weak alias. */
367 # undef error
368 # undef error_at_line
369 weak_alias (__error, error)
370 weak_alias (__error_at_line, error_at_line)
371 #endif