1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990,91,92,93,94,95,96,97,98, 99 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>. */
30 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
33 # define VA_START(args, lastarg) va_start(args, lastarg)
36 # define VA_START(args, lastarg) va_start(args)
39 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
40 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
43 #if STDC_HEADERS || _LIBC
53 # define _(String) String
56 /* If NULL, error will flush stdout, then print on stderr the program
57 name, a colon and a space. Otherwise, error will call this
58 function without parameters instead. */
59 void (*error_print_progname
) (
65 /* This variable is incremented each time `error' is called. */
66 unsigned int error_message_count
;
69 /* In the GNU C library, there is a predefined variable for this. */
71 # define program_name program_invocation_name
74 /* In GNU libc we want do not want to use the common name `error' directly.
75 Instead make it a weak alias. */
76 # define error __error
77 # define error_at_line __error_at_line
81 /* The calling program should define program_name and set it to the
82 name of the executing program. */
83 extern char *program_name
;
85 # ifdef HAVE_STRERROR_R
86 # define __strerror_r strerror_r
89 # ifndef strerror /* On some systems, strerror is a macro */
94 private_strerror (errnum
)
97 extern char *sys_errlist
[];
100 if (errnum
> 0 && errnum
<= sys_nerr
)
101 return _(sys_errlist
[errnum
]);
102 return _("Unknown system error");
104 # define strerror private_strerror
105 # endif /* HAVE_STRERROR */
106 # endif /* HAVE_STRERROR_R */
107 #endif /* not _LIBC */
109 /* Print the program name and error message MESSAGE, which is a printf-style
110 format string with optional args.
111 If ERRNUM is nonzero, print its corresponding system error message.
112 Exit with status STATUS if it is nonzero. */
116 #if defined VA_START && __STDC__
117 error (int status
, int errnum
, const char *message
, ...)
119 error (status
, errnum
, message
, va_alist
)
130 if (error_print_progname
)
131 (*error_print_progname
) ();
135 fprintf (stderr
, "%s: ", program_name
);
139 VA_START (args
, message
);
140 # if HAVE_VPRINTF || _LIBC
141 vfprintf (stderr
, message
, args
);
143 _doprnt (message
, args
, stderr
);
147 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
150 ++error_message_count
;
153 #if defined HAVE_STRERROR_R || defined _LIBC
155 /* Don't use __strerror_r's return value because on some systems
156 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
157 __strerror_r (errnum
, errbuf
, sizeof errbuf
);
158 fprintf (stderr
, ": %s", errbuf
);
160 fprintf (stderr
, ": %s", strerror (errnum
));
169 /* Sometimes we want to have at most one error per line. This
170 variable controls whether this mode is selected or not. */
171 int error_one_per_line
;
174 #if defined VA_START && __STDC__
175 error_at_line (int status
, int errnum
, const char *file_name
,
176 unsigned int line_number
, const char *message
, ...)
178 error_at_line (status
, errnum
, file_name
, line_number
, message
, va_alist
)
181 const char *file_name
;
182 unsigned int line_number
;
191 if (error_one_per_line
)
193 static const char *old_file_name
;
194 static unsigned int old_line_number
;
196 if (old_line_number
== line_number
&&
197 (file_name
== old_file_name
|| !strcmp (old_file_name
, file_name
)))
198 /* Simply return and print nothing. */
201 old_file_name
= file_name
;
202 old_line_number
= line_number
;
205 if (error_print_progname
)
206 (*error_print_progname
) ();
210 fprintf (stderr
, "%s:", program_name
);
213 if (file_name
!= NULL
)
214 fprintf (stderr
, "%s:%d: ", file_name
, line_number
);
217 VA_START (args
, message
);
218 # if HAVE_VPRINTF || _LIBC
219 vfprintf (stderr
, message
, args
);
221 _doprnt (message
, args
, stderr
);
225 fprintf (stderr
, message
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, a8
);
228 ++error_message_count
;
231 #if defined HAVE_STRERROR_R || defined _LIBC
233 /* Don't use __strerror_r's return value because on some systems
234 (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
235 __strerror_r (errnum
, errbuf
, sizeof errbuf
);
236 fprintf (stderr
, ": %s", 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
)