Update.
[glibc.git] / misc / error.c
blob9d01066169667b9542701143a1b22c140a16fcdd
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>
78 /* In GNU libc we want do not want to use the common name `error' directly.
79 Instead make it a weak alias. */
80 extern void __error (int status, int errnum, const char *message, ...)
81 __attribute__ ((__format__ (__printf__, 3, 4)));
82 extern void __error_at_line (int status, int errnum, const char *file_name,
83 unsigned int line_number, const char *message,
84 ...)
85 __attribute__ ((__format__ (__printf__, 5, 6)));;
86 # define error __error
87 # define error_at_line __error_at_line
89 # ifdef USE_IN_LIBIO
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)
94 # endif
96 #else /* not _LIBC */
98 /* The calling program should define program_name and set it to the
99 name of the executing program. */
100 extern char *program_name;
102 # ifdef HAVE_STRERROR_R
103 # define __strerror_r strerror_r
104 # else
105 # if HAVE_STRERROR
106 # ifndef strerror /* On some systems, strerror is a macro */
107 char *strerror ();
108 # endif
109 # else
110 static char *
111 private_strerror (errnum)
112 int errnum;
114 extern char *sys_errlist[];
115 extern int sys_nerr;
117 if (errnum > 0 && errnum <= sys_nerr)
118 return _(sys_errlist[errnum]);
119 return _("Unknown system error");
121 # define strerror private_strerror
122 # endif /* HAVE_STRERROR */
123 # endif /* HAVE_STRERROR_R */
124 #endif /* not _LIBC */
127 #ifdef VA_START
128 static void
129 error_tail (int status, int errnum, const char *message, va_list args)
131 # if HAVE_VPRINTF || _LIBC
132 # if _LIBC && USE_IN_LIBIO
133 if (_IO_fwide (stderr, 0) > 0)
135 # define ALLOCA_LIMIT 2000
136 size_t len = strlen (message) + 1;
137 wchar_t *wmessage = NULL;
138 mbstate_t st;
139 size_t res;
140 const char *tmp;
144 if (len < ALLOCA_LIMIT)
145 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
146 else
148 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
149 wmessage = NULL;
151 wmessage = (wchar_t *) realloc (wmessage,
152 len * sizeof (wchar_t));
154 if (wmessage == NULL)
156 fputws_unlocked (L"out of memory\n", stderr);
157 return;
161 memset (&st, '\0', sizeof (st));
162 tmp =message;
164 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
166 if (res == (size_t) -1)
167 /* The string cannot be converted. */
168 wmessage = (wchar_t *) L"???";
170 __vfwprintf (stderr, wmessage, args);
172 else
173 # endif
174 vfprintf (stderr, message, args);
175 # else
176 _doprnt (message, args, stderr);
177 # endif
178 va_end (args);
180 ++error_message_count;
181 if (errnum)
183 #if defined HAVE_STRERROR_R || _LIBC
184 char errbuf[1024];
185 char *s = __strerror_r (errnum, errbuf, sizeof errbuf);
186 # if _LIBC && USE_IN_LIBIO
187 if (_IO_fwide (stderr, 0) > 0)
188 __fwprintf (stderr, L": %s", s);
189 else
190 # endif
191 fprintf (stderr, ": %s", s);
192 #else
193 fprintf (stderr, ": %s", strerror (errnum));
194 #endif
196 #if _LIBC && USE_IN_LIBIO
197 if (_IO_fwide (stderr, 0) > 0)
198 putwc (L'\n', stderr);
199 else
200 #endif
201 putc ('\n', stderr);
202 fflush (stderr);
203 if (status)
204 exit (status);
206 #endif
209 /* Print the program name and error message MESSAGE, which is a printf-style
210 format string with optional args.
211 If ERRNUM is nonzero, print its corresponding system error message.
212 Exit with status STATUS if it is nonzero. */
213 /* VARARGS */
214 void
215 #if defined VA_START && __STDC__
216 error (int status, int errnum, const char *message, ...)
217 #else
218 error (status, errnum, message, va_alist)
219 int status;
220 int errnum;
221 char *message;
222 va_dcl
223 #endif
225 #ifdef VA_START
226 va_list args;
227 #endif
229 fflush (stdout);
230 #ifdef _LIBC
231 # ifdef USE_IN_LIBIO
232 _IO_flockfile (stderr);
233 # else
234 __flockfile (stderr);
235 # endif
236 #endif
237 if (error_print_progname)
238 (*error_print_progname) ();
239 else
241 #if _LIBC && USE_IN_LIBIO
242 if (_IO_fwide (stderr, 0) > 0)
243 __fwprintf (stderr, L"%s: ", program_name);
244 else
245 #endif
246 fprintf (stderr, "%s: ", program_name);
249 #ifdef VA_START
250 VA_START (args, message);
251 error_tail (status, errnum, message, args);
252 #else
253 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
255 ++error_message_count;
256 if (errnum)
257 fprintf (stderr, ": %s", strerror (errnum));
258 putc ('\n', stderr);
259 fflush (stderr);
260 if (status)
261 exit (status);
262 #endif
264 #ifdef _LIBC
265 # ifdef USE_IN_LIBIO
266 _IO_funlockfile (stderr);
267 # else
268 __funlockfile (stderr);
269 # endif
270 #endif
273 /* Sometimes we want to have at most one error per line. This
274 variable controls whether this mode is selected or not. */
275 int error_one_per_line;
277 void
278 #if defined VA_START && __STDC__
279 error_at_line (int status, int errnum, const char *file_name,
280 unsigned int line_number, const char *message, ...)
281 #else
282 error_at_line (status, errnum, file_name, line_number, message, va_alist)
283 int status;
284 int errnum;
285 const char *file_name;
286 unsigned int line_number;
287 char *message;
288 va_dcl
289 #endif
291 #ifdef VA_START
292 va_list args;
293 #endif
295 if (error_one_per_line)
297 static const char *old_file_name;
298 static unsigned int old_line_number;
300 if (old_line_number == line_number
301 && (file_name == old_file_name
302 || strcmp (old_file_name, file_name) == 0))
303 /* Simply return and print nothing. */
304 return;
306 old_file_name = file_name;
307 old_line_number = line_number;
310 fflush (stdout);
311 #ifdef _LIBC
312 # ifdef USE_IN_LIBIO
313 _IO_flockfile (stderr);
314 # else
315 __flockfile (stderr);
316 # endif
317 #endif
318 if (error_print_progname)
319 (*error_print_progname) ();
320 else
322 #if _LIBC && USE_IN_LIBIO
323 if (_IO_fwide (stderr, 0) > 0)
324 __fwprintf (stderr, L"%s: ", program_name);
325 else
326 #endif
327 fprintf (stderr, "%s:", program_name);
330 if (file_name != NULL)
332 #if _LIBC && USE_IN_LIBIO
333 if (_IO_fwide (stderr, 0) > 0)
334 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
335 else
336 #endif
337 fprintf (stderr, "%s:%d: ", file_name, line_number);
340 #ifdef VA_START
341 VA_START (args, message);
342 error_tail (status, errnum, message, args);
343 #else
344 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
346 ++error_message_count;
347 if (errnum)
348 fprintf (stderr, ": %s", strerror (errnum));
349 putc ('\n', stderr);
350 fflush (stderr);
351 if (status)
352 exit (status);
353 #endif
355 #ifdef _LIBC
356 # ifdef USE_IN_LIBIO
357 _IO_funlockfile (stderr);
358 # else
359 __funlockfile (stderr);
360 # endif
361 #endif
364 #ifdef _LIBC
365 /* Make the weak alias. */
366 # undef error
367 # undef error_at_line
368 weak_alias (__error, error)
369 weak_alias (__error_at_line, error_at_line)
370 #endif