Import upstream version 1.28
[debian/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-2014 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 native Windows API functions.  */
95 #  define WIN32_LEAN_AND_MEAN
96 #  include <windows.h>
97 /* Get _get_osfhandle.  */
98 #  include "msvc-nothrow.h"
99 # endif
100
101 /* The gnulib override of fcntl is not needed in this file.  */
102 # undef fcntl
103
104 # if !HAVE_DECL_STRERROR_R
105 #  ifndef HAVE_DECL_STRERROR_R
106 "this configure-time declaration test was not run"
107 #  endif
108 #  if STRERROR_R_CHAR_P
109 char *strerror_r ();
110 #  else
111 int strerror_r ();
112 #  endif
113 # endif
114
115 /* The calling program should define program_name and set it to the
116    name of the executing program.  */
117 extern char *program_name;
118
119 # if HAVE_STRERROR_R || defined strerror_r
120 #  define __strerror_r strerror_r
121 # endif /* HAVE_STRERROR_R || defined strerror_r */
122 #endif  /* not _LIBC */
123
124 #if !_LIBC
125 /* Return non-zero if FD is open.  */
126 static int
127 is_open (int fd)
128 {
129 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
130   /* On native Windows: The initial state of unassigned standard file
131      descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
132      There is no fcntl, and the gnulib replacement fcntl does not support
133      F_GETFL.  */
134   return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
135 # else
136 #  ifndef F_GETFL
137 #   error Please port fcntl to your platform
138 #  endif
139   return 0 <= fcntl (fd, F_GETFL);
140 # endif
141 }
142 #endif
143
144 static void
145 flush_stdout (void)
146 {
147 #if !_LIBC
148   int stdout_fd;
149
150 # if GNULIB_FREOPEN_SAFER
151   /* Use of gnulib's freopen-safer module normally ensures that
152        fileno (stdout) == 1
153      whenever stdout is open.  */
154   stdout_fd = STDOUT_FILENO;
155 # else
156   /* POSIX states that fileno (stdout) after fclose is unspecified.  But in
157      practice it is not a problem, because stdout is statically allocated and
158      the fd of a FILE stream is stored as a field in its allocated memory.  */
159   stdout_fd = fileno (stdout);
160 # endif
161   /* POSIX states that fflush (stdout) after fclose is unspecified; it
162      is safe in glibc, but not on all other platforms.  fflush (NULL)
163      is always defined, but too draconian.  */
164   if (0 <= stdout_fd && is_open (stdout_fd))
165 #endif
166     fflush (stdout);
167 }
168
169 static void
170 print_errno_message (int errnum)
171 {
172   char const *s;
173
174 #if defined HAVE_STRERROR_R || _LIBC
175   char errbuf[1024];
176 # if STRERROR_R_CHAR_P || _LIBC
177   s = __strerror_r (errnum, errbuf, sizeof errbuf);
178 # else
179   if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
180     s = errbuf;
181   else
182     s = 0;
183 # endif
184 #else
185   s = strerror (errnum);
186 #endif
187
188 #if !_LIBC
189   if (! s)
190     s = _("Unknown system error");
191 #endif
192
193 #if _LIBC
194   __fxprintf (NULL, ": %s", s);
195 #else
196   fprintf (stderr, ": %s", s);
197 #endif
198 }
199
200 static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3))
201 error_tail (int status, int errnum, const char *message, va_list args)
202 {
203 #if _LIBC
204   if (_IO_fwide (stderr, 0) > 0)
205     {
206 # define ALLOCA_LIMIT 2000
207       size_t len = strlen (message) + 1;
208       wchar_t *wmessage = NULL;
209       mbstate_t st;
210       size_t res;
211       const char *tmp;
212       bool use_malloc = false;
213
214       while (1)
215         {
216           if (__libc_use_alloca (len * sizeof (wchar_t)))
217             wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
218           else
219             {
220               if (!use_malloc)
221                 wmessage = NULL;
222
223               wchar_t *p = (wchar_t *) realloc (wmessage,
224                                                 len * sizeof (wchar_t));
225               if (p == NULL)
226                 {
227                   free (wmessage);
228                   fputws_unlocked (L"out of memory\n", stderr);
229                   return;
230                 }
231               wmessage = p;
232               use_malloc = true;
233             }
234
235           memset (&st, '\0', sizeof (st));
236           tmp = message;
237
238           res = mbsrtowcs (wmessage, &tmp, len, &st);
239           if (res != len)
240             break;
241
242           if (__builtin_expect (len >= SIZE_MAX / 2, 0))
243             {
244               /* This really should not happen if everything is fine.  */
245               res = (size_t) -1;
246               break;
247             }
248
249           len *= 2;
250         }
251
252       if (res == (size_t) -1)
253         {
254           /* The string cannot be converted.  */
255           if (use_malloc)
256             {
257               free (wmessage);
258               use_malloc = false;
259             }
260           wmessage = (wchar_t *) L"???";
261         }
262
263       __vfwprintf (stderr, wmessage, args);
264
265       if (use_malloc)
266         free (wmessage);
267     }
268   else
269 #endif
270     vfprintf (stderr, message, args);
271   va_end (args);
272
273   ++error_message_count;
274   if (errnum)
275     print_errno_message (errnum);
276 #if _LIBC
277   __fxprintf (NULL, "\n");
278 #else
279   putc ('\n', stderr);
280 #endif
281   fflush (stderr);
282   if (status)
283     exit (status);
284 }
285
286
287 /* Print the program name and error message MESSAGE, which is a printf-style
288    format string with optional args.
289    If ERRNUM is nonzero, print its corresponding system error message.
290    Exit with status STATUS if it is nonzero.  */
291 void
292 error (int status, int errnum, const char *message, ...)
293 {
294   va_list args;
295
296 #if defined _LIBC && defined __libc_ptf_call
297   /* We do not want this call to be cut short by a thread
298      cancellation.  Therefore disable cancellation for now.  */
299   int state = PTHREAD_CANCEL_ENABLE;
300   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
301                    0);
302 #endif
303
304   flush_stdout ();
305 #ifdef _LIBC
306   _IO_flockfile (stderr);
307 #endif
308   if (error_print_progname)
309     (*error_print_progname) ();
310   else
311     {
312 #if _LIBC
313       __fxprintf (NULL, "%s: ", program_name);
314 #else
315       fprintf (stderr, "%s: ", program_name);
316 #endif
317     }
318
319   va_start (args, message);
320   error_tail (status, errnum, message, args);
321
322 #ifdef _LIBC
323   _IO_funlockfile (stderr);
324 # ifdef __libc_ptf_call
325   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
326 # endif
327 #endif
328 }
329 \f
330 /* Sometimes we want to have at most one error per line.  This
331    variable controls whether this mode is selected or not.  */
332 int error_one_per_line;
333
334 void
335 error_at_line (int status, int errnum, const char *file_name,
336                unsigned int line_number, const char *message, ...)
337 {
338   va_list args;
339
340   if (error_one_per_line)
341     {
342       static const char *old_file_name;
343       static unsigned int old_line_number;
344
345       if (old_line_number == line_number
346           && (file_name == old_file_name
347               || strcmp (old_file_name, file_name) == 0))
348         /* Simply return and print nothing.  */
349         return;
350
351       old_file_name = file_name;
352       old_line_number = line_number;
353     }
354
355 #if defined _LIBC && defined __libc_ptf_call
356   /* We do not want this call to be cut short by a thread
357      cancellation.  Therefore disable cancellation for now.  */
358   int state = PTHREAD_CANCEL_ENABLE;
359   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
360                    0);
361 #endif
362
363   flush_stdout ();
364 #ifdef _LIBC
365   _IO_flockfile (stderr);
366 #endif
367   if (error_print_progname)
368     (*error_print_progname) ();
369   else
370     {
371 #if _LIBC
372       __fxprintf (NULL, "%s:", program_name);
373 #else
374       fprintf (stderr, "%s:", program_name);
375 #endif
376     }
377
378 #if _LIBC
379   __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
380               file_name, line_number);
381 #else
382   fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
383            file_name, line_number);
384 #endif
385
386   va_start (args, message);
387   error_tail (status, errnum, message, args);
388
389 #ifdef _LIBC
390   _IO_funlockfile (stderr);
391 # ifdef __libc_ptf_call
392   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
393 # endif
394 #endif
395 }
396
397 #ifdef _LIBC
398 /* Make the weak alias.  */
399 # undef error
400 # undef error_at_line
401 weak_alias (__error, error)
402 weak_alias (__error_at_line, error_at_line)
403 #endif