source: vendor/glibc/current/misc/error.c

Last change on this file was 1036, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 8.7 KB
Line 
1/* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000, 2001, 2002 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.
5
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.
10
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.
15
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. */
20
21/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
22
23#ifdef HAVE_CONFIG_H
24# include <config.h>
25#endif
26
27#include <stdio.h>
28#include <libintl.h>
29#ifdef _LIBC
30# include <wchar.h>
31# define mbsrtowcs __mbsrtowcs
32#endif
33
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
46
47#if STDC_HEADERS || _LIBC
48# include <stdlib.h>
49# include <string.h>
50#else
51void exit ();
52#endif
53
54#include "error.h"
55
56#ifndef _
57# define _(String) String
58#endif
59
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. */
63void (*error_print_progname) (
64#if __STDC__ - 0
65 void
66#endif
67 );
68
69/* This variable is incremented each time `error' is called. */
70unsigned int error_message_count;
71
72#ifdef _LIBC
73/* In the GNU C library, there is a predefined variable for this. */
74
75# define program_name program_invocation_name
76# include <errno.h>
77# include <libio/libioP.h>
78
79/* In GNU libc we want do not want to use the common name `error' directly.
80 Instead make it a weak alias. */
81extern void __error (int status, int errnum, const char *message, ...)
82 __attribute__ ((__format__ (__printf__, 3, 4)));
83extern void __error_at_line (int status, int errnum, const char *file_name,
84 unsigned int line_number, const char *message,
85 ...)
86 __attribute__ ((__format__ (__printf__, 5, 6)));;
87# define error __error
88# define error_at_line __error_at_line
89
90# include <libio/iolibio.h>
91# define fflush(s) INTUSE(_IO_fflush) (s)
92# undef putc
93# define putc(c, fp) INTUSE(_IO_putc) (c, fp)
94
95#else /* not _LIBC */
96
97# if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
98# ifndef HAVE_DECL_STRERROR_R
99"this configure-time declaration test was not run"
100# endif
101char *strerror_r ();
102# endif
103
104/* The calling program should define program_name and set it to the
105 name of the executing program. */
106extern char *program_name;
107
108# if HAVE_STRERROR_R || defined strerror_r
109# define __strerror_r strerror_r
110# else
111# if HAVE_STRERROR
112# ifndef HAVE_DECL_STRERROR
113"this configure-time declaration test was not run"
114# endif
115# if !HAVE_DECL_STRERROR
116char *strerror ();
117# endif
118# else
119static char *
120private_strerror (int errnum)
121{
122 extern char *sys_errlist[];
123 extern int sys_nerr;
124
125 if (errnum > 0 && errnum <= sys_nerr)
126 return _(sys_errlist[errnum]);
127 return _("Unknown system error");
128}
129# define strerror private_strerror
130# endif /* HAVE_STRERROR */
131# endif /* HAVE_STRERROR_R || defined strerror_r */
132#endif /* not _LIBC */
133
134static void
135print_errno_message (int errnum)
136{
137 char const *s;
138
139#if defined HAVE_STRERROR_R || _LIBC
140 char errbuf[1024];
141# if STRERROR_R_CHAR_P || _LIBC
142 s = __strerror_r (errnum, errbuf, sizeof errbuf);
143# else
144 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
145 s = errbuf;
146 else
147 s = 0;
148# endif
149#else
150 s = strerror (errnum);
151#endif
152
153#if !_LIBC
154 if (! s)
155 s = _("Unknown system error");
156#endif
157
158#if _LIBC
159 if (_IO_fwide (stderr, 0) > 0)
160 {
161 __fwprintf (stderr, L": %s", s);
162 return;
163 }
164#endif
165
166 fprintf (stderr, ": %s", s);
167}
168
169#ifdef VA_START
170static void
171error_tail (int status, int errnum, const char *message, va_list args)
172{
173# if HAVE_VPRINTF || _LIBC
174# if _LIBC
175 if (_IO_fwide (stderr, 0) > 0)
176 {
177# define ALLOCA_LIMIT 2000
178 size_t len = strlen (message) + 1;
179 wchar_t *wmessage = NULL;
180 mbstate_t st;
181 size_t res;
182 const char *tmp;
183
184 do
185 {
186 if (len < ALLOCA_LIMIT)
187 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
188 else
189 {
190 if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
191 wmessage = NULL;
192
193 wmessage = (wchar_t *) realloc (wmessage,
194 len * sizeof (wchar_t));
195
196 if (wmessage == NULL)
197 {
198 fputws_unlocked (L"out of memory\n", stderr);
199 return;
200 }
201 }
202
203 memset (&st, '\0', sizeof (st));
204 tmp =message;
205 }
206 while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
207
208 if (res == (size_t) -1)
209 /* The string cannot be converted. */
210 wmessage = (wchar_t *) L"???";
211
212 __vfwprintf (stderr, wmessage, args);
213 }
214 else
215# endif
216 vfprintf (stderr, message, args);
217# else
218 _doprnt (message, args, stderr);
219# endif
220 va_end (args);
221
222 ++error_message_count;
223 if (errnum)
224 print_errno_message (errnum);
225# if _LIBC
226 if (_IO_fwide (stderr, 0) > 0)
227 putwc (L'\n', stderr);
228 else
229# endif
230 putc ('\n', stderr);
231 fflush (stderr);
232 if (status)
233 exit (status);
234}
235#endif
236
237
238/* Print the program name and error message MESSAGE, which is a printf-style
239 format string with optional args.
240 If ERRNUM is nonzero, print its corresponding system error message.
241 Exit with status STATUS if it is nonzero. */
242/* VARARGS */
243void
244#if defined VA_START && __STDC__
245error (int status, int errnum, const char *message, ...)
246#else
247error (status, errnum, message, va_alist)
248 int status;
249 int errnum;
250 char *message;
251 va_dcl
252#endif
253{
254#ifdef VA_START
255 va_list args;
256#endif
257
258 fflush (stdout);
259#ifdef _LIBC
260 _IO_flockfile (stderr);
261#endif
262 if (error_print_progname)
263 (*error_print_progname) ();
264 else
265 {
266#if _LIBC
267 if (_IO_fwide (stderr, 0) > 0)
268 __fwprintf (stderr, L"%s: ", program_name);
269 else
270#endif
271 fprintf (stderr, "%s: ", program_name);
272 }
273
274#ifdef VA_START
275 VA_START (args, message);
276 error_tail (status, errnum, message, args);
277#else
278 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
279
280 ++error_message_count;
281 if (errnum)
282 print_errno_message (errnum);
283 putc ('\n', stderr);
284 fflush (stderr);
285 if (status)
286 exit (status);
287#endif
288
289#ifdef _LIBC
290 _IO_funlockfile (stderr);
291#endif
292}
293
294
295/* Sometimes we want to have at most one error per line. This
296 variable controls whether this mode is selected or not. */
297int error_one_per_line;
298
299void
300#if defined VA_START && __STDC__
301error_at_line (int status, int errnum, const char *file_name,
302 unsigned int line_number, const char *message, ...)
303#else
304error_at_line (status, errnum, file_name, line_number, message, va_alist)
305 int status;
306 int errnum;
307 const char *file_name;
308 unsigned int line_number;
309 char *message;
310 va_dcl
311#endif
312{
313#ifdef VA_START
314 va_list args;
315#endif
316
317 if (error_one_per_line)
318 {
319 static const char *old_file_name;
320 static unsigned int old_line_number;
321
322 if (old_line_number == line_number
323 && (file_name == old_file_name
324 || strcmp (old_file_name, file_name) == 0))
325 /* Simply return and print nothing. */
326 return;
327
328 old_file_name = file_name;
329 old_line_number = line_number;
330 }
331
332 fflush (stdout);
333#ifdef _LIBC
334 _IO_flockfile (stderr);
335#endif
336 if (error_print_progname)
337 (*error_print_progname) ();
338 else
339 {
340#if _LIBC
341 if (_IO_fwide (stderr, 0) > 0)
342 __fwprintf (stderr, L"%s: ", program_name);
343 else
344#endif
345 fprintf (stderr, "%s:", program_name);
346 }
347
348 if (file_name != NULL)
349 {
350#if _LIBC
351 if (_IO_fwide (stderr, 0) > 0)
352 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
353 else
354#endif
355 fprintf (stderr, "%s:%d: ", file_name, line_number);
356 }
357
358#ifdef VA_START
359 VA_START (args, message);
360 error_tail (status, errnum, message, args);
361#else
362 fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
363
364 ++error_message_count;
365 if (errnum)
366 print_errno_message (errnum);
367 putc ('\n', stderr);
368 fflush (stderr);
369 if (status)
370 exit (status);
371#endif
372
373#ifdef _LIBC
374 _IO_funlockfile (stderr);
375#endif
376}
377
378#ifdef _LIBC
379/* Make the weak alias. */
380# undef error
381# undef error_at_line
382weak_alias (__error, error)
383weak_alias (__error_at_line, error_at_line)
384#endif
Note: See TracBrowser for help on using the repository browser.