1 | /* Error handler for noninteractive utilities |
---|
2 | Copyright (C) 1990-1998, 2000, 2001 Free Software Foundation, Inc. |
---|
3 | |
---|
4 | |
---|
5 | NOTE: The canonical source of this file is maintained with the GNU C Library. |
---|
6 | Bugs can be reported to bug-glibc@gnu.org. |
---|
7 | |
---|
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 |
---|
11 | later version. |
---|
12 | |
---|
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. |
---|
17 | |
---|
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, |
---|
21 | USA. */ |
---|
22 | |
---|
23 | /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ |
---|
24 | |
---|
25 | #ifdef HAVE_CONFIG_H |
---|
26 | # include <config.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <stdio.h> |
---|
30 | |
---|
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 |
---|
43 | |
---|
44 | #if STDC_HEADERS || _LIBC |
---|
45 | # include <stdlib.h> |
---|
46 | # include <string.h> |
---|
47 | #else |
---|
48 | void exit (); |
---|
49 | #endif |
---|
50 | |
---|
51 | #include "error.h" |
---|
52 | |
---|
53 | #ifndef _ |
---|
54 | # if ENABLE_NLS || defined _LIBC |
---|
55 | # include <libintl.h> |
---|
56 | # ifndef _ |
---|
57 | # define _(Str) gettext (Str) |
---|
58 | # endif |
---|
59 | # else |
---|
60 | # define _(Str) (Str) |
---|
61 | # endif |
---|
62 | #endif |
---|
63 | |
---|
64 | /* If NULL, error will flush stdout, then print on stderr the program |
---|
65 | name, a colon and a space. Otherwise, error will call this |
---|
66 | function without parameters instead. */ |
---|
67 | void (*error_print_progname) ( |
---|
68 | #if __STDC__ - 0 |
---|
69 | void |
---|
70 | #endif |
---|
71 | ); |
---|
72 | |
---|
73 | /* This variable is incremented each time `error' is called. */ |
---|
74 | unsigned int error_message_count; |
---|
75 | |
---|
76 | #ifdef _LIBC |
---|
77 | /* In the GNU C library, there is a predefined variable for this. */ |
---|
78 | |
---|
79 | # define program_name program_invocation_name |
---|
80 | # include <errno.h> |
---|
81 | |
---|
82 | /* In GNU libc we want do not want to use the common name `error' directly. |
---|
83 | Instead make it a weak alias. */ |
---|
84 | # define error __error |
---|
85 | # define error_at_line __error_at_line |
---|
86 | |
---|
87 | # ifdef USE_IN_LIBIO |
---|
88 | # include <libio/iolibio.h> |
---|
89 | # define fflush(s) _IO_fflush (s) |
---|
90 | # endif |
---|
91 | |
---|
92 | #else /* not _LIBC */ |
---|
93 | |
---|
94 | /* The calling program should define program_name and set it to the |
---|
95 | name of the executing program. */ |
---|
96 | extern char *program_name; |
---|
97 | |
---|
98 | # ifdef HAVE_STRERROR_R |
---|
99 | # define __strerror_r strerror_r |
---|
100 | # else |
---|
101 | # if HAVE_STRERROR |
---|
102 | # ifndef strerror /* On some systems, strerror is a macro */ |
---|
103 | char *strerror (); |
---|
104 | # endif |
---|
105 | # else |
---|
106 | static char * |
---|
107 | private_strerror (errnum) |
---|
108 | int errnum; |
---|
109 | { |
---|
110 | extern char *sys_errlist[]; |
---|
111 | extern int sys_nerr; |
---|
112 | |
---|
113 | if (errnum > 0 && errnum <= sys_nerr) |
---|
114 | return _(sys_errlist[errnum]); |
---|
115 | return _("Unknown system error"); |
---|
116 | } |
---|
117 | # define strerror private_strerror |
---|
118 | # endif /* HAVE_STRERROR */ |
---|
119 | # endif /* HAVE_STRERROR_R */ |
---|
120 | #endif /* not _LIBC */ |
---|
121 | |
---|
122 | /* Print the program name and error message MESSAGE, which is a printf-style |
---|
123 | format string with optional args. |
---|
124 | If ERRNUM is nonzero, print its corresponding system error message. |
---|
125 | Exit with status STATUS if it is nonzero. */ |
---|
126 | /* VARARGS */ |
---|
127 | |
---|
128 | void |
---|
129 | #if defined VA_START && __STDC__ |
---|
130 | error (int status, int errnum, const char *message, ...) |
---|
131 | #else |
---|
132 | error (status, errnum, message, va_alist) |
---|
133 | int status; |
---|
134 | int errnum; |
---|
135 | char *message; |
---|
136 | va_dcl |
---|
137 | #endif |
---|
138 | { |
---|
139 | #ifdef VA_START |
---|
140 | va_list args; |
---|
141 | #endif |
---|
142 | |
---|
143 | if (error_print_progname) |
---|
144 | (*error_print_progname) (); |
---|
145 | else |
---|
146 | { |
---|
147 | fflush (stdout); |
---|
148 | fprintf (stderr, "%s: ", program_name); |
---|
149 | } |
---|
150 | |
---|
151 | #ifdef VA_START |
---|
152 | VA_START (args, message); |
---|
153 | # if HAVE_VPRINTF || _LIBC |
---|
154 | vfprintf (stderr, message, args); |
---|
155 | # else |
---|
156 | _doprnt (message, args, stderr); |
---|
157 | # endif |
---|
158 | va_end (args); |
---|
159 | #else |
---|
160 | fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); |
---|
161 | #endif |
---|
162 | |
---|
163 | ++error_message_count; |
---|
164 | if (errnum) |
---|
165 | { |
---|
166 | #if defined HAVE_STRERROR_R || _LIBC |
---|
167 | char errbuf[1024]; |
---|
168 | fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf)); |
---|
169 | #else |
---|
170 | fprintf (stderr, ": %s", strerror (errnum)); |
---|
171 | #endif |
---|
172 | } |
---|
173 | putc ('\n', stderr); |
---|
174 | fflush (stderr); |
---|
175 | if (status) |
---|
176 | exit (status); |
---|
177 | } |
---|
178 | |
---|
179 | /* Sometimes we want to have at most one error per line. This |
---|
180 | variable controls whether this mode is selected or not. */ |
---|
181 | int error_one_per_line; |
---|
182 | |
---|
183 | void |
---|
184 | #if defined VA_START && __STDC__ |
---|
185 | error_at_line (int status, int errnum, const char *file_name, |
---|
186 | unsigned int line_number, const char *message, ...) |
---|
187 | #else |
---|
188 | error_at_line (status, errnum, file_name, line_number, message, va_alist) |
---|
189 | int status; |
---|
190 | int errnum; |
---|
191 | const char *file_name; |
---|
192 | unsigned int line_number; |
---|
193 | char *message; |
---|
194 | va_dcl |
---|
195 | #endif |
---|
196 | { |
---|
197 | #ifdef VA_START |
---|
198 | va_list args; |
---|
199 | #endif |
---|
200 | |
---|
201 | if (error_one_per_line) |
---|
202 | { |
---|
203 | static const char *old_file_name; |
---|
204 | static unsigned int old_line_number; |
---|
205 | |
---|
206 | if (old_line_number == line_number && |
---|
207 | (file_name == old_file_name || !strcmp (old_file_name, file_name))) |
---|
208 | /* Simply return and print nothing. */ |
---|
209 | return; |
---|
210 | |
---|
211 | old_file_name = file_name; |
---|
212 | old_line_number = line_number; |
---|
213 | } |
---|
214 | |
---|
215 | if (error_print_progname) |
---|
216 | (*error_print_progname) (); |
---|
217 | else |
---|
218 | { |
---|
219 | fflush (stdout); |
---|
220 | fprintf (stderr, "%s:", program_name); |
---|
221 | } |
---|
222 | |
---|
223 | if (file_name != NULL) |
---|
224 | fprintf (stderr, "%s:%d: ", file_name, line_number); |
---|
225 | |
---|
226 | #ifdef VA_START |
---|
227 | VA_START (args, message); |
---|
228 | # if HAVE_VPRINTF || _LIBC |
---|
229 | vfprintf (stderr, message, args); |
---|
230 | # else |
---|
231 | _doprnt (message, args, stderr); |
---|
232 | # endif |
---|
233 | va_end (args); |
---|
234 | #else |
---|
235 | fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); |
---|
236 | #endif |
---|
237 | |
---|
238 | ++error_message_count; |
---|
239 | if (errnum) |
---|
240 | { |
---|
241 | #if defined HAVE_STRERROR_R || _LIBC |
---|
242 | char errbuf[1024]; |
---|
243 | fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf)); |
---|
244 | #else |
---|
245 | fprintf (stderr, ": %s", strerror (errnum)); |
---|
246 | #endif |
---|
247 | } |
---|
248 | putc ('\n', stderr); |
---|
249 | fflush (stderr); |
---|
250 | if (status) |
---|
251 | exit (status); |
---|
252 | } |
---|
253 | |
---|
254 | #ifdef _LIBC |
---|
255 | /* Make the weak alias. */ |
---|
256 | # undef error |
---|
257 | # undef error_at_line |
---|
258 | weak_alias (__error, error) |
---|
259 | weak_alias (__error_at_line, error_at_line) |
---|
260 | #endif |
---|