1 /* error.c -- error handler for noninteractive utilities
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
26 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
29 # define VA_START(args, lastarg) va_start(args, lastarg)
32 # define VA_START(args, lastarg) va_start(args)
35 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
36 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
39 #if STDC_HEADERS || _LIBC
47 #define _(String) String
50 /* If NULL, error will flush stdout, then print on stderr the program
51 name, a colon and a space. Otherwise, error will call this
52 function without parameters instead. */
53 void (*error_print_progname) (
59 /* This variable is incremented each time `error' is called. */
60 unsigned int error_message_count;
63 /* In the GNU C library, there is a predefined variable for this. */
65 #define program_name program_invocation_name
70 /* The calling program should define program_name and set it to the
71 name of the executing program. */
72 extern char *program_name;
75 # ifndef strerror /* On some systems, strerror is a macro */
80 private_strerror (errnum)
83 extern char *sys_errlist[];
86 if (errnum > 0 && errnum <= sys_nerr)
87 return sys_errlist[errnum];
88 return _("Unknown system error");
90 #define strerror private_strerror
91 #endif /* HAVE_STRERROR */
94 /* Print the program name and error message MESSAGE, which is a printf-style
95 format string with optional args.
96 If ERRNUM is nonzero, print its corresponding system error message.
97 Exit with status STATUS if it is nonzero. */
101 #if defined(VA_START) && __STDC__
102 error (int status, int errnum, const char *message, ...)
104 error (status, errnum, message, va_alist)
115 if (error_print_progname)
116 (*error_print_progname) ();
120 fprintf (stderr, "%s: ", program_name);
124 VA_START (args, message);
125 # if HAVE_VPRINTF || _LIBC
126 vfprintf (stderr, message, args);
128 _doprnt (message, args, stderr);
132 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
135 ++error_message_count;
137 fprintf (stderr, ": %s", strerror (errnum));
144 /* Sometimes we want to have at most one error per line. This
145 variable controls whether this mode is selected or not. */
146 int error_one_per_line;
149 #if defined(VA_START) && __STDC__
150 error_at_line (int status, int errnum, const char *file_name,
151 unsigned int line_number, const char *message, ...)
153 error_at_line (status, errnum, file_name, line_number, message, va_alist)
156 const char *file_name;
157 unsigned int line_number;
166 if (error_one_per_line)
168 static const char *old_file_name;
169 static unsigned int old_line_number;
171 if (old_line_number == line_number &&
172 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
173 /* Simply return and print nothing. */
176 old_file_name = file_name;
177 old_line_number = line_number;
180 if (error_print_progname)
181 (*error_print_progname) ();
185 fprintf (stderr, "%s:", program_name);
188 if (file_name != NULL)
189 fprintf (stderr, "%s:%d: ", file_name, line_number);
192 VA_START (args, message);
193 # if HAVE_VPRINTF || _LIBC
194 vfprintf (stderr, message, args);
196 _doprnt (message, args, stderr);
200 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
203 ++error_message_count;
205 fprintf (stderr, ": %s", strerror (errnum));