* string/string.h (strndupa): Add cast for C++ conformance.
[glibc.git] / misc / error.c
blob8758bd0adeec86e73fc8b6bf86a4fabda2272163
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000 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 if (error_print_progname)
143 (*error_print_progname) ();
144 else
146 fflush (stdout);
147 fprintf (stderr, "%s: ", program_name);
150 #ifdef VA_START
151 VA_START (args, message);
152 # if HAVE_VPRINTF || _LIBC
153 vfprintf (stderr, message, args);
154 # else
155 _doprnt (message, args, stderr);
156 # endif
157 va_end (args);
158 #else
159 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
160 #endif
162 ++error_message_count;
163 if (errnum)
165 #if defined HAVE_STRERROR_R || _LIBC
166 char errbuf[1024];
167 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
168 #else
169 fprintf (stderr, ": %s", strerror (errnum));
170 #endif
172 putc ('\n', stderr);
173 fflush (stderr);
174 if (status)
175 exit (status);
178 /* Sometimes we want to have at most one error per line. This
179 variable controls whether this mode is selected or not. */
180 int error_one_per_line;
182 void
183 #if defined VA_START && __STDC__
184 error_at_line (int status, int errnum, const char *file_name,
185 unsigned int line_number, const char *message, ...)
186 #else
187 error_at_line (status, errnum, file_name, line_number, message, va_alist)
188 int status;
189 int errnum;
190 const char *file_name;
191 unsigned int line_number;
192 char *message;
193 va_dcl
194 #endif
196 #ifdef VA_START
197 va_list args;
198 #endif
200 if (error_one_per_line)
202 static const char *old_file_name;
203 static unsigned int old_line_number;
205 if (old_line_number == line_number &&
206 (file_name == old_file_name || !strcmp (old_file_name, file_name)))
207 /* Simply return and print nothing. */
208 return;
210 old_file_name = file_name;
211 old_line_number = line_number;
214 if (error_print_progname)
215 (*error_print_progname) ();
216 else
218 fflush (stdout);
219 fprintf (stderr, "%s:", program_name);
222 if (file_name != NULL)
223 fprintf (stderr, "%s:%d: ", file_name, line_number);
225 #ifdef VA_START
226 VA_START (args, message);
227 # if HAVE_VPRINTF || _LIBC
228 vfprintf (stderr, message, args);
229 # else
230 _doprnt (message, args, stderr);
231 # endif
232 va_end (args);
233 #else
234 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
235 #endif
237 ++error_message_count;
238 if (errnum)
240 #if defined HAVE_STRERROR_R || _LIBC
241 char errbuf[1024];
242 fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
243 #else
244 fprintf (stderr, ": %s", strerror (errnum));
245 #endif
247 putc ('\n', stderr);
248 fflush (stderr);
249 if (status)
250 exit (status);
253 #ifdef _LIBC
254 /* Make the weak alias. */
255 # undef error
256 # undef error_at_line
257 weak_alias (__error, error)
258 weak_alias (__error_at_line, error_at_line)
259 #endif