9
|
1 /* error.c -- error handler for noninteractive utilities |
440
|
2 Copyright (C) 1990, 91, 92, 93, 94, 95 Free Software Foundation, Inc. |
9
|
3 |
|
4 This program is free software; you can redistribute it and/or modify |
|
5 it under the terms of the GNU General Public License as published by |
|
6 the Free Software Foundation; either version 2, or (at your option) |
|
7 any later version. |
|
8 |
|
9 This program is distributed in the hope that it will be useful, |
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 GNU General Public License for more details. |
|
13 |
|
14 You should have received a copy of the GNU General Public License |
|
15 along with this program; if not, write to the Free Software |
|
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
17 |
398
|
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */ |
|
19 |
|
20 #ifdef HAVE_CONFIG_H |
|
21 #include <config.h> |
|
22 #endif |
9
|
23 |
|
24 #include <stdio.h> |
|
25 |
398
|
26 #if HAVE_VPRINTF || HAVE_DOPRNT |
|
27 # if __STDC__ |
|
28 # include <stdarg.h> |
|
29 # define VA_START(args, lastarg) va_start(args, lastarg) |
|
30 # else |
|
31 # include <varargs.h> |
|
32 # define VA_START(args, lastarg) va_start(args) |
|
33 # endif |
|
34 #else |
|
35 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8 |
|
36 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8; |
|
37 #endif |
9
|
38 |
398
|
39 #if STDC_HEADERS |
|
40 # include <stdlib.h> |
|
41 # include <string.h> |
|
42 #else |
|
43 void exit (); |
|
44 #endif |
9
|
45 |
440
|
46 /* This variable is incremented each time `error' is called. */ |
|
47 unsigned int error_message_count; |
|
48 |
398
|
49 /* If NULL, error will flush stdout, then print on stderr the program |
|
50 name, a colon and a space. Otherwise, error will call this |
|
51 function without parameters instead. */ |
|
52 void (*error_print_progname) () = NULL; |
9
|
53 |
398
|
54 /* The calling program should define program_name and set it to the |
|
55 name of the executing program. */ |
|
56 extern char *program_name; |
9
|
57 |
398
|
58 #if HAVE_STRERROR |
482
|
59 # ifndef strerror /* On some systems, strerror is a macro */ |
398
|
60 char *strerror (); |
|
61 # endif |
|
62 #else |
9
|
63 static char * |
|
64 private_strerror (errnum) |
|
65 int errnum; |
|
66 { |
|
67 extern char *sys_errlist[]; |
|
68 extern int sys_nerr; |
|
69 |
|
70 if (errnum > 0 && errnum <= sys_nerr) |
|
71 return sys_errlist[errnum]; |
|
72 return "Unknown system error"; |
|
73 } |
|
74 #define strerror private_strerror |
398
|
75 #endif |
9
|
76 |
|
77 /* Print the program name and error message MESSAGE, which is a printf-style |
|
78 format string with optional args. |
|
79 If ERRNUM is nonzero, print its corresponding system error message. |
|
80 Exit with status STATUS if it is nonzero. */ |
|
81 /* VARARGS */ |
398
|
82 |
9
|
83 void |
398
|
84 #if defined(VA_START) && __STDC__ |
|
85 error (int status, int errnum, const char *message, ...) |
|
86 #else |
9
|
87 error (status, errnum, message, va_alist) |
|
88 int status; |
|
89 int errnum; |
|
90 char *message; |
|
91 va_dcl |
398
|
92 #endif |
9
|
93 { |
398
|
94 #ifdef VA_START |
9
|
95 va_list args; |
398
|
96 #endif |
9
|
97 |
398
|
98 if (error_print_progname) |
|
99 (*error_print_progname) (); |
|
100 else |
|
101 { |
|
102 fflush (stdout); |
|
103 fprintf (stderr, "%s: ", program_name); |
|
104 } |
|
105 |
|
106 #ifdef VA_START |
9
|
107 VA_START (args, message); |
398
|
108 # if HAVE_VPRINTF |
9
|
109 vfprintf (stderr, message, args); |
398
|
110 # else |
|
111 _doprnt (message, args, stderr); |
|
112 # endif |
9
|
113 va_end (args); |
398
|
114 #else |
9
|
115 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8); |
398
|
116 #endif |
|
117 |
440
|
118 ++error_message_count; |
|
119 |
9
|
120 if (errnum) |
|
121 fprintf (stderr, ": %s", strerror (errnum)); |
|
122 putc ('\n', stderr); |
|
123 fflush (stderr); |
|
124 if (status) |
|
125 exit (status); |
|
126 } |