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