1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000 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 # define error __error
78 # define error_at_line __error_at_line
81 # include <libio/iolibio.h>
82 # define fflush(s) _IO_fflush (s)
87 /* The calling program should define program_name and set it to the
88 name of the executing program. */
89 extern char *program_name
;
91 # ifdef HAVE_STRERROR_R
92 # define __strerror_r strerror_r
95 # ifndef strerror /* On some systems, strerror is a macro */
100 private_strerror (errnum
)
103 extern char *sys_errlist
[];
106 if (errnum
> 0 && errnum
<= sys_nerr
)
107 return _(sys_errlist
[errnum
]);
108 return _("Unknown system error");
110 # define strerror private_strerror
111 # endif /* HAVE_STRERROR */
112 # endif /* HAVE_STRERROR_R */
113 #endif /* not _LIBC */
115 /* Print the program name and error message MESSAGE, which is a printf-style
116 format string with optional args.
117 If ERRNUM is nonzero, print its corresponding system error message.
118 Exit with status STATUS if it is nonzero. */
122 #if defined VA_START && __STDC__
123 error (int status
, int errnum
, const char *message
, ...)
125 error (status
, errnum
, message
, va_alist
)
136 if (error_print_progname
)
137 (*error_print_progname
) ();
141 fprintf (stderr
, "%s: ", program_name
);
145 VA_START (args
, message
);
146 # if HAVE_VPRINTF || _LIBC
147 vfprintf (stderr
, message
, args
);
149 _doprnt (message
, args
, stderr
);
153 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
156 ++error_message_count
;
159 #if defined HAVE_STRERROR_R || _LIBC
161 fprintf (stderr
, ": %s", __strerror_r (errnum
, errbuf
, sizeof errbuf
));
163 fprintf (stderr
, ": %s", strerror (errnum
));
172 /* Sometimes we want to have at most one error per line. This
173 variable controls whether this mode is selected or not. */
174 int error_one_per_line
;
177 #if defined VA_START && __STDC__
178 error_at_line (int status
, int errnum
, const char *file_name
,
179 unsigned int line_number
, const char *message
, ...)
181 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
184 const char *file_name
;
185 unsigned int line_number
;
194 if (error_one_per_line
)
196 static const char *old_file_name
;
197 static unsigned int old_line_number
;
199 if (old_line_number
== line_number
&&
200 (file_name
== old_file_name
|| !strcmp (old_file_name
, file_name
)))
201 /* Simply return and print nothing. */
204 old_file_name
= file_name
;
205 old_line_number
= line_number
;
208 if (error_print_progname
)
209 (*error_print_progname
) ();
213 fprintf (stderr
, "%s:", program_name
);
216 if (file_name
!= NULL
)
217 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
220 VA_START (args
, message
);
221 # if HAVE_VPRINTF || _LIBC
222 vfprintf (stderr
, message
, args
);
224 _doprnt (message
, args
, stderr
);
228 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
231 ++error_message_count
;
234 #if defined HAVE_STRERROR_R || _LIBC
236 fprintf (stderr
, ": %s", __strerror_r (errnum
, errbuf
, sizeof errbuf
));
238 fprintf (stderr
, ": %s", strerror (errnum
));
248 /* Make the weak alias. */
250 # undef error_at_line
251 weak_alias (__error
, error
)
252 weak_alias (__error_at_line
, error_at_line
)