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