comparison lib/error.c @ 4636:8b869710f671

Remove K&R cruft.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 08 Sep 2003 23:26:06 +0000
parents cdb224406ea7
children 0ec32cb1202f
comparison
equal deleted inserted replaced
4635:c74e2d0a05f5 4636:8b869710f671
18 18
19 #ifdef HAVE_CONFIG_H 19 #ifdef HAVE_CONFIG_H
20 # include <config.h> 20 # include <config.h>
21 #endif 21 #endif
22 22
23 #include "error.h"
24
25 #include <stdarg.h>
23 #include <stdio.h> 26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
24 29
25 #ifdef _LIBC 30 #ifdef _LIBC
26 # include <libintl.h> 31 # include <libintl.h>
27 #else 32 #else
28 # include "gettext.h" 33 # include "gettext.h"
31 #ifdef _LIBC 36 #ifdef _LIBC
32 # include <wchar.h> 37 # include <wchar.h>
33 # define mbsrtowcs __mbsrtowcs 38 # define mbsrtowcs __mbsrtowcs
34 #endif 39 #endif
35 40
36 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
37 # if __STDC__
38 # include <stdarg.h>
39 # define VA_START(args, lastarg) va_start(args, lastarg)
40 # else
41 # include <varargs.h>
42 # define VA_START(args, lastarg) va_start(args)
43 # endif
44 #else
45 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
46 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
47 #endif
48
49 #if STDC_HEADERS || _LIBC
50 # include <stdlib.h>
51 # include <string.h>
52 #else
53 void exit ();
54 #endif
55
56 #include "error.h"
57
58 #if !_LIBC 41 #if !_LIBC
59 # include "unlocked-io.h" 42 # include "unlocked-io.h"
60 #endif 43 #endif
61 44
62 #ifndef _ 45 #ifndef _
64 #endif 47 #endif
65 48
66 /* If NULL, error will flush stdout, then print on stderr the program 49 /* If NULL, error will flush stdout, then print on stderr the program
67 name, a colon and a space. Otherwise, error will call this 50 name, a colon and a space. Otherwise, error will call this
68 function without parameters instead. */ 51 function without parameters instead. */
69 void (*error_print_progname) ( 52 void (*error_print_progname) (void);
70 #if __STDC__ - 0
71 void
72 #endif
73 );
74 53
75 /* This variable is incremented each time `error' is called. */ 54 /* This variable is incremented each time `error' is called. */
76 unsigned int error_message_count; 55 unsigned int error_message_count;
77 56
78 #ifdef _LIBC 57 #ifdef _LIBC
79 /* In the GNU C library, there is a predefined variable for this. */ 58 /* In the GNU C library, there is a predefined variable for this. */
80 59
81 # define program_name program_invocation_name 60 # define program_name program_invocation_name
82 # include <errno.h>
83 # include <libio/libioP.h> 61 # include <libio/libioP.h>
84 62
85 /* In GNU libc we want do not want to use the common name `error' directly. 63 /* In GNU libc we want do not want to use the common name `error' directly.
86 Instead make it a weak alias. */ 64 Instead make it a weak alias. */
87 extern void __error (int status, int errnum, const char *message, ...) 65 extern void __error (int status, int errnum, const char *message, ...)
111 name of the executing program. */ 89 name of the executing program. */
112 extern char *program_name; 90 extern char *program_name;
113 91
114 # if HAVE_STRERROR_R || defined strerror_r 92 # if HAVE_STRERROR_R || defined strerror_r
115 # define __strerror_r strerror_r 93 # define __strerror_r strerror_r
116 # else 94 # endif
117 # if HAVE_STRERROR
118 # ifndef HAVE_DECL_STRERROR
119 "this configure-time declaration test was not run"
120 # endif
121 # if !HAVE_DECL_STRERROR
122 char *strerror ();
123 # endif
124 # else
125 static char *
126 private_strerror (int errnum)
127 {
128 extern char *sys_errlist[];
129 extern int sys_nerr;
130
131 if (errnum > 0 && errnum <= sys_nerr)
132 return _(sys_errlist[errnum]);
133 return _("Unknown system error");
134 }
135 # define strerror private_strerror
136 # endif /* HAVE_STRERROR */
137 # endif /* HAVE_STRERROR_R || defined strerror_r */
138 #endif /* not _LIBC */ 95 #endif /* not _LIBC */
139 96
140 static void 97 static void
141 print_errno_message (int errnum) 98 print_errno_message (int errnum)
142 { 99 {
170 #endif 127 #endif
171 128
172 fprintf (stderr, ": %s", s); 129 fprintf (stderr, ": %s", s);
173 } 130 }
174 131
175 #ifdef VA_START
176 static void 132 static void
177 error_tail (int status, int errnum, const char *message, va_list args) 133 error_tail (int status, int errnum, const char *message, va_list args)
178 { 134 {
179 # if HAVE_VPRINTF || _LIBC 135 # if HAVE_VPRINTF || _LIBC
180 # if _LIBC 136 # if _LIBC
236 putc ('\n', stderr); 192 putc ('\n', stderr);
237 fflush (stderr); 193 fflush (stderr);
238 if (status) 194 if (status)
239 exit (status); 195 exit (status);
240 } 196 }
241 #endif
242 197
243 198
244 /* Print the program name and error message MESSAGE, which is a printf-style 199 /* Print the program name and error message MESSAGE, which is a printf-style
245 format string with optional args. 200 format string with optional args.
246 If ERRNUM is nonzero, print its corresponding system error message. 201 If ERRNUM is nonzero, print its corresponding system error message.
247 Exit with status STATUS if it is nonzero. */ 202 Exit with status STATUS if it is nonzero. */
248 /* VARARGS */ 203 /* VARARGS */
249 void 204 void
250 #if defined VA_START && __STDC__
251 error (int status, int errnum, const char *message, ...) 205 error (int status, int errnum, const char *message, ...)
252 #else 206 {
253 error (status, errnum, message, va_alist)
254 int status;
255 int errnum;
256 char *message;
257 va_dcl
258 #endif
259 {
260 #ifdef VA_START
261 va_list args; 207 va_list args;
262 #endif
263 208
264 fflush (stdout); 209 fflush (stdout);
265 #ifdef _LIBC 210 #ifdef _LIBC
266 _IO_flockfile (stderr); 211 _IO_flockfile (stderr);
267 #endif 212 #endif
275 else 220 else
276 #endif 221 #endif
277 fprintf (stderr, "%s: ", program_name); 222 fprintf (stderr, "%s: ", program_name);
278 } 223 }
279 224
280 #ifdef VA_START 225 va_start (args, message);
281 VA_START (args, message);
282 error_tail (status, errnum, message, args); 226 error_tail (status, errnum, message, args);
283 #else
284 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
285
286 ++error_message_count;
287 if (errnum)
288 print_errno_message (errnum);
289 putc ('\n', stderr);
290 fflush (stderr);
291 if (status)
292 exit (status);
293 #endif
294 227
295 #ifdef _LIBC 228 #ifdef _LIBC
296 _IO_funlockfile (stderr); 229 _IO_funlockfile (stderr);
297 #endif 230 #endif
298 } 231 }
300 /* Sometimes we want to have at most one error per line. This 233 /* Sometimes we want to have at most one error per line. This
301 variable controls whether this mode is selected or not. */ 234 variable controls whether this mode is selected or not. */
302 int error_one_per_line; 235 int error_one_per_line;
303 236
304 void 237 void
305 #if defined VA_START && __STDC__
306 error_at_line (int status, int errnum, const char *file_name, 238 error_at_line (int status, int errnum, const char *file_name,
307 unsigned int line_number, const char *message, ...) 239 unsigned int line_number, const char *message, ...)
308 #else 240 {
309 error_at_line (status, errnum, file_name, line_number, message, va_alist)
310 int status;
311 int errnum;
312 const char *file_name;
313 unsigned int line_number;
314 char *message;
315 va_dcl
316 #endif
317 {
318 #ifdef VA_START
319 va_list args; 241 va_list args;
320 #endif
321 242
322 if (error_one_per_line) 243 if (error_one_per_line)
323 { 244 {
324 static const char *old_file_name; 245 static const char *old_file_name;
325 static unsigned int old_line_number; 246 static unsigned int old_line_number;
358 else 279 else
359 #endif 280 #endif
360 fprintf (stderr, "%s:%d: ", file_name, line_number); 281 fprintf (stderr, "%s:%d: ", file_name, line_number);
361 } 282 }
362 283
363 #ifdef VA_START 284 va_start (args, message);
364 VA_START (args, message);
365 error_tail (status, errnum, message, args); 285 error_tail (status, errnum, message, args);
366 #else
367 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
368
369 ++error_message_count;
370 if (errnum)
371 print_errno_message (errnum);
372 putc ('\n', stderr);
373 fflush (stderr);
374 if (status)
375 exit (status);
376 #endif
377 286
378 #ifdef _LIBC 287 #ifdef _LIBC
379 _IO_funlockfile (stderr); 288 _IO_funlockfile (stderr);
380 #endif 289 #endif
381 } 290 }