1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2002, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
40 # define mbsrtowcs __mbsrtowcs
44 # include "unlocked-io.h"
48 # define _(String) String
51 /* If NULL, error will flush stdout, then print on stderr the program
52 name, a colon and a space. Otherwise, error will call this
53 function without parameters instead. */
54 void (*error_print_progname
) (void);
56 /* This variable is incremented each time `error' is called. */
57 unsigned int error_message_count
;
60 /* In the GNU C library, there is a predefined variable for this. */
62 # define program_name program_invocation_name
64 # include <libio/libioP.h>
66 /* In GNU libc we want do not want to use the common name `error' directly.
67 Instead make it a weak alias. */
68 extern void __error (int status
, int errnum
, const char *message
, ...)
69 __attribute__ ((__format__ (__printf__
, 3, 4)));
70 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
71 unsigned int line_number
, const char *message
,
73 __attribute__ ((__format__ (__printf__
, 5, 6)));;
74 # define error __error
75 # define error_at_line __error_at_line
77 # include <libio/iolibio.h>
78 # define fflush(s) INTUSE(_IO_fflush) (s)
80 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
82 # include <bits/libc-lock.h>
86 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
87 # ifndef HAVE_DECL_STRERROR_R
88 "this configure-time declaration test was not run"
94 # define SIZE_MAX ((size_t) -1)
97 /* The calling program should define program_name and set it to the
98 name of the executing program. */
99 extern char *program_name
;
101 # if HAVE_STRERROR_R || defined strerror_r
102 # define __strerror_r strerror_r
104 #endif /* not _LIBC */
107 print_errno_message (int errnum
)
111 #if defined HAVE_STRERROR_R || _LIBC
113 # if STRERROR_R_CHAR_P || _LIBC
114 s
= __strerror_r (errnum
, errbuf
, sizeof errbuf
);
116 if (__strerror_r (errnum
, errbuf
, sizeof errbuf
) == 0)
122 s
= strerror (errnum
);
127 s
= _("Unknown system error");
131 if (_IO_fwide (stderr
, 0) > 0)
133 __fwprintf (stderr
, L
": %s", s
);
138 fprintf (stderr
, ": %s", s
);
142 error_tail (int status
, int errnum
, const char *message
, va_list args
)
145 if (_IO_fwide (stderr
, 0) > 0)
147 # define ALLOCA_LIMIT 2000
148 size_t len
= strlen (message
) + 1;
149 const wchar_t *wmessage
= L
"out of memory";
150 wchar_t *wbuf
= (len
< ALLOCA_LIMIT
151 ? alloca (len
* sizeof *wbuf
)
152 : len
<= SIZE_MAX
/ sizeof *wbuf
153 ? malloc (len
* sizeof *wbuf
)
160 const char *tmp
= message
;
161 memset (&st
, '\0', sizeof (st
));
162 res
= mbsrtowcs (wbuf
, &tmp
, len
, &st
);
163 wmessage
= res
== (size_t) -1 ? L
"???" : wbuf
;
166 __vfwprintf (stderr
, wmessage
, args
);
167 if (! (len
< ALLOCA_LIMIT
))
172 vfprintf (stderr
, message
, args
);
175 ++error_message_count
;
177 print_errno_message (errnum
);
179 if (_IO_fwide (stderr
, 0) > 0)
180 putwc (L
'\n', stderr
);
190 /* Print the program name and error message MESSAGE, which is a printf-style
191 format string with optional args.
192 If ERRNUM is nonzero, print its corresponding system error message.
193 Exit with status STATUS if it is nonzero. */
195 error (int status
, int errnum
, const char *message
, ...)
199 #if defined _LIBC && defined __libc_ptf_call
200 /* We do not want this call to be cut short by a thread
201 cancellation. Therefore disable cancellation for now. */
202 int state
= PTHREAD_CANCEL_ENABLE
;
203 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
209 _IO_flockfile (stderr
);
211 if (error_print_progname
)
212 (*error_print_progname
) ();
216 if (_IO_fwide (stderr
, 0) > 0)
217 __fwprintf (stderr
, L
"%s: ", program_name
);
220 fprintf (stderr
, "%s: ", program_name
);
223 va_start (args
, message
);
224 error_tail (status
, errnum
, message
, args
);
227 _IO_funlockfile (stderr
);
228 # ifdef __libc_ptf_call
229 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
234 /* Sometimes we want to have at most one error per line. This
235 variable controls whether this mode is selected or not. */
236 int error_one_per_line
;
239 error_at_line (int status
, int errnum
, const char *file_name
,
240 unsigned int line_number
, const char *message
, ...)
244 if (error_one_per_line
)
246 static const char *old_file_name
;
247 static unsigned int old_line_number
;
249 if (old_line_number
== line_number
250 && (file_name
== old_file_name
251 || strcmp (old_file_name
, file_name
) == 0))
252 /* Simply return and print nothing. */
255 old_file_name
= file_name
;
256 old_line_number
= line_number
;
259 #if defined _LIBC && defined __libc_ptf_call
260 /* We do not want this call to be cut short by a thread
261 cancellation. Therefore disable cancellation for now. */
262 int state
= PTHREAD_CANCEL_ENABLE
;
263 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
269 _IO_flockfile (stderr
);
271 if (error_print_progname
)
272 (*error_print_progname
) ();
276 if (_IO_fwide (stderr
, 0) > 0)
277 __fwprintf (stderr
, L
"%s: ", program_name
);
280 fprintf (stderr
, "%s:", program_name
);
283 if (file_name
!= NULL
)
286 if (_IO_fwide (stderr
, 0) > 0)
287 __fwprintf (stderr
, L
"%s:%d: ", file_name
, line_number
);
290 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
293 va_start (args
, message
);
294 error_tail (status
, errnum
, message
, args
);
297 _IO_funlockfile (stderr
);
298 # ifdef __libc_ptf_call
299 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
305 /* Make the weak alias. */
307 # undef error_at_line
308 weak_alias (__error
, error
)
309 weak_alias (__error_at_line
, error_at_line
)