Update.
[glibc.git] / misc / error.c
blobac6de324dd1eaefaae3fa02117f6be2c1fc07eb8
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990,91,92,93,94,95,96,97,98 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>. */
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
28 #include <stdio.h>
30 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
31 # if __STDC__
32 # include <stdarg.h>
33 # define VA_START(args, lastarg) va_start(args, lastarg)
34 # else
35 # include <varargs.h>
36 # define VA_START(args, lastarg) va_start(args)
37 # endif
38 #else
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;
41 #endif
43 #if STDC_HEADERS || _LIBC
44 # include <stdlib.h>
45 # include <string.h>
46 #else
47 void exit ();
48 #endif
50 #include "error.h"
52 #ifndef _
53 # define _(String) String
54 #endif
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) (
60 #if __STDC__ - 0
61 void
62 #endif
65 /* This variable is incremented each time `error' is called. */
66 unsigned int error_message_count;
68 #ifdef _LIBC
69 /* In the GNU C library, there is a predefined variable for this. */
71 # define program_name program_invocation_name
72 # include <errno.h>
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
79 # ifdef USE_IN_LIBIO
80 # define fflush(s) _IO_fflush (s)
81 # endif
83 #else /* not _LIBC */
85 /* The calling program should define program_name and set it to the
86 name of the executing program. */
87 extern char *program_name;
89 # ifdef HAVE_STRERROR_R
90 # define __strerror_r strerror_r
91 # else
92 # if HAVE_STRERROR
93 # ifndef strerror /* On some systems, strerror is a macro */
94 char *strerror ();
95 # endif
96 # else
97 static char *
98 private_strerror (errnum)
99 int errnum;
101 extern char *sys_errlist[];
102 extern int sys_nerr;
104 if (errnum > 0 && errnum <= sys_nerr)
105 return _(sys_errlist[errnum]);
106 return _("Unknown system error");
108 # define strerror private_strerror
109 # endif /* HAVE_STRERROR */
110 # endif /* HAVE_STRERROR_R */
111 #endif /* not _LIBC */
113 /* Print the program name and error message MESSAGE, which is a printf-style
114 format string with optional args.
115 If ERRNUM is nonzero, print its corresponding system error message.
116 Exit with status STATUS if it is nonzero. */
117 /* VARARGS */
119 void
120 #if defined VA_START && __STDC__
121 error (int status, int errnum, const char *message, ...)
122 #else
123 error (status, errnum, message, va_alist)
124 int status;
125 int errnum;
126 char *message;
127 va_dcl
128 #endif
130 #ifdef VA_START
131 va_list args;
132 #endif
134 if (error_print_progname)
135 (*error_print_progname) ();
136 else
138 fflush (stdout);
139 fprintf (stderr, "%s: ", program_name);
142 #ifdef VA_START
143 VA_START (args, message);
144 # if HAVE_VPRINTF || _LIBC
145 vfprintf (stderr, message, args);
146 # else
147 _doprnt (message, args, stderr);
148 # endif
149 va_end (args);
150 #else
151 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
152 #endif
154 ++error_message_count;
155 if (errnum)
157 #if defined HAVE_STRERROR_R || defined _LIBC
158 char errbuf[1024];
159 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
160 #else
161 fprintf (stderr, ": %s", strerror (errnum));
162 #endif
164 putc ('\n', stderr);
165 fflush (stderr);
166 if (status)
167 exit (status);
170 /* Sometimes we want to have at most one error per line. This
171 variable controls whether this mode is selected or not. */
172 int error_one_per_line;
174 void
175 #if defined VA_START && __STDC__
176 error_at_line (int status, int errnum, const char *file_name,
177 unsigned int line_number, const char *message, ...)
178 #else
179 error_at_line (status, errnum, file_name, line_number, message, va_alist)
180 int status;
181 int errnum;
182 const char *file_name;
183 unsigned int line_number;
184 char *message;
185 va_dcl
186 #endif
188 #ifdef VA_START
189 va_list args;
190 #endif
192 if (error_one_per_line)
194 static const char *old_file_name;
195 static unsigned int old_line_number;
197 if (old_line_number == line_number &&
198 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
199 /* Simply return and print nothing. */
200 return;
202 old_file_name = file_name;
203 old_line_number = line_number;
206 if (error_print_progname)
207 (*error_print_progname) ();
208 else
210 fflush (stdout);
211 fprintf (stderr, "%s:", program_name);
214 if (file_name != NULL)
215 fprintf (stderr, "%s:%d: ", file_name, line_number);
217 #ifdef VA_START
218 VA_START (args, message);
219 # if HAVE_VPRINTF || _LIBC
220 vfprintf (stderr, message, args);
221 # else
222 _doprnt (message, args, stderr);
223 # endif
224 va_end (args);
225 #else
226 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
227 #endif
229 ++error_message_count;
230 if (errnum)
232 #if defined HAVE_STRERROR_R || defined _LIBC
233 char errbuf[1024];
234 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
235 #else
236 fprintf (stderr, ": %s", strerror (errnum));
237 #endif
239 putc ('\n', stderr);
240 fflush (stderr);
241 if (status)
242 exit (status);
245 #ifdef _LIBC
246 /* Make the weak alias. */
247 # undef error
248 # undef error_at_line
249 weak_alias (__error, error)
250 weak_alias (__error_at_line, error_at_line)
251 #endif