]>
Dogcows Code - chaz/tar/blob - lib/error.c
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000, 2001, 2002 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along
14 with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
33 # define mbsrtowcs __mbsrtowcs
36 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
39 # define VA_START(args, lastarg) va_start(args, lastarg)
42 # define VA_START(args, lastarg) va_start(args)
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;
49 #if STDC_HEADERS || _LIBC
59 # include "unlocked-io.h"
63 # define _(String) String
66 /* If NULL, error will flush stdout, then print on stderr the program
67 name, a colon and a space. Otherwise, error will call this
68 function without parameters instead. */
69 void (*error_print_progname
) (
75 /* This variable is incremented each time `error' is called. */
76 unsigned int error_message_count
;
79 /* In the GNU C library, there is a predefined variable for this. */
81 # define program_name program_invocation_name
83 # include <libio/libioP.h>
85 /* In GNU libc we want do not want to use the common name `error' directly.
86 Instead make it a weak alias. */
87 extern void __error (int status
, int errnum
, const char *message
, ...)
88 __attribute__ ((__format__ (__printf__
, 3, 4)));
89 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
90 unsigned int line_number
, const char *message
,
92 __attribute__ ((__format__ (__printf__
, 5, 6)));;
93 # define error __error
94 # define error_at_line __error_at_line
97 # include <libio/iolibio.h>
98 # define fflush(s) INTUSE(_IO_fflush) (s)
100 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
103 #else /* not _LIBC */
105 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
106 # ifndef HAVE_DECL_STRERROR_R
107 "this configure-time declaration test was not run"
112 /* The calling program should define program_name and set it to the
113 name of the executing program. */
114 extern char *program_name
;
116 # if HAVE_STRERROR_R || defined strerror_r
117 # define __strerror_r strerror_r
120 # ifndef HAVE_DECL_STRERROR
121 "this configure-time declaration test was not run"
123 # if !HAVE_DECL_STRERROR
128 private_strerror (int errnum
)
130 extern char *sys_errlist
[];
133 if (errnum
> 0 && errnum
<= sys_nerr
)
134 return _(sys_errlist
[errnum
]);
135 return _("Unknown system error");
137 # define strerror private_strerror
138 # endif /* HAVE_STRERROR */
139 # endif /* HAVE_STRERROR_R || defined strerror_r */
140 #endif /* not _LIBC */
143 print_errno_message (int errnum
)
147 #if defined HAVE_STRERROR_R || _LIBC
149 # if STRERROR_R_CHAR_P || _LIBC
150 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
152 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
158 s
= strerror (errnum
);
163 s
= _("Unknown system error");
166 #if _LIBC && USE_IN_LIBIO
167 if (_IO_fwide (stderr
, 0) > 0)
169 __fwprintf (stderr
, L
": %s", s
);
174 fprintf (stderr
, ": %s", s
);
179 error_tail (int status
, int errnum
, const char *message
, va_list args
)
181 # if HAVE_VPRINTF || _LIBC
182 # if _LIBC && USE_IN_LIBIO
183 if (_IO_fwide (stderr
, 0) > 0)
185 # define ALLOCA_LIMIT 2000
186 size_t len
= strlen (message
) + 1;
187 wchar_t *wmessage
= NULL
;
194 if (len
< ALLOCA_LIMIT
)
195 wmessage
= (wchar_t *) alloca (len
* sizeof (wchar_t));
198 if (wmessage
!= NULL
&& len
/ 2 < ALLOCA_LIMIT
)
201 wmessage
= (wchar_t *) realloc (wmessage
,
202 len
* sizeof (wchar_t));
204 if (wmessage
== NULL
)
206 fputws_unlocked (L
"out of memory\n", stderr
);
211 memset (&st
, '\0', sizeof (st
));
214 while ((res
= mbsrtowcs (wmessage
, &tmp
, len
, &st
)) == len
);
216 if (res
== (size_t) -1)
217 /* The string cannot be converted. */
218 wmessage
= (wchar_t *) L
"???";
220 __vfwprintf (stderr
, wmessage
, args
);
224 vfprintf (stderr
, message
, args
);
226 _doprnt (message
, args
, stderr
);
230 ++error_message_count
;
232 print_errno_message (errnum
);
233 # if _LIBC && USE_IN_LIBIO
234 if (_IO_fwide (stderr
, 0) > 0)
235 putwc (L
'\n', stderr
);
246 /* Print the program name and error message MESSAGE, which is a printf-style
247 format string with optional args.
248 If ERRNUM is nonzero, print its corresponding system error message.
249 Exit with status STATUS if it is nonzero. */
252 #if defined VA_START && __STDC__
253 error (int status
, int errnum
, const char *message
, ...)
255 error (status
, errnum
, message
, va_alist
)
269 _IO_flockfile (stderr
);
271 __flockfile (stderr
);
274 if (error_print_progname
)
275 (*error_print_progname
) ();
278 #if _LIBC && USE_IN_LIBIO
279 if (_IO_fwide (stderr
, 0) > 0)
280 __fwprintf (stderr
, L
"%s: ", program_name
);
283 fprintf (stderr
, "%s: ", program_name
);
287 VA_START (args
, message
);
288 error_tail (status
, errnum
, message
, args
);
290 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
292 ++error_message_count
;
294 print_errno_message (errnum
);
303 _IO_funlockfile (stderr
);
305 __funlockfile (stderr
);
310 /* Sometimes we want to have at most one error per line. This
311 variable controls whether this mode is selected or not. */
312 int error_one_per_line
;
315 #if defined VA_START && __STDC__
316 error_at_line (int status
, int errnum
, const char *file_name
,
317 unsigned int line_number
, const char *message
, ...)
319 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
322 const char *file_name
;
323 unsigned int line_number
;
332 if (error_one_per_line
)
334 static const char *old_file_name
;
335 static unsigned int old_line_number
;
337 if (old_line_number
== line_number
338 && (file_name
== old_file_name
339 || strcmp (old_file_name
, file_name
) == 0))
340 /* Simply return and print nothing. */
343 old_file_name
= file_name
;
344 old_line_number
= line_number
;
350 _IO_flockfile (stderr
);
352 __flockfile (stderr
);
355 if (error_print_progname
)
356 (*error_print_progname
) ();
359 #if _LIBC && USE_IN_LIBIO
360 if (_IO_fwide (stderr
, 0) > 0)
361 __fwprintf (stderr
, L
"%s: ", program_name
);
364 fprintf (stderr
, "%s:", program_name
);
367 if (file_name
!= NULL
)
369 #if _LIBC && USE_IN_LIBIO
370 if (_IO_fwide (stderr
, 0) > 0)
371 __fwprintf (stderr
, L
"%s:%d: ", file_name
, line_number
);
374 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
378 VA_START (args
, message
);
379 error_tail (status
, errnum
, message
, args
);
381 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
383 ++error_message_count
;
385 print_errno_message (errnum
);
394 _IO_funlockfile (stderr
);
396 __funlockfile (stderr
);
402 /* Make the weak alias. */
404 # undef error_at_line
405 weak_alias (__error
, error
)
406 weak_alias (__error_at_line
, error_at_line
)
This page took 0.0491 seconds and 4 git commands to generate.