]> git.cworth.org Git - tar/blob - gnu/error.c
Imported Upstream version 1.24
[tar] / gnu / error.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Error handler for noninteractive utilities
4    Copyright (C) 1990-1998, 2000-2007, 2009-2010 Free Software Foundation, Inc.
5    This file is part of the GNU C Library.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
21
22 #if !_LIBC
23 # include <config.h>
24 #endif
25
26 #include "error.h"
27
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #if !_LIBC && ENABLE_NLS
34 # include "gettext.h"
35 # define _(msgid) gettext (msgid)
36 #endif
37
38 #ifdef _LIBC
39 # include <libintl.h>
40 # include <stdbool.h>
41 # include <stdint.h>
42 # include <wchar.h>
43 # define mbsrtowcs __mbsrtowcs
44 #endif
45
46 #if USE_UNLOCKED_IO
47 # include "unlocked-io.h"
48 #endif
49
50 #ifndef _
51 # define _(String) String
52 #endif
53
54 /* If NULL, error will flush stdout, then print on stderr the program
55    name, a colon and a space.  Otherwise, error will call this
56    function without parameters instead.  */
57 void (*error_print_progname) (void);
58
59 /* This variable is incremented each time `error' is called.  */
60 unsigned int error_message_count;
61
62 #ifdef _LIBC
63 /* In the GNU C library, there is a predefined variable for this.  */
64
65 # define program_name program_invocation_name
66 # include <errno.h>
67 # include <limits.h>
68 # include <libio/libioP.h>
69
70 /* In GNU libc we want do not want to use the common name `error' directly.
71    Instead make it a weak alias.  */
72 extern void __error (int status, int errnum, const char *message, ...)
73      __attribute__ ((__format__ (__printf__, 3, 4)));
74 extern void __error_at_line (int status, int errnum, const char *file_name,
75                              unsigned int line_number, const char *message,
76                              ...)
77      __attribute__ ((__format__ (__printf__, 5, 6)));;
78 # define error __error
79 # define error_at_line __error_at_line
80
81 # include <libio/iolibio.h>
82 # define fflush(s) INTUSE(_IO_fflush) (s)
83 # undef putc
84 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
85
86 # include <bits/libc-lock.h>
87
88 #else /* not _LIBC */
89
90 # include <fcntl.h>
91 # include <unistd.h>
92
93 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
94 /* Get declarations of the Win32 API functions.  */
95 #  define WIN32_LEAN_AND_MEAN
96 #  include <windows.h>
97 # endif
98
99 /* The gnulib override of fcntl is not needed in this file.  */
100 # undef fcntl
101
102 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
103 #  ifndef HAVE_DECL_STRERROR_R
104 "this configure-time declaration test was not run"
105 #  endif
106 char *strerror_r ();
107 # endif
108
109 /* The calling program should define program_name and set it to the
110    name of the executing program.  */
111 extern char *program_name;
112
113 # if HAVE_STRERROR_R || defined strerror_r
114 #  define __strerror_r strerror_r
115 # endif /* HAVE_STRERROR_R || defined strerror_r */
116 #endif  /* not _LIBC */
117
118 #if !_LIBC
119 /* Return non-zero if FD is open.  */
120 static inline int
121 is_open (int fd)
122 {
123 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
124   /* On Win32: The initial state of unassigned standard file descriptors is
125      that they are open but point to an INVALID_HANDLE_VALUE.  There is no
126      fcntl, and the gnulib replacement fcntl does not support F_GETFL.  */
127   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
128 # else
129 #  ifndef F_GETFL
130 #   error Please port fcntl to your platform
131 #  endif
132   return 0 <= fcntl (fd, F_GETFL);
133 # endif
134 }
135 #endif
136
137 static inline void
138 flush_stdout (void)
139 {
140 #if !_LIBC
141   int stdout_fd;
142
143 # if GNULIB_FREOPEN_SAFER
144   /* Use of gnulib's freopen-safer module normally ensures that
145        fileno (stdout) == 1
146      whenever stdout is open.  */
147   stdout_fd = STDOUT_FILENO;
148 # else
149   /* POSIX states that fileno (stdout) after fclose is unspecified.  But in
150      practice it is not a problem, because stdout is statically allocated and
151      the fd of a FILE stream is stored as a field in its allocated memory.  */
152   stdout_fd = fileno (stdout);
153 # endif
154   /* POSIX states that fflush (stdout) after fclose is unspecified; it
155      is safe in glibc, but not on all other platforms.  fflush (NULL)
156      is always defined, but too draconian.  */
157   if (0 <= stdout_fd && is_open (stdout_fd))
158 #endif
159     fflush (stdout);
160 }
161
162 static void
163 print_errno_message (int errnum)
164 {
165   char const *s;
166
167 #if defined HAVE_STRERROR_R || _LIBC
168   char errbuf[1024];
169 # if STRERROR_R_CHAR_P || _LIBC
170   s = __strerror_r (errnum, errbuf, sizeof errbuf);
171 # else
172   if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
173     s = errbuf;
174   else
175     s = 0;
176 # endif
177 #else
178   s = strerror (errnum);
179 #endif
180
181 #if !_LIBC
182   if (! s)
183     s = _("Unknown system error");
184 #endif
185
186 #if _LIBC
187   __fxprintf (NULL, ": %s", s);
188 #else
189   fprintf (stderr, ": %s", s);
190 #endif
191 }
192
193 static void
194 error_tail (int status, int errnum, const char *message, va_list args)
195 {
196 #if _LIBC
197   if (_IO_fwide (stderr, 0) > 0)
198     {
199 # define ALLOCA_LIMIT 2000
200       size_t len = strlen (message) + 1;
201       wchar_t *wmessage = NULL;
202       mbstate_t st;
203       size_t res;
204       const char *tmp;
205       bool use_malloc = false;
206
207       while (1)
208         {
209           if (__libc_use_alloca (len * sizeof (wchar_t)))
210             wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
211           else
212             {
213               if (!use_malloc)
214                 wmessage = NULL;
215
216               wchar_t *p = (wchar_t *) realloc (wmessage,
217                                                 len * sizeof (wchar_t));
218               if (p == NULL)
219                 {
220                   free (wmessage);
221                   fputws_unlocked (L"out of memory\n", stderr);
222                   return;
223                 }
224               wmessage = p;
225               use_malloc = true;
226             }
227
228           memset (&st, '\0', sizeof (st));
229           tmp = message;
230
231           res = mbsrtowcs (wmessage, &tmp, len, &st);
232           if (res != len)
233             break;
234
235           if (__builtin_expect (len >= SIZE_MAX / 2, 0))
236             {
237               /* This really should not happen if everything is fine.  */
238               res = (size_t) -1;
239               break;
240             }
241
242           len *= 2;
243         }
244
245       if (res == (size_t) -1)
246         {
247           /* The string cannot be converted.  */
248           if (use_malloc)
249             {
250               free (wmessage);
251               use_malloc = false;
252             }
253           wmessage = (wchar_t *) L"???";
254         }
255
256       __vfwprintf (stderr, wmessage, args);
257
258       if (use_malloc)
259         free (wmessage);
260     }
261   else
262 #endif
263     vfprintf (stderr, message, args);
264   va_end (args);
265
266   ++error_message_count;
267   if (errnum)
268     print_errno_message (errnum);
269 #if _LIBC
270   __fxprintf (NULL, "\n");
271 #else
272   putc ('\n', stderr);
273 #endif
274   fflush (stderr);
275   if (status)
276     exit (status);
277 }
278
279
280 /* Print the program name and error message MESSAGE, which is a printf-style
281    format string with optional args.
282    If ERRNUM is nonzero, print its corresponding system error message.
283    Exit with status STATUS if it is nonzero.  */
284 void
285 error (int status, int errnum, const char *message, ...)
286 {
287   va_list args;
288
289 #if defined _LIBC && defined __libc_ptf_call
290   /* We do not want this call to be cut short by a thread
291      cancellation.  Therefore disable cancellation for now.  */
292   int state = PTHREAD_CANCEL_ENABLE;
293   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
294                    0);
295 #endif
296
297   flush_stdout ();
298 #ifdef _LIBC
299   _IO_flockfile (stderr);
300 #endif
301   if (error_print_progname)
302     (*error_print_progname) ();
303   else
304     {
305 #if _LIBC
306       __fxprintf (NULL, "%s: ", program_name);
307 #else
308       fprintf (stderr, "%s: ", program_name);
309 #endif
310     }
311
312   va_start (args, message);
313   error_tail (status, errnum, message, args);
314
315 #ifdef _LIBC
316   _IO_funlockfile (stderr);
317 # ifdef __libc_ptf_call
318   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
319 # endif
320 #endif
321 }
322 \f
323 /* Sometimes we want to have at most one error per line.  This
324    variable controls whether this mode is selected or not.  */
325 int error_one_per_line;
326
327 void
328 error_at_line (int status, int errnum, const char *file_name,
329                unsigned int line_number, const char *message, ...)
330 {
331   va_list args;
332
333   if (error_one_per_line)
334     {
335       static const char *old_file_name;
336       static unsigned int old_line_number;
337
338       if (old_line_number == line_number
339           && (file_name == old_file_name
340               || strcmp (old_file_name, file_name) == 0))
341         /* Simply return and print nothing.  */
342         return;
343
344       old_file_name = file_name;
345       old_line_number = line_number;
346     }
347
348 #if defined _LIBC && defined __libc_ptf_call
349   /* We do not want this call to be cut short by a thread
350      cancellation.  Therefore disable cancellation for now.  */
351   int state = PTHREAD_CANCEL_ENABLE;
352   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
353                    0);
354 #endif
355
356   flush_stdout ();
357 #ifdef _LIBC
358   _IO_flockfile (stderr);
359 #endif
360   if (error_print_progname)
361     (*error_print_progname) ();
362   else
363     {
364 #if _LIBC
365       __fxprintf (NULL, "%s:", program_name);
366 #else
367       fprintf (stderr, "%s:", program_name);
368 #endif
369     }
370
371 #if _LIBC
372   __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
373               file_name, line_number);
374 #else
375   fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
376            file_name, line_number);
377 #endif
378
379   va_start (args, message);
380   error_tail (status, errnum, message, args);
381
382 #ifdef _LIBC
383   _IO_funlockfile (stderr);
384 # ifdef __libc_ptf_call
385   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
386 # endif
387 #endif
388 }
389
390 #ifdef _LIBC
391 /* Make the weak alias.  */
392 # undef error
393 # undef error_at_line
394 weak_alias (__error, error)
395 weak_alias (__error_at_line, error_at_line)
396 #endif