Update
[glibc.git] / misc / error.c
blob690b5bdc9998525e532860921ecdb263347a8f87
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000 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>
29 #include <libintl.h>
31 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
32 # if __STDC__
33 # include <stdarg.h>
34 # define VA_START(args, lastarg) va_start(args, lastarg)
35 # else
36 # include <varargs.h>
37 # define VA_START(args, lastarg) va_start(args)
38 # endif
39 #else
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;
42 #endif
44 #if STDC_HEADERS || _LIBC
45 # include <stdlib.h>
46 # include <string.h>
47 #else
48 void exit ();
49 #endif
51 #include "error.h"
53 #ifndef _
54 # define _(String) String
55 #endif
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) (
61 #if __STDC__ - 0
62 void
63 #endif
66 /* This variable is incremented each time `error' is called. */
67 unsigned int error_message_count;
69 #ifdef _LIBC
70 /* In the GNU C library, there is a predefined variable for this. */
72 # define program_name program_invocation_name
73 # include <errno.h>
75 /* In GNU libc we want do not want to use the common name `error' directly.
76 Instead make it a weak alias. */
77 # define error __error
78 # define error_at_line __error_at_line
80 # ifdef USE_IN_LIBIO
81 # include <libio/iolibio.h>
82 # define fflush(s) _IO_fflush (s)
83 # endif
85 #else /* not _LIBC */
87 /* The calling program should define program_name and set it to the
88 name of the executing program. */
89 extern char *program_name;
91 # ifdef HAVE_STRERROR_R
92 # define __strerror_r strerror_r
93 # else
94 # if HAVE_STRERROR
95 # ifndef strerror /* On some systems, strerror is a macro */
96 char *strerror ();
97 # endif
98 # else
99 static char *
100 private_strerror (errnum)
101 int errnum;
103 extern char *sys_errlist[];
104 extern int sys_nerr;
106 if (errnum > 0 && errnum <= sys_nerr)
107 return _(sys_errlist[errnum]);
108 return _("Unknown system error");
110 # define strerror private_strerror
111 # endif /* HAVE_STRERROR */
112 # endif /* HAVE_STRERROR_R */
113 #endif /* not _LIBC */
115 /* Print the program name and error message MESSAGE, which is a printf-style
116 format string with optional args.
117 If ERRNUM is nonzero, print its corresponding system error message.
118 Exit with status STATUS if it is nonzero. */
119 /* VARARGS */
121 void
122 #if defined VA_START && __STDC__
123 error (int status, int errnum, const char *message, ...)
124 #else
125 error (status, errnum, message, va_alist)
126 int status;
127 int errnum;
128 char *message;
129 va_dcl
130 #endif
132 #ifdef VA_START
133 va_list args;
134 #endif
136 if (error_print_progname)
137 (*error_print_progname) ();
138 else
140 fflush (stdout);
141 fprintf (stderr, "%s: ", program_name);
144 #ifdef VA_START
145 VA_START (args, message);
146 # if HAVE_VPRINTF || _LIBC
147 vfprintf (stderr, message, args);
148 # else
149 _doprnt (message, args, stderr);
150 # endif
151 va_end (args);
152 #else
153 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
154 #endif
156 ++error_message_count;
157 if (errnum)
159 #if defined HAVE_STRERROR_R || _LIBC
160 char errbuf[1024];
161 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
162 #else
163 fprintf (stderr, ": %s", strerror (errnum));
164 #endif
166 putc ('\n', stderr);
167 fflush (stderr);
168 if (status)
169 exit (status);
172 /* Sometimes we want to have at most one error per line. This
173 variable controls whether this mode is selected or not. */
174 int error_one_per_line;
176 void
177 #if defined VA_START && __STDC__
178 error_at_line (int status, int errnum, const char *file_name,
179 unsigned int line_number, const char *message, ...)
180 #else
181 error_at_line (status, errnum, file_name, line_number, message, va_alist)
182 int status;
183 int errnum;
184 const char *file_name;
185 unsigned int line_number;
186 char *message;
187 va_dcl
188 #endif
190 #ifdef VA_START
191 va_list args;
192 #endif
194 if (error_one_per_line)
196 static const char *old_file_name;
197 static unsigned int old_line_number;
199 if (old_line_number == line_number &&
200 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
201 /* Simply return and print nothing. */
202 return;
204 old_file_name = file_name;
205 old_line_number = line_number;
208 if (error_print_progname)
209 (*error_print_progname) ();
210 else
212 fflush (stdout);
213 fprintf (stderr, "%s:", program_name);
216 if (file_name != NULL)
217 fprintf (stderr, "%s:%d: ", file_name, line_number);
219 #ifdef VA_START
220 VA_START (args, message);
221 # if HAVE_VPRINTF || _LIBC
222 vfprintf (stderr, message, args);
223 # else
224 _doprnt (message, args, stderr);
225 # endif
226 va_end (args);
227 #else
228 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
229 #endif
231 ++error_message_count;
232 if (errnum)
234 #if defined HAVE_STRERROR_R || _LIBC
235 char errbuf[1024];
236 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
237 #else
238 fprintf (stderr, ": %s", strerror (errnum));
239 #endif
241 putc ('\n', stderr);
242 fflush (stderr);
243 if (status)
244 exit (status);
247 #ifdef _LIBC
248 /* Make the weak alias. */
249 # undef error
250 # undef error_at_line
251 weak_alias (__error, error)
252 weak_alias (__error_at_line, error_at_line)
253 #endif