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