1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however. The master source lives in /gd/gnu/lib.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library 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 GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
31 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
34 # define VA_START(args, lastarg) va_start(args, lastarg)
37 # define VA_START(args, lastarg) va_start(args)
40 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
41 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
44 #if STDC_HEADERS || _LIBC
54 # define _(String) String
57 /* If NULL, error will flush stdout, then print on stderr the program
58 name, a colon and a space. Otherwise, error will call this
59 function without parameters instead. */
60 void (*error_print_progname
) (
66 /* This variable is incremented each time `error' is called. */
67 unsigned int error_message_count
;
70 /* In the GNU C library, there is a predefined variable for this. */
72 # define program_name program_invocation_name
75 /* In GNU libc we want do not want to use the common name `error' directly.
76 Instead make it a weak alias. */
77 extern void __error (int status
, int errnum
, const char *message
, ...)
78 __attribute__ ((__format__ (__printf__
, 3, 4)));
79 extern void __error_at_line (int status
, int errnum
, const char *file_name
,
80 unsigned int line_number
, const char *message
,
82 __attribute__ ((__format__ (__printf__
, 5, 6)));;
83 # define error __error
84 # define error_at_line __error_at_line
87 # include <libio/iolibio.h>
88 # define fflush(s) _IO_fflush (s)
93 /* The calling program should define program_name and set it to the
94 name of the executing program. */
95 extern char *program_name
;
97 # ifdef HAVE_STRERROR_R
98 # define __strerror_r strerror_r
101 # ifndef strerror /* On some systems, strerror is a macro */
106 private_strerror (errnum
)
109 extern char *sys_errlist
[];
112 if (errnum
> 0 && errnum
<= sys_nerr
)
113 return _(sys_errlist
[errnum
]);
114 return _("Unknown system error");
116 # define strerror private_strerror
117 # endif /* HAVE_STRERROR */
118 # endif /* HAVE_STRERROR_R */
119 #endif /* not _LIBC */
121 /* Print the program name and error message MESSAGE, which is a printf-style
122 format string with optional args.
123 If ERRNUM is nonzero, print its corresponding system error message.
124 Exit with status STATUS if it is nonzero. */
128 #if defined VA_START && __STDC__
129 error (int status
, int errnum
, const char *message
, ...)
131 error (status
, errnum
, message
, va_alist
)
143 if (error_print_progname
)
144 (*error_print_progname
) ();
146 fprintf (stderr
, "%s: ", program_name
);
149 VA_START (args
, message
);
150 # if HAVE_VPRINTF || _LIBC
151 vfprintf (stderr
, message
, args
);
153 _doprnt (message
, args
, stderr
);
157 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
160 ++error_message_count
;
163 #if defined HAVE_STRERROR_R || _LIBC
165 fprintf (stderr
, ": %s", __strerror_r (errnum
, errbuf
, sizeof errbuf
));
167 fprintf (stderr
, ": %s", strerror (errnum
));
176 /* Sometimes we want to have at most one error per line. This
177 variable controls whether this mode is selected or not. */
178 int error_one_per_line
;
181 #if defined VA_START && __STDC__
182 error_at_line (int status
, int errnum
, const char *file_name
,
183 unsigned int line_number
, const char *message
, ...)
185 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
188 const char *file_name
;
189 unsigned int line_number
;
198 if (error_one_per_line
)
200 static const char *old_file_name
;
201 static unsigned int old_line_number
;
203 if (old_line_number
== line_number
&&
204 (file_name
== old_file_name
|| !strcmp (old_file_name
, file_name
)))
205 /* Simply return and print nothing. */
208 old_file_name
= file_name
;
209 old_line_number
= line_number
;
213 if (error_print_progname
)
214 (*error_print_progname
) ();
216 fprintf (stderr
, "%s:", program_name
);
218 if (file_name
!= NULL
)
219 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
222 VA_START (args
, message
);
223 # if HAVE_VPRINTF || _LIBC
224 vfprintf (stderr
, message
, args
);
226 _doprnt (message
, args
, stderr
);
230 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
233 ++error_message_count
;
236 #if defined HAVE_STRERROR_R || _LIBC
238 fprintf (stderr
, ": %s", __strerror_r (errnum
, errbuf
, sizeof errbuf
));
240 fprintf (stderr
, ": %s", strerror (errnum
));
250 /* Make the weak alias. */
252 # undef error_at_line
253 weak_alias (__error
, error
)
254 weak_alias (__error_at_line
, error_at_line
)