1 /* error.c -- error handler for noninteractive utilities
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96 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
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, 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
76 /* The calling program should define program_name and set it to the
77 name of the executing program. */
78 extern char *program_name;
80 #ifndef HAVE_STRERROR_R
82 # ifndef strerror /* On some systems, strerror is a macro */
87 private_strerror (errnum)
90 extern char *sys_errlist[];
93 if (errnum > 0 && errnum <= sys_nerr)
94 return _(sys_errlist[errnum]);
95 return _("Unknown system error");
97 # define strerror private_strerror
98 # endif /* HAVE_STRERROR */
99 #endif /* HAVE_STRERROR_R */
102 /* Print the program name and error message MESSAGE, which is a printf-style
103 format string with optional args.
104 If ERRNUM is nonzero, print its corresponding system error message.
105 Exit with status STATUS if it is nonzero. */
109 #if defined(VA_START) && __STDC__
110 error (int status, int errnum, const char *message, ...)
112 error (status, errnum, message, va_alist)
123 if (error_print_progname)
124 (*error_print_progname) ();
128 fprintf (stderr, "%s: ", program_name);
132 VA_START (args, message);
133 # if HAVE_VPRINTF || _LIBC
134 vfprintf (stderr, message, args);
136 _doprnt (message, args, stderr);
140 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
143 ++error_message_count;
146 #if defined HAVE_STRERROR_R || defined _LIBC
148 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
150 fprintf (stderr, ": %s", strerror (errnum));
159 /* Sometimes we want to have at most one error per line. This
160 variable controls whether this mode is selected or not. */
161 int error_one_per_line;
164 #if defined(VA_START) && __STDC__
165 error_at_line (int status, int errnum, const char *file_name,
166 unsigned int line_number, const char *message, ...)
168 error_at_line (status, errnum, file_name, line_number, message, va_alist)
171 const char *file_name;
172 unsigned int line_number;
181 if (error_one_per_line)
183 static const char *old_file_name;
184 static unsigned int old_line_number;
186 if (old_line_number == line_number &&
187 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
188 /* Simply return and print nothing. */
191 old_file_name = file_name;
192 old_line_number = line_number;
195 if (error_print_progname)
196 (*error_print_progname) ();
200 fprintf (stderr, "%s:", program_name);
203 if (file_name != NULL)
204 fprintf (stderr, "%s:%d: ", file_name, line_number);
207 VA_START (args, message);
208 # if HAVE_VPRINTF || _LIBC
209 vfprintf (stderr, message, args);
211 _doprnt (message, args, stderr);
215 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
218 ++error_message_count;
221 #if defined HAVE_STRERROR_R || defined _LIBC
223 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
225 fprintf (stderr, ": %s", strerror (errnum));