* sysdeps/unix/sysv/linux/m68k/getpagesize.c: Fix last change.
[glibc.git] / misc / error.c
blob6b911de98b6232c10209c380f85f16e1dd1b0f73
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. Its master source is NOT part of
4 the C library, however. The master source lives in /gd/gnu/lib.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include <stdio.h>
28 #include <libintl.h>
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
43 #if STDC_HEADERS || _LIBC
44 # include <stdlib.h>
45 # include <string.h>
46 #else
47 void exit ();
48 #endif
50 #include "error.h"
52 #ifndef _
53 # define _(String) String
54 #endif
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
65 /* This variable is incremented each time `error' is called. */
66 unsigned int error_message_count;
68 #ifdef _LIBC
69 /* In the GNU C library, there is a predefined variable for this. */
71 # define program_name program_invocation_name
72 # include <errno.h>
74 /* In GNU libc we want do not want to use the common name `error' directly.
75 Instead make it a weak alias. */
76 extern void __error (int status, int errnum, const char *message, ...)
77 __attribute__ ((__format__ (__printf__, 3, 4)));
78 extern void __error_at_line (int status, int errnum, const char *file_name,
79 unsigned int line_number, const char *message,
80 ...)
81 __attribute__ ((__format__ (__printf__, 5, 6)));;
82 # define error __error
83 # define error_at_line __error_at_line
85 # ifdef USE_IN_LIBIO
86 # include <libio/iolibio.h>
87 # define fflush(s) _IO_fflush (s)
88 # endif
90 #else /* not _LIBC */
92 /* The calling program should define program_name and set it to the
93 name of the executing program. */
94 extern char *program_name;
96 # ifdef HAVE_STRERROR_R
97 # define __strerror_r strerror_r
98 # else
99 # if HAVE_STRERROR
100 # ifndef strerror /* On some systems, strerror is a macro */
101 char *strerror ();
102 # endif
103 # else
104 static char *
105 private_strerror (errnum)
106 int errnum;
108 extern char *sys_errlist[];
109 extern int sys_nerr;
111 if (errnum > 0 && errnum <= sys_nerr)
112 return _(sys_errlist[errnum]);
113 return _("Unknown system error");
115 # define strerror private_strerror
116 # endif /* HAVE_STRERROR */
117 # endif /* HAVE_STRERROR_R */
118 #endif /* not _LIBC */
120 /* Print the program name and error message MESSAGE, which is a printf-style
121 format string with optional args.
122 If ERRNUM is nonzero, print its corresponding system error message.
123 Exit with status STATUS if it is nonzero. */
124 /* VARARGS */
126 void
127 #if defined VA_START && __STDC__
128 error (int status, int errnum, const char *message, ...)
129 #else
130 error (status, errnum, message, va_alist)
131 int status;
132 int errnum;
133 char *message;
134 va_dcl
135 #endif
137 #ifdef VA_START
138 va_list args;
139 #endif
141 fflush (stdout);
142 if (error_print_progname)
143 (*error_print_progname) ();
144 else
145 fprintf (stderr, "%s: ", program_name);
147 #ifdef VA_START
148 VA_START (args, message);
149 # if HAVE_VPRINTF || _LIBC
150 vfprintf (stderr, message, args);
151 # else
152 _doprnt (message, args, stderr);
153 # endif
154 va_end (args);
155 #else
156 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
157 #endif
159 ++error_message_count;
160 if (errnum)
162 #if defined HAVE_STRERROR_R || _LIBC
163 char errbuf[1024];
164 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
165 #else
166 fprintf (stderr, ": %s", strerror (errnum));
167 #endif
169 putc ('\n', stderr);
170 fflush (stderr);
171 if (status)
172 exit (status);
175 /* Sometimes we want to have at most one error per line. This
176 variable controls whether this mode is selected or not. */
177 int error_one_per_line;
179 void
180 #if defined VA_START && __STDC__
181 error_at_line (int status, int errnum, const char *file_name,
182 unsigned int line_number, const char *message, ...)
183 #else
184 error_at_line (status, errnum, file_name, line_number, message, va_alist)
185 int status;
186 int errnum;
187 const char *file_name;
188 unsigned int line_number;
189 char *message;
190 va_dcl
191 #endif
193 #ifdef VA_START
194 va_list args;
195 #endif
197 if (error_one_per_line)
199 static const char *old_file_name;
200 static unsigned int old_line_number;
202 if (old_line_number == line_number &&
203 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
204 /* Simply return and print nothing. */
205 return;
207 old_file_name = file_name;
208 old_line_number = line_number;
211 fflush (stdout);
212 if (error_print_progname)
213 (*error_print_progname) ();
214 else
215 fprintf (stderr, "%s:", program_name);
217 if (file_name != NULL)
218 fprintf (stderr, "%s:%d: ", file_name, line_number);
220 #ifdef VA_START
221 VA_START (args, message);
222 # if HAVE_VPRINTF || _LIBC
223 vfprintf (stderr, message, args);
224 # else
225 _doprnt (message, args, stderr);
226 # endif
227 va_end (args);
228 #else
229 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
230 #endif
232 ++error_message_count;
233 if (errnum)
235 #if defined HAVE_STRERROR_R || _LIBC
236 char errbuf[1024];
237 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
238 #else
239 fprintf (stderr, ": %s", strerror (errnum));
240 #endif
242 putc ('\n', stderr);
243 fflush (stderr);
244 if (status)
245 exit (status);
248 #ifdef _LIBC
249 /* Make the weak alias. */
250 # undef error
251 # undef error_at_line
252 weak_alias (__error, error)
253 weak_alias (__error_at_line, error_at_line)
254 #endif