*** empty log message ***
[coreutils.git] / lib / error.c
blobc02abcee8a65752fb4de153accd9d08a0c063418
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000, 2001 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 #if HAVE_LIBINTL_H
29 # include <libintl.h>
30 #endif
31 #ifdef _LIBC
32 # include <wchar.h>
33 # define mbsrtowcs __mbsrtowcs
34 #endif
36 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
37 # if __STDC__
38 # include <stdarg.h>
39 # define VA_START(args, lastarg) va_start(args, lastarg)
40 # else
41 # include <varargs.h>
42 # define VA_START(args, lastarg) va_start(args)
43 # endif
44 #else
45 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
46 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
47 #endif
49 #if STDC_HEADERS || _LIBC
50 # include <stdlib.h>
51 # include <string.h>
52 #else
53 void exit ();
54 #endif
56 #include "error.h"
57 #include "unlocked-io.h"
59 #ifndef _
60 # define _(String) String
61 #endif
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) (
67 #if __STDC__ - 0
68 void
69 #endif
72 /* This variable is incremented each time `error' is called. */
73 unsigned int error_message_count;
75 #ifdef _LIBC
76 /* In the GNU C library, there is a predefined variable for this. */
78 # define program_name program_invocation_name
79 # include <errno.h>
81 /* In GNU libc we want do not want to use the common name `error' directly.
82 Instead make it a weak alias. */
83 extern void __error (int status, int errnum, const char *message, ...)
84 __attribute__ ((__format__ (__printf__, 3, 4)));
85 extern void __error_at_line (int status, int errnum, const char *file_name,
86 unsigned int line_number, const char *message,
87 ...)
88 __attribute__ ((__format__ (__printf__, 5, 6)));;
89 # define error __error
90 # define error_at_line __error_at_line
92 # ifdef USE_IN_LIBIO
93 # include <libio/iolibio.h>
94 # define fflush(s) _IO_fflush (s)
95 # endif
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 && USE_IN_LIBIO
161 if (_IO_fwide (stderr, 0) > 0)
163 __fwprintf (stderr, L": %s", s);
164 return;
166 #endif
168 fprintf (stderr, ": %s", s);
171 #ifdef VA_START
172 static void
173 error_tail (int status, int errnum, const char *message, va_list args)
175 # if HAVE_VPRINTF || _LIBC
176 # if _LIBC && USE_IN_LIBIO
177 if (_IO_fwide (stderr, 0) > 0)
179 # define ALLOCA_LIMIT 2000
180 size_t len = strlen (message) + 1;
181 wchar_t *wmessage = NULL;
182 mbstate_t st;
183 size_t res;
184 const char *tmp;
188 if (len < ALLOCA_LIMIT)
189 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
190 else
192 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
193 wmessage = NULL;
195 wmessage = (wchar_t *) realloc (wmessage,
196 len * sizeof (wchar_t));
198 if (wmessage == NULL)
200 fputws_unlocked (L"out of memory\n", stderr);
201 return;
205 memset (&st, '\0', sizeof (st));
206 tmp =message;
208 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
210 if (res == (size_t) -1)
211 /* The string cannot be converted. */
212 wmessage = (wchar_t *) L"???";
214 __vfwprintf (stderr, wmessage, args);
216 else
217 # endif
218 vfprintf (stderr, message, args);
219 # else
220 _doprnt (message, args, stderr);
221 # endif
222 va_end (args);
224 ++error_message_count;
225 if (errnum)
226 print_errno_message (errnum);
227 # if _LIBC && USE_IN_LIBIO
228 if (_IO_fwide (stderr, 0) > 0)
229 putwc (L'\n', stderr);
230 else
231 # endif
232 putc ('\n', stderr);
233 fflush (stderr);
234 if (status)
235 exit (status);
237 #endif
240 /* Print the program name and error message MESSAGE, which is a printf-style
241 format string with optional args.
242 If ERRNUM is nonzero, print its corresponding system error message.
243 Exit with status STATUS if it is nonzero. */
244 /* VARARGS */
245 void
246 #if defined VA_START && __STDC__
247 error (int status, int errnum, const char *message, ...)
248 #else
249 error (status, errnum, message, va_alist)
250 int status;
251 int errnum;
252 char *message;
253 va_dcl
254 #endif
256 #ifdef VA_START
257 va_list args;
258 #endif
260 fflush (stdout);
261 #ifdef _LIBC
262 # ifdef USE_IN_LIBIO
263 _IO_flockfile (stderr);
264 # else
265 __flockfile (stderr);
266 # endif
267 #endif
268 if (error_print_progname)
269 (*error_print_progname) ();
270 else
272 #if _LIBC && USE_IN_LIBIO
273 if (_IO_fwide (stderr, 0) > 0)
274 __fwprintf (stderr, L"%s: ", program_name);
275 else
276 #endif
277 fprintf (stderr, "%s: ", program_name);
280 #ifdef VA_START
281 VA_START (args, message);
282 error_tail (status, errnum, message, args);
283 #else
284 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
286 ++error_message_count;
287 if (errnum)
288 print_errno_message (errnum);
289 putc ('\n', stderr);
290 fflush (stderr);
291 if (status)
292 exit (status);
293 #endif
295 #ifdef _LIBC
296 # ifdef USE_IN_LIBIO
297 _IO_funlockfile (stderr);
298 # else
299 __funlockfile (stderr);
300 # endif
301 #endif
304 /* Sometimes we want to have at most one error per line. This
305 variable controls whether this mode is selected or not. */
306 int error_one_per_line;
308 void
309 #if defined VA_START && __STDC__
310 error_at_line (int status, int errnum, const char *file_name,
311 unsigned int line_number, const char *message, ...)
312 #else
313 error_at_line (status, errnum, file_name, line_number, message, va_alist)
314 int status;
315 int errnum;
316 const char *file_name;
317 unsigned int line_number;
318 char *message;
319 va_dcl
320 #endif
322 #ifdef VA_START
323 va_list args;
324 #endif
326 if (error_one_per_line)
328 static const char *old_file_name;
329 static unsigned int old_line_number;
331 if (old_line_number == line_number
332 && (file_name == old_file_name
333 || strcmp (old_file_name, file_name) == 0))
334 /* Simply return and print nothing. */
335 return;
337 old_file_name = file_name;
338 old_line_number = line_number;
341 fflush (stdout);
342 #ifdef _LIBC
343 # ifdef USE_IN_LIBIO
344 _IO_flockfile (stderr);
345 # else
346 __flockfile (stderr);
347 # endif
348 #endif
349 if (error_print_progname)
350 (*error_print_progname) ();
351 else
353 #if _LIBC && USE_IN_LIBIO
354 if (_IO_fwide (stderr, 0) > 0)
355 __fwprintf (stderr, L"%s: ", program_name);
356 else
357 #endif
358 fprintf (stderr, "%s:", program_name);
361 if (file_name != NULL)
363 #if _LIBC && USE_IN_LIBIO
364 if (_IO_fwide (stderr, 0) > 0)
365 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
366 else
367 #endif
368 fprintf (stderr, "%s:%d: ", file_name, line_number);
371 #ifdef VA_START
372 VA_START (args, message);
373 error_tail (status, errnum, message, args);
374 #else
375 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
377 ++error_message_count;
378 if (errnum)
379 print_errno_message (errnum);
380 putc ('\n', stderr);
381 fflush (stderr);
382 if (status)
383 exit (status);
384 #endif
386 #ifdef _LIBC
387 # ifdef USE_IN_LIBIO
388 _IO_funlockfile (stderr);
389 # else
390 __funlockfile (stderr);
391 # endif
392 #endif
395 #ifdef _LIBC
396 /* Make the weak alias. */
397 # undef error
398 # undef error_at_line
399 weak_alias (__error, error)
400 weak_alias (__error_at_line, error_at_line)
401 #endif