1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990,91,92,93,94,95,96,97 Free Software Foundation, Inc.
5 NOTE: The canonical source of this file is maintained with the GNU C Library.
6 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23 /* 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. */
78 #define error_at_line __error_at_line
82 /* The calling program should define program_name and set it to the
83 name of the executing program. */
84 extern char *program_name;
86 #ifndef HAVE_STRERROR_R
88 # ifndef strerror /* On some systems, strerror is a macro */
93 private_strerror (errnum)
96 extern char *sys_errlist[];
99 if (errnum > 0 && errnum <= sys_nerr)
100 return _(sys_errlist[errnum]);
101 return _("Unknown system error");
103 # define strerror private_strerror
104 # endif /* HAVE_STRERROR */
105 #endif /* HAVE_STRERROR_R */
108 /* Print the program name and error message MESSAGE, which is a printf-style
109 format string with optional args.
110 If ERRNUM is nonzero, print its corresponding system error message.
111 Exit with status STATUS if it is nonzero. */
115 #if defined(VA_START) && __STDC__
116 error (int status, int errnum, const char *message, ...)
118 error (status, errnum, message, va_alist)
129 if (error_print_progname)
130 (*error_print_progname) ();
134 fprintf (stderr, "%s: ", program_name);
138 VA_START (args, message);
139 # if HAVE_VPRINTF || _LIBC
140 vfprintf (stderr, message, args);
142 _doprnt (message, args, stderr);
146 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
149 ++error_message_count;
152 #if defined HAVE_STRERROR_R || defined _LIBC
154 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
156 fprintf (stderr, ": %s", strerror (errnum));
165 /* Sometimes we want to have at most one error per line. This
166 variable controls whether this mode is selected or not. */
167 int error_one_per_line;
170 #if defined(VA_START) && __STDC__
171 error_at_line (int status, int errnum, const char *file_name,
172 unsigned int line_number, const char *message, ...)
174 error_at_line (status, errnum, file_name, line_number, message, va_alist)
177 const char *file_name;
178 unsigned int line_number;
187 if (error_one_per_line)
189 static const char *old_file_name;
190 static unsigned int old_line_number;
192 if (old_line_number == line_number &&
193 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
194 /* Simply return and print nothing. */
197 old_file_name = file_name;
198 old_line_number = line_number;
201 if (error_print_progname)
202 (*error_print_progname) ();
206 fprintf (stderr, "%s:", program_name);
209 if (file_name != NULL)
210 fprintf (stderr, "%s:%d: ", file_name, line_number);
213 VA_START (args, message);
214 # if HAVE_VPRINTF || _LIBC
215 vfprintf (stderr, message, args);
217 _doprnt (message, args, stderr);
221 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
224 ++error_message_count;
227 #if defined HAVE_STRERROR_R || defined _LIBC
229 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
231 fprintf (stderr, ": %s", strerror (errnum));
241 /* Make the weak alias. */
244 weak_alias (__error, error)
245 weak_alias (__error_at_line, error_at_line)