Use #if, not #ifdef in test for HAVE_CONFIG_H.
[gnulib.git] / lib / error.c
1 /* error.c -- error handler for noninteractive utilities
2    Copyright (C) 1990, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.  */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25
26 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
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
38
39 #if STDC_HEADERS || _LIBC
40 # include <stdlib.h>
41 # include <string.h>
42 #else
43 void exit ();
44 #endif
45
46 #ifndef _
47 # define _(String) String
48 #endif
49
50 /* Get prototypes for the functions defined here.  */
51 #include <error.h>
52
53 /* If NULL, error will flush stdout, then print on stderr the program
54    name, a colon and a space.  Otherwise, error will call this
55    function without parameters instead.  */
56 void (*error_print_progname) (
57 #if __STDC__ - 0
58                               void
59 #endif
60                               );
61
62 /* This variable is incremented each time `error' is called.  */
63 unsigned int error_message_count;
64
65 #ifdef _LIBC
66 /* In the GNU C library, there is a predefined variable for this.  */
67
68 # define program_name program_invocation_name
69 # include <errno.h>
70
71 #else
72
73 /* The calling program should define program_name and set it to the
74    name of the executing program.  */
75 extern char *program_name;
76
77 # if HAVE_STRERROR
78 #  ifndef strerror              /* On some systems, strerror is a macro */
79 char *strerror ();
80 #  endif
81 # else
82 static char *
83 private_strerror (errnum)
84      int errnum;
85 {
86   extern char *sys_errlist[];
87   extern int sys_nerr;
88
89   if (errnum > 0 && errnum <= sys_nerr)
90     return sys_errlist[errnum];
91   return _("Unknown system error");
92 }
93 #  define strerror private_strerror
94 # endif /* HAVE_STRERROR */
95 #endif  /* _LIBC */
96
97 /* Print the program name and error message MESSAGE, which is a printf-style
98    format string with optional args.
99    If ERRNUM is nonzero, print its corresponding system error message.
100    Exit with status STATUS if it is nonzero.  */
101 /* VARARGS */
102
103 void
104 #if defined(VA_START) && __STDC__
105 error (int status, int errnum, const char *message, ...)
106 #else
107 error (status, errnum, message, va_alist)
108      int status;
109      int errnum;
110      char *message;
111      va_dcl
112 #endif
113 {
114 #ifdef VA_START
115   va_list args;
116 #endif
117
118   if (error_print_progname)
119     (*error_print_progname) ();
120   else
121     {
122       fflush (stdout);
123       fprintf (stderr, "%s: ", program_name);
124     }
125
126 #ifdef VA_START
127   VA_START (args, message);
128 # if HAVE_VPRINTF || _LIBC
129   vfprintf (stderr, message, args);
130 # else
131   _doprnt (message, args, stderr);
132 # endif
133   va_end (args);
134 #else
135   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
136 #endif
137
138   ++error_message_count;
139   if (errnum)
140     fprintf (stderr, ": %s", strerror (errnum));
141   putc ('\n', stderr);
142   fflush (stderr);
143   if (status)
144     exit (status);
145 }
146 \f
147 /* Sometimes we want to have at most one error per line.  This
148    variable controls whether this mode is selected or not.  */
149 int error_one_per_line;
150
151 void
152 #if defined(VA_START) && __STDC__
153 error_at_line (int status, int errnum, const char *file_name,
154                unsigned int line_number, const char *message, ...)
155 #else
156 error_at_line (status, errnum, file_name, line_number, message, va_alist)
157      int status;
158      int errnum;
159      const char *file_name;
160      unsigned int line_number;
161      char *message;
162      va_dcl
163 #endif
164 {
165 #ifdef VA_START
166   va_list args;
167 #endif
168
169   if (error_one_per_line)
170     {
171       static const char *old_file_name;
172       static unsigned int old_line_number;
173
174       if (old_line_number == line_number &&
175           (file_name == old_file_name || !strcmp (old_file_name, file_name)))
176         /* Simply return and print nothing.  */
177         return;
178
179       old_file_name = file_name;
180       old_line_number = line_number;
181     }
182
183   if (error_print_progname)
184     (*error_print_progname) ();
185   else
186     {
187       fflush (stdout);
188       fprintf (stderr, "%s:", program_name);
189     }
190
191   if (file_name != NULL)
192     fprintf (stderr, "%s:%d: ", file_name, line_number);
193
194 #ifdef VA_START
195   VA_START (args, message);
196 # if HAVE_VPRINTF || _LIBC
197   vfprintf (stderr, message, args);
198 # else
199   _doprnt (message, args, stderr);
200 # endif
201   va_end (args);
202 #else
203   fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
204 #endif
205
206   ++error_message_count;
207   if (errnum)
208     fprintf (stderr, ": %s", strerror (errnum));
209   putc ('\n', stderr);
210   fflush (stderr);
211   if (status)
212     exit (status);
213 }