Initial import from FreeBSD RELENG_4:
[games.git] / contrib / tar / lib / error.c
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.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program 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
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
21
22 /* $FreeBSD: src/contrib/tar/lib/error.c,v 1.2.2.1 2002/07/14 13:19:40 sobomax Exp $ */
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #if HAVE_LIBINTL_H
30 # include <libintl.h>
31 #endif
32 #ifdef _LIBC
33 # include <wchar.h>
34 # define mbsrtowcs __mbsrtowcs
35 #endif
36
37 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
38 # if __STDC__
39 #  include <stdarg.h>
40 #  define VA_START(args, lastarg) va_start(args, lastarg)
41 # else
42 #  include <varargs.h>
43 #  define VA_START(args, lastarg) va_start(args)
44 # endif
45 #else
46 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
47 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
48 #endif
49
50 #if STDC_HEADERS || _LIBC
51 # include <stdlib.h>
52 # include <string.h>
53 #else
54 void exit ();
55 #endif
56
57 #include "error.h"
58
59 #ifndef HAVE_DECL_STRERROR_R
60 "this configure-time declaration test was not run"
61 #endif
62 #if !HAVE_DECL_STRERROR_R
63 char *strerror_r ();
64 #endif
65
66 #ifndef _
67 # define _(String) String
68 #endif
69
70 /* If NULL, error will flush stdout, then print on stderr the program
71    name, a colon and a space.  Otherwise, error will call this
72    function without parameters instead.  */
73 void (*error_print_progname) (
74 #if __STDC__ - 0
75                               void
76 #endif
77                               );
78
79 /* This variable is incremented each time `error' is called.  */
80 unsigned int error_message_count;
81
82 #ifdef _LIBC
83 /* In the GNU C library, there is a predefined variable for this.  */
84
85 # define program_name program_invocation_name
86 # include <errno.h>
87
88 /* In GNU libc we want do not want to use the common name `error' directly.
89    Instead make it a weak alias.  */
90 extern void __error (int status, int errnum, const char *message, ...)
91      __attribute__ ((__format__ (__printf__, 3, 4)));
92 extern void __error_at_line (int status, int errnum, const char *file_name,
93                              unsigned int line_number, const char *message,
94                              ...)
95      __attribute__ ((__format__ (__printf__, 5, 6)));;
96 # define error __error
97 # define error_at_line __error_at_line
98
99 # ifdef USE_IN_LIBIO
100 #  include <libio/iolibio.h>
101 #  define fflush(s) _IO_fflush (s)
102 # endif
103
104 #else /* not _LIBC */
105
106 /* The calling program should define program_name and set it to the
107    name of the executing program.  */
108 extern char *program_name;
109
110 # ifdef HAVE_STRERROR_R
111 #  define __strerror_r strerror_r
112 # else
113 #  if HAVE_STRERROR
114 #   ifndef strerror             /* On some systems, strerror is a macro */
115 char *strerror ();
116 #   endif
117 #  else
118 static char *
119 private_strerror (errnum)
120      int errnum;
121 {
122   extern char *sys_errlist[];
123   extern int sys_nerr;
124
125   if (errnum > 0 && errnum <= sys_nerr)
126     return _(sys_errlist[errnum]);
127   return _("Unknown system error");
128 }
129 #   define strerror private_strerror
130 #  endif /* HAVE_STRERROR */
131 # endif /* HAVE_STRERROR_R */
132 #endif  /* not _LIBC */
133
134
135 #ifdef VA_START
136 static void
137 error_tail (int status, int errnum, const char *message, va_list args)
138 {
139 # if HAVE_VPRINTF || _LIBC
140 #  if _LIBC && USE_IN_LIBIO
141   if (_IO_fwide (stderr, 0) > 0)
142     {
143 #   define ALLOCA_LIMIT 2000
144       size_t len = strlen (message) + 1;
145       wchar_t *wmessage = NULL;
146       mbstate_t st;
147       size_t res;
148       const char *tmp;
149
150       do
151         {
152           if (len < ALLOCA_LIMIT)
153             wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
154           else
155             {
156               if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
157                 wmessage = NULL;
158
159               wmessage = (wchar_t *) realloc (wmessage,
160                                               len * sizeof (wchar_t));
161
162               if (wmessage == NULL)
163                 {
164                   fputws_unlocked (L"out of memory\n", stderr);
165                   return;
166                 }
167             }
168
169           memset (&st, '\0', sizeof (st));
170           tmp =message;
171         }
172       while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
173
174       if (res == (size_t) -1)
175         /* The string cannot be converted.  */
176         wmessage = (wchar_t *) L"???";
177
178       __vfwprintf (stderr, wmessage, args);
179     }
180   else
181 #  endif
182     vfprintf (stderr, message, args);
183 # else
184   _doprnt (message, args, stderr);
185 # endif
186   va_end (args);
187
188   ++error_message_count;
189   if (errnum)
190     {
191 # if defined HAVE_STRERROR_R || _LIBC
192       char errbuf[1024];
193       char *s;
194       /* Don't use __strerror_r's return value because on some systems
195          (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'.  */
196       (void)__strerror_r (errnum, errbuf, sizeof errbuf);
197       s = errbuf;
198 #  if _LIBC && USE_IN_LIBIO
199       if (_IO_fwide (stderr, 0) > 0)
200         __fwprintf (stderr, L": %s", s);
201       else
202 #  endif
203         fprintf (stderr, ": %s", s);
204 # else
205       fprintf (stderr, ": %s", strerror (errnum));
206 # endif
207     }
208 # if _LIBC && USE_IN_LIBIO
209   if (_IO_fwide (stderr, 0) > 0)
210     putwc (L'\n', stderr);
211   else
212 # endif
213     putc ('\n', stderr);
214   fflush (stderr);
215   if (status)
216     exit (status);
217 }
218 #endif
219
220
221 /* Print the program name and error message MESSAGE, which is a printf-style
222    format string with optional args.
223    If ERRNUM is nonzero, print its corresponding system error message.
224    Exit with status STATUS if it is nonzero.  */
225 /* VARARGS */
226 void
227 #if defined VA_START && __STDC__
228 error (int status, int errnum, const char *message, ...)
229 #else
230 error (status, errnum, message, va_alist)
231      int status;
232      int errnum;
233      char *message;
234      va_dcl
235 #endif
236 {
237 #ifdef VA_START
238   va_list args;
239 #endif
240
241   fflush (stdout);
242 #ifdef _LIBC
243 # ifdef USE_IN_LIBIO
244   _IO_flockfile (stderr);
245 # else
246   __flockfile (stderr);
247 # endif
248 #endif
249   if (error_print_progname)
250     (*error_print_progname) ();
251   else
252     {
253 #if _LIBC && USE_IN_LIBIO
254       if (_IO_fwide (stderr, 0) > 0)
255         __fwprintf (stderr, L"%s: ", program_name);
256       else
257 #endif
258         fprintf (stderr, "%s: ", program_name);
259     }
260
261 #ifdef VA_START
262   VA_START (args, message);
263   error_tail (status, errnum, message, args);
264 #else
265   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
266
267   ++error_message_count;
268   if (errnum)
269     {
270 # if defined HAVE_STRERROR_R || _LIBC
271       char errbuf[1024];
272       /* Don't use __strerror_r's return value because on some systems
273          (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'.  */
274       __strerror_r (errnum, errbuf, sizeof errbuf);
275       fprintf (stderr, ": %s", errbuf);
276 # else
277       fprintf (stderr, ": %s", strerror (errnum));
278 # endif
279     }
280   putc ('\n', stderr);
281   fflush (stderr);
282   if (status)
283     exit (status);
284 #endif
285
286 #ifdef _LIBC
287 # ifdef USE_IN_LIBIO
288   _IO_funlockfile (stderr);
289 # else
290   __funlockfile (stderr);
291 # endif
292 #endif
293 }
294 \f
295 /* Sometimes we want to have at most one error per line.  This
296    variable controls whether this mode is selected or not.  */
297 int error_one_per_line;
298
299 void
300 #if defined VA_START && __STDC__
301 error_at_line (int status, int errnum, const char *file_name,
302                unsigned int line_number, const char *message, ...)
303 #else
304 error_at_line (status, errnum, file_name, line_number, message, va_alist)
305      int status;
306      int errnum;
307      const char *file_name;
308      unsigned int line_number;
309      char *message;
310      va_dcl
311 #endif
312 {
313 #ifdef VA_START
314   va_list args;
315 #endif
316
317   if (error_one_per_line)
318     {
319       static const char *old_file_name;
320       static unsigned int old_line_number;
321
322       if (old_line_number == line_number
323           && (file_name == old_file_name
324               || strcmp (old_file_name, file_name) == 0))
325         /* Simply return and print nothing.  */
326         return;
327
328       old_file_name = file_name;
329       old_line_number = line_number;
330     }
331
332   fflush (stdout);
333 #ifdef _LIBC
334 # ifdef USE_IN_LIBIO
335   _IO_flockfile (stderr);
336 # else
337   __flockfile (stderr);
338 # endif
339 #endif
340   if (error_print_progname)
341     (*error_print_progname) ();
342   else
343     {
344 #if _LIBC && USE_IN_LIBIO
345       if (_IO_fwide (stderr, 0) > 0)
346         __fwprintf (stderr, L"%s: ", program_name);
347       else
348 #endif
349         fprintf (stderr, "%s:", program_name);
350     }
351
352   if (file_name != NULL)
353     {
354 #if _LIBC && USE_IN_LIBIO
355       if (_IO_fwide (stderr, 0) > 0)
356         __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
357       else
358 #endif
359         fprintf (stderr, "%s:%d: ", file_name, line_number);
360     }
361
362 #ifdef VA_START
363   VA_START (args, message);
364   error_tail (status, errnum, message, args);
365 #else
366   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
367
368   ++error_message_count;
369   if (errnum)
370     {
371 # if defined HAVE_STRERROR_R || _LIBC
372       char errbuf[1024];
373       /* Don't use __strerror_r's return value because on some systems
374          (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'.  */
375       __strerror_r (errnum, errbuf, sizeof errbuf);
376       fprintf (stderr, ": %s", errbuf);
377 # else
378       fprintf (stderr, ": %s", strerror (errnum));
379 # endif
380     }
381   putc ('\n', stderr);
382   fflush (stderr);
383   if (status)
384     exit (status);
385 #endif
386
387 #ifdef _LIBC
388 # ifdef USE_IN_LIBIO
389   _IO_funlockfile (stderr);
390 # else
391   __funlockfile (stderr);
392 # endif
393 #endif
394 }
395
396 #ifdef _LIBC
397 /* Make the weak alias.  */
398 # undef error
399 # undef error_at_line
400 weak_alias (__error, error)
401 weak_alias (__error_at_line, error_at_line)
402 #endif