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