Include <string.h>.
[glibc.git] / misc / error.c
blobaa0d8bbea713c16bceb97f627988d29f0a66b90d
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>
29 #ifdef _LIBC
30 # include <wchar.h>
31 # define mbsrtowcs __mbsrtowcs
32 #endif
34 #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
35 # if __STDC__
36 # include <stdarg.h>
37 # define VA_START(args, lastarg) va_start(args, lastarg)
38 # else
39 # include <varargs.h>
40 # define VA_START(args, lastarg) va_start(args)
41 # endif
42 #else
43 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
44 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
45 #endif
47 #if STDC_HEADERS || _LIBC
48 # include <stdlib.h>
49 # include <string.h>
50 #else
51 void exit ();
52 #endif
54 #include "error.h"
56 #ifndef _
57 # define _(String) String
58 #endif
60 /* If NULL, error will flush stdout, then print on stderr the program
61 name, a colon and a space. Otherwise, error will call this
62 function without parameters instead. */
63 void (*error_print_progname) (
64 #if __STDC__ - 0
65 void
66 #endif
69 /* This variable is incremented each time `error' is called. */
70 unsigned int error_message_count;
72 #ifdef _LIBC
73 /* In the GNU C library, there is a predefined variable for this. */
75 # define program_name program_invocation_name
76 # include <errno.h>
78 /* In GNU libc we want do not want to use the common name `error' directly.
79 Instead make it a weak alias. */
80 extern void __error (int status, int errnum, const char *message, ...)
81 __attribute__ ((__format__ (__printf__, 3, 4)));
82 extern void __error_at_line (int status, int errnum, const char *file_name,
83 unsigned int line_number, const char *message,
84 ...)
85 __attribute__ ((__format__ (__printf__, 5, 6)));;
86 # define error __error
87 # define error_at_line __error_at_line
89 # ifdef USE_IN_LIBIO
90 # include <libio/iolibio.h>
91 # define fflush(s) _IO_fflush (s)
92 # endif
94 #else /* not _LIBC */
96 /* The calling program should define program_name and set it to the
97 name of the executing program. */
98 extern char *program_name;
100 # ifdef HAVE_STRERROR_R
101 # define __strerror_r strerror_r
102 # else
103 # if HAVE_STRERROR
104 # ifndef strerror /* On some systems, strerror is a macro */
105 char *strerror ();
106 # endif
107 # else
108 static char *
109 private_strerror (errnum)
110 int errnum;
112 extern char *sys_errlist[];
113 extern int sys_nerr;
115 if (errnum > 0 && errnum <= sys_nerr)
116 return _(sys_errlist[errnum]);
117 return _("Unknown system error");
119 # define strerror private_strerror
120 # endif /* HAVE_STRERROR */
121 # endif /* HAVE_STRERROR_R */
122 #endif /* not _LIBC */
125 #ifdef VA_START
126 static void
127 error_tail (int status, int errnum, const char *message, va_list args)
129 # if HAVE_VPRINTF || _LIBC
130 # if _LIBC && USE_IN_LIBIO
131 if (_IO_fwide (stderr, 0) > 0)
133 # define ALLOCA_LIMIT 2000
134 size_t len = strlen (message) + 1;
135 wchar_t *wmessage = NULL;
136 mbstate_t st;
137 size_t res;
138 const char *tmp;
142 if (len < ALLOCA_LIMIT)
143 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
144 else
146 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
147 wmessage = NULL;
149 wmessage = (wchar_t *) realloc (wmessage,
150 len * sizeof (wchar_t));
152 if (wmessage == NULL)
154 fputws_unlocked (L"out of memory\n", stderr);
155 return;
159 memset (&st, '\0', sizeof (st));
160 tmp =message;
162 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
164 if (res == (size_t) -1)
165 /* The string cannot be converted. */
166 wmessage = (wchar_t *) L"???";
168 __vfwprintf (stderr, wmessage, args);
170 else
171 # endif
172 vfprintf (stderr, message, args);
173 # else
174 _doprnt (message, args, stderr);
175 # endif
176 va_end (args);
178 ++error_message_count;
179 if (errnum)
181 #if defined HAVE_STRERROR_R || _LIBC
182 char errbuf[1024];
183 char *s = __strerror_r (errnum, errbuf, sizeof errbuf);
184 # if _LIBC && USE_IN_LIBIO
185 if (_IO_fwide (stderr, 0) > 0)
186 __fwprintf (stderr, L": %s", s);
187 else
188 # endif
189 fprintf (stderr, ": %s", s);
190 #else
191 fprintf (stderr, ": %s", strerror (errnum));
192 #endif
194 #if _LIBC && USE_IN_LIBIO
195 if (_IO_fwide (stderr, 0) > 0)
196 putwc (L'\n', stderr);
197 else
198 #endif
199 putc ('\n', stderr);
200 fflush (stderr);
201 if (status)
202 exit (status);
204 #endif
207 /* Print the program name and error message MESSAGE, which is a printf-style
208 format string with optional args.
209 If ERRNUM is nonzero, print its corresponding system error message.
210 Exit with status STATUS if it is nonzero. */
211 /* VARARGS */
212 void
213 #if defined VA_START && __STDC__
214 error (int status, int errnum, const char *message, ...)
215 #else
216 error (status, errnum, message, va_alist)
217 int status;
218 int errnum;
219 char *message;
220 va_dcl
221 #endif
223 #ifdef VA_START
224 va_list args;
225 #endif
227 fflush (stdout);
228 #ifdef _LIBC
229 # ifdef USE_IN_LIBIO
230 _IO_flockfile (stderr);
231 # else
232 __flockfile (stderr);
233 # endif
234 #endif
235 if (error_print_progname)
236 (*error_print_progname) ();
237 else
239 #if _LIBC && USE_IN_LIBIO
240 if (_IO_fwide (stderr, 0) > 0)
241 __fwprintf (stderr, L"%s: ", program_name);
242 else
243 #endif
244 fprintf (stderr, "%s: ", program_name);
247 #ifdef VA_START
248 VA_START (args, message);
249 error_tail (status, errnum, message, args);
250 #else
251 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
253 ++error_message_count;
254 if (errnum)
255 fprintf (stderr, ": %s", strerror (errnum));
256 putc ('\n', stderr);
257 fflush (stderr);
258 if (status)
259 exit (status);
260 #endif
262 #ifdef _LIBC
263 # ifdef USE_IN_LIBIO
264 _IO_funlockfile (stderr);
265 # else
266 __funlockfile (stderr);
267 # endif
268 #endif
271 /* Sometimes we want to have at most one error per line. This
272 variable controls whether this mode is selected or not. */
273 int error_one_per_line;
275 void
276 #if defined VA_START && __STDC__
277 error_at_line (int status, int errnum, const char *file_name,
278 unsigned int line_number, const char *message, ...)
279 #else
280 error_at_line (status, errnum, file_name, line_number, message, va_alist)
281 int status;
282 int errnum;
283 const char *file_name;
284 unsigned int line_number;
285 char *message;
286 va_dcl
287 #endif
289 #ifdef VA_START
290 va_list args;
291 #endif
293 if (error_one_per_line)
295 static const char *old_file_name;
296 static unsigned int old_line_number;
298 if (old_line_number == line_number
299 && (file_name == old_file_name
300 || strcmp (old_file_name, file_name) == 0))
301 /* Simply return and print nothing. */
302 return;
304 old_file_name = file_name;
305 old_line_number = line_number;
308 fflush (stdout);
309 #ifdef _LIBC
310 # ifdef USE_IN_LIBIO
311 _IO_flockfile (stderr);
312 # else
313 __flockfile (stderr);
314 # endif
315 #endif
316 if (error_print_progname)
317 (*error_print_progname) ();
318 else
320 #if _LIBC && USE_IN_LIBIO
321 if (_IO_fwide (stderr, 0) > 0)
322 __fwprintf (stderr, L"%s: ", program_name);
323 else
324 #endif
325 fprintf (stderr, "%s:", program_name);
328 if (file_name != NULL)
330 #if _LIBC && USE_IN_LIBIO
331 if (_IO_fwide (stderr, 0) > 0)
332 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
333 else
334 #endif
335 fprintf (stderr, "%s:%d: ", file_name, line_number);
338 #ifdef VA_START
339 VA_START (args, message);
340 error_tail (status, errnum, message, args);
341 #else
342 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
344 ++error_message_count;
345 if (errnum)
346 fprintf (stderr, ": %s", strerror (errnum));
347 putc ('\n', stderr);
348 fflush (stderr);
349 if (status)
350 exit (status);
351 #endif
353 #ifdef _LIBC
354 # ifdef USE_IN_LIBIO
355 _IO_funlockfile (stderr);
356 # else
357 __funlockfile (stderr);
358 # endif
359 #endif
362 #ifdef _LIBC
363 /* Make the weak alias. */
364 # undef error
365 # undef error_at_line
366 weak_alias (__error, error)
367 weak_alias (__error_at_line, error_at_line)
368 #endif