chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / misc / error.c
1 /* Error handler for noninteractive utilities
2    Copyright (C) 1990-1998, 2000-2005, 2006 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #ifdef _LIBC
32 # include <gnu/option-groups.h>
33 # include <libintl.h>
34 # include <stdbool.h>
35 # include <stdint.h>
36 # include <wchar.h>
37 # define mbsrtowcs __mbsrtowcs
38 #endif
39
40 #include "error.h"
41
42 #ifndef _
43 # define _(String) String
44 #endif
45
46 /* If NULL, error will flush stdout, then print on stderr the program
47    name, a colon and a space.  Otherwise, error will call this
48    function without parameters instead.  */
49 void (*error_print_progname) (void);
50
51 /* This variable is incremented each time `error' is called.  */
52 unsigned int error_message_count;
53
54 #ifdef _LIBC
55 /* In the GNU C library, there is a predefined variable for this.  */
56
57 # define program_name program_invocation_name
58 # include <errno.h>
59 # include <limits.h>
60 # include <libio/libioP.h>
61
62 /* In GNU libc we want do not want to use the common name `error' directly.
63    Instead make it a weak alias.  */
64 extern void __error (int status, int errnum, const char *message, ...)
65      __attribute__ ((__format__ (__printf__, 3, 4)));
66 extern void __error_at_line (int status, int errnum, const char *file_name,
67                              unsigned int line_number, const char *message,
68                              ...)
69      __attribute__ ((__format__ (__printf__, 5, 6)));;
70 # define error __error
71 # define error_at_line __error_at_line
72
73 # include <libio/iolibio.h>
74 # define fflush(s) INTUSE(_IO_fflush) (s)
75 # undef putc
76 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
77
78 # include <bits/libc-lock.h>
79
80 #else /* not _LIBC */
81
82 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
83 #  ifndef HAVE_DECL_STRERROR_R
84 "this configure-time declaration test was not run"
85 #  endif
86 char *strerror_r ();
87 # endif
88
89 /* The calling program should define program_name and set it to the
90    name of the executing program.  */
91 extern char *program_name;
92
93 # if HAVE_STRERROR_R || defined strerror_r
94 #  define __strerror_r strerror_r
95 # endif /* HAVE_STRERROR_R || defined strerror_r */
96 #endif  /* not _LIBC */
97
98 static void
99 print_errno_message (int errnum)
100 {
101   char const *s;
102
103 #if defined HAVE_STRERROR_R || _LIBC
104   char errbuf[1024];
105 # if STRERROR_R_CHAR_P || _LIBC
106   s = __strerror_r (errnum, errbuf, sizeof errbuf);
107 # else
108   if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
109     s = errbuf;
110   else
111     s = 0;
112 # endif
113 #else
114   s = strerror (errnum);
115 #endif
116
117 #if !_LIBC
118   if (! s)
119     s = _("Unknown system error");
120 #endif
121
122 #if _LIBC
123   __fxprintf (NULL, ": %s", s);
124 #else
125   fprintf (stderr, ": %s", s);
126 #endif
127 }
128
129 static void
130 error_tail (int status, int errnum, const char *message, va_list args)
131 {
132 #if _LIBC
133   if (_IO_fwide (stderr, 0) > 0)
134     {
135 #if __OPTION_POSIX_WIDE_CHAR_DEVICE_IO
136 # define ALLOCA_LIMIT 2000
137       size_t len = strlen (message) + 1;
138       wchar_t *wmessage = NULL;
139       mbstate_t st;
140       size_t res;
141       const char *tmp;
142       bool use_malloc = false;
143
144       while (1)
145         {
146           if (__libc_use_alloca (len * sizeof (wchar_t)))
147             wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
148           else
149             {
150               if (!use_malloc)
151                 wmessage = NULL;
152
153               wchar_t *p = (wchar_t *) realloc (wmessage,
154                                                 len * sizeof (wchar_t));
155               if (p == NULL)
156                 {
157                   free (wmessage);
158                   fputws_unlocked (L"out of memory\n", stderr);
159                   return;
160                 }
161               wmessage = p;
162               use_malloc = true;
163             }
164
165           memset (&st, '\0', sizeof (st));
166           tmp = message;
167
168           res = mbsrtowcs (wmessage, &tmp, len, &st);
169           if (res != len)
170             break;
171
172           if (__builtin_expect (len >= SIZE_MAX / 2, 0))
173             {
174               /* This really should not happen if everything is fine.  */
175               res = (size_t) -1;
176               break;
177             }
178
179           len *= 2;
180         }
181
182       if (res == (size_t) -1)
183         {
184           /* The string cannot be converted.  */
185           if (use_malloc)
186             {
187               free (wmessage);
188               use_malloc = false;
189             }
190           wmessage = (wchar_t *) L"???";
191         }
192
193       __vfwprintf (stderr, wmessage, args);
194
195       if (use_malloc)
196         free (wmessage);
197 #else
198       abort ();
199 #endif
200     }
201   else
202 #endif
203     vfprintf (stderr, message, args);
204   va_end (args);
205
206   ++error_message_count;
207   if (errnum)
208     print_errno_message (errnum);
209 #if _LIBC
210   __fxprintf (NULL, "\n");
211 #else
212   putc ('\n', stderr);
213 #endif
214   fflush (stderr);
215   if (status)
216     exit (status);
217 }
218
219
220 /* Print the program name and error message MESSAGE, which is a printf-style
221    format string with optional args.
222    If ERRNUM is nonzero, print its corresponding system error message.
223    Exit with status STATUS if it is nonzero.  */
224 void
225 error (int status, int errnum, const char *message, ...)
226 {
227   va_list args;
228
229 #if defined _LIBC && defined __libc_ptf_call
230   /* We do not want this call to be cut short by a thread
231      cancellation.  Therefore disable cancellation for now.  */
232   int state = PTHREAD_CANCEL_ENABLE;
233   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
234                    0);
235 #endif
236
237   fflush (stdout);
238 #ifdef _LIBC
239   _IO_flockfile (stderr);
240 #endif
241   if (error_print_progname)
242     (*error_print_progname) ();
243   else
244     {
245 #if _LIBC
246       __fxprintf (NULL, "%s: ", program_name);
247 #else
248       fprintf (stderr, "%s: ", program_name);
249 #endif
250     }
251
252   va_start (args, message);
253   error_tail (status, errnum, message, args);
254
255 #ifdef _LIBC
256   _IO_funlockfile (stderr);
257 # ifdef __libc_ptf_call
258   __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
259 # endif
260 #endif
261 }
262 \f
263 /* Sometimes we want to have at most one error per line.  This
264    variable controls whether this mode is selected or not.  */
265 int error_one_per_line;
266
267 void
268 error_at_line (int status, int errnum, const char *file_name,
269                unsigned int line_number, const char *message, ...)
270 {
271   va_list args;
272
273   if (error_one_per_line)
274     {
275       static const char *old_file_name;
276       static unsigned int old_line_number;
277
278       if (old_line_number == line_number
279           && (file_name == old_file_name
280               || strcmp (old_file_name, file_name) == 0))
281         /* Simply return and print nothing.  */
282         return;
283
284       old_file_name = file_name;
285       old_line_number = line_number;
286     }
287
288 #if defined _LIBC && defined __libc_ptf_call
289   /* We do not want this call to be cut short by a thread
290      cancellation.  Therefore disable cancellation for now.  */
291   int state = PTHREAD_CANCEL_ENABLE;
292   __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
293                    0);
294 #endif
295
296   fflush (stdout);
297 #ifdef _LIBC
298   _IO_flockfile (stderr);
299 #endif
300   if (error_print_progname)
301     (*error_print_progname) ();
302   else
303     {
304 #if _LIBC
305       __fxprintf (NULL, "%s:", program_name);
306 #else
307       fprintf (stderr, "%s:", program_name);
308 #endif
309     }
310
311 #if _LIBC
312   __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
313               file_name, line_number);
314 #else
315   fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
316            file_name, line_number);
317 #endif
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
330 #ifdef _LIBC
331 /* Make the weak alias.  */
332 # undef error
333 # undef error_at_line
334 weak_alias (__error, error)
335 weak_alias (__error_at_line, error_at_line)
336 #endif