1 /* error.c -- error handler for noninteractive utilities
2 Copyright (C) 1990, 91, 92, 93, 94, 1995
3 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <sys/types.h>
27
28 #if defined(HAVE_VPRINTF) || defined(HAVE_DOPRNT)
29 # if __STDC__
30 # include <stdarg.h>
31 # define VA_START(args, lastarg) va_start(args, lastarg)
32 # else
33 # include <varargs.h>
34 # define VA_START(args, lastarg) va_start(args)
35 # endif
36 #else
37 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
38 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
39 #endif
40
41 #if STDC_HEADERS
42 # include <stdlib.h>
43 # include <string.h>
44 #else
45 void exit ();
46 #endif
47
48 #ifdef HAVE_LIBINTL_H
49 # include <libintl.h>
50 #endif
51
52 #define _(str) (str)
53
54 /* The calling program should define program_name and set it to the
55 name of the executing program. */
56 extern char *program_name;
57
58 #ifdef HAVE_STRERROR
59 char *strerror ();
60 #else
61 extern char *sys_errlist[];
62 extern int sys_nerr;
63
64 /* bit of a pointless prorotype, but still ... */
65 static char *private_strerror (int errnum);
66 static char *
private_strerror(errnum)67 private_strerror (errnum)
68 int errnum;
69 {
70 if (errnum > 0 && errnum <= sys_nerr)
71 return sys_errlist[errnum];
72 return _("Unknown system error");
73 }
74 #define strerror private_strerror
75 #endif
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 */
82
83 void
84 #if defined(VA_START) && __STDC__
error(int status,int errnum,const char * message,...)85 error (int status, int errnum, const char *message, ...)
86 #else
87 error (status, errnum, message, va_alist)
88 int status;
89 int errnum;
90 char *message;
91 va_dcl
92 #endif
93 {
94 #ifdef VA_START
95 va_list args;
96 #endif
97
98 fflush (stdout);
99 fprintf (stderr, "%s: ", program_name);
100
101 #ifdef VA_START
102 VA_START (args, message);
103 # if HAVE_VPRINTF
104 vfprintf (stderr, message, args);
105 # else
106 _doprnt (message, args, stderr);
107 # endif
108 va_end (args);
109 #else
110 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
111 #endif
112
113 if (errnum)
114 fprintf (stderr, ": %s", strerror (errnum));
115 putc ('\n', stderr);
116 fflush (stderr);
117 if (status)
118 exit (status);
119 }
120
121 void
122 #if defined(VA_START) && __STDC__
error_with_loc(int status,int errnum,const char * file_name,size_t line_number,const char * message,...)123 error_with_loc (int status, int errnum, const char *file_name,
124 size_t line_number, const char *message, ...)
125 #else
126 error_with_loc (status, errnum, file_name, line_number, message, va_alist)
127 int status;
128 int errnum;
129 char *file_name;
130 size_t line_number;
131 char *message;
132 va_dcl
133 #endif
134 {
135 #ifdef VA_START
136 va_list args;
137 #endif
138
139 fflush (stdout);
140 fprintf (stderr, "%s:", program_name);
141
142 if (file_name != NULL)
143 fprintf (stderr, "%s:%d: ", file_name, (unsigned int)line_number);
144
145 #ifdef VA_START
146 VA_START (args, message);
147 # ifdef HAVE_VPRINTF
148 vfprintf (stderr, message, args);
149 # else
150 _doprnt (message, args, stderr);
151 # endif
152 va_end (args);
153 #else
154 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
155 #endif
156
157 if (errnum)
158 fprintf (stderr, ": %s", strerror (errnum));
159 putc ('\n', stderr);
160 fflush (stderr);
161 if (status)
162 exit (status);
163 }
164
165