summaryrefslogtreecommitdiffstats
path: root/lib/error.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/error.c')
-rw-r--r--lib/error.c304
1 files changed, 0 insertions, 304 deletions
diff --git a/lib/error.c b/lib/error.c
deleted file mode 100644
index 45698be..0000000
--- a/lib/error.c
+++ /dev/null
@@ -1,304 +0,0 @@
1/* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20
21#ifdef HAVE_CONFIG_H
22# include <config.h>
23#endif
24
25#include "error.h"
26
27#include <stdarg.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#if !_LIBC && ENABLE_NLS
33# include "gettext.h"
34#endif
35
36#ifdef _LIBC
37# include <wchar.h>
38# define mbsrtowcs __mbsrtowcs
39#endif
40
41#if USE_UNLOCKED_IO
42# include "unlocked-io.h"
43#endif
44
45#ifndef _
46# define _(String) String
47#endif
48
49/* If NULL, error will flush stdout, then print on stderr the program
50 name, a colon and a space. Otherwise, error will call this
51 function without parameters instead. */
52void (*error_print_progname) (void);
53
54/* This variable is incremented each time `error' is called. */
55unsigned int error_message_count;
56
57#ifdef _LIBC
58/* In the GNU C library, there is a predefined variable for this. */
59
60# define program_name program_invocation_name
61# include <errno.h>
62# include <libio/libioP.h>
63
64/* In GNU libc we want do not want to use the common name `error' directly.
65 Instead make it a weak alias. */
66extern void __error (int status, int errnum, const char *message, ...)
67 __attribute__ ((__format__ (__printf__, 3, 4)));
68extern void __error_at_line (int status, int errnum, const char *file_name,
69 unsigned int line_number, const char *message,
70 ...)
71 __attribute__ ((__format__ (__printf__, 5, 6)));;
72# define error __error
73# define error_at_line __error_at_line
74
75# include <libio/iolibio.h>
76# define fflush(s) INTUSE(_IO_fflush) (s)
77# undef putc
78# define putc(c, fp) INTUSE(_IO_putc) (c, fp)
79
80# include <bits/libc-lock.h>
81
82#else /* not _LIBC */
83
84# if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
85# ifndef HAVE_DECL_STRERROR_R
86"this configure-time declaration test was not run"
87# endif
88char *strerror_r ();
89# endif
90
91# ifndef SIZE_MAX
92# define SIZE_MAX ((size_t) -1)
93# endif
94
95/* The calling program should define program_name and set it to the
96 name of the executing program. */
97extern char *program_name;
98
99# if HAVE_STRERROR_R || defined strerror_r
100# define __strerror_r strerror_r
101# endif
102#endif /* not _LIBC */
103
104static void
105print_errno_message (int errnum)
106{
107 char const *s = NULL;
108
109#if defined HAVE_STRERROR_R || _LIBC
110 char errbuf[1024];
111# if STRERROR_R_CHAR_P || _LIBC
112 s = __strerror_r (errnum, errbuf, sizeof errbuf);
113# else
114 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
115 s = errbuf;
116# endif
117#endif
118
119#if !_LIBC
120 if (! s && ! (s = strerror (errnum)))
121 s = _("Unknown system error");
122#endif
123
124#if _LIBC
125 if (_IO_fwide (stderr, 0) > 0)
126 {
127 __fwprintf (stderr, L": %s", s);
128 return;
129 }
130#endif
131
132 fprintf (stderr, ": %s", s);
133}
134
135static void
136error_tail (int status, int errnum, const char *message, va_list args)
137{
138#if _LIBC
139 if (_IO_fwide (stderr, 0) > 0)
140 {
141# define ALLOCA_LIMIT 2000
142 size_t len = strlen (message) + 1;
143 const wchar_t *wmessage = L"out of memory";
144 wchar_t *wbuf = (len < ALLOCA_LIMIT
145 ? alloca (len * sizeof *wbuf)
146 : len <= SIZE_MAX / sizeof *wbuf
147 ? malloc (len * sizeof *wbuf)
148 : NULL);
149
150 if (wbuf)
151 {
152 size_t res;
153 mbstate_t st;
154 const char *tmp = message;
155 memset (&st, '\0', sizeof (st));
156 res = mbsrtowcs (wbuf, &tmp, len, &st);
157 wmessage = res == (size_t) -1 ? L"???" : wbuf;
158 }
159
160 __vfwprintf (stderr, wmessage, args);
161 if (! (len < ALLOCA_LIMIT))
162 free (wbuf);
163 }
164 else
165#endif
166 vfprintf (stderr, message, args);
167 va_end (args);
168
169 ++error_message_count;
170 if (errnum)
171 print_errno_message (errnum);
172#if _LIBC
173 if (_IO_fwide (stderr, 0) > 0)
174 putwc (L'\n', stderr);
175 else
176#endif
177 putc ('\n', stderr);
178 fflush (stderr);
179 if (status)
180 exit (status);
181}
182
183
184/* Print the program name and error message MESSAGE, which is a printf-style
185 format string with optional args.
186 If ERRNUM is nonzero, print its corresponding system error message.
187 Exit with status STATUS if it is nonzero. */
188void
189error (int status, int errnum, const char *message, ...)
190{
191 va_list args;
192
193#if defined _LIBC && defined __libc_ptf_call
194 /* We do not want this call to be cut short by a thread
195 cancellation. Therefore disable cancellation for now. */
196 int state = PTHREAD_CANCEL_ENABLE;
197 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
198 0);
199#endif
200
201 fflush (stdout);
202#ifdef _LIBC
203 _IO_flockfile (stderr);
204#endif
205 if (error_print_progname)
206 (*error_print_progname) ();
207 else
208 {
209#if _LIBC
210 if (_IO_fwide (stderr, 0) > 0)
211 __fwprintf (stderr, L"%s: ", program_name);
212 else
213#endif
214 fprintf (stderr, "%s: ", program_name);
215 }
216
217 va_start (args, message);
218 error_tail (status, errnum, message, args);
219
220#ifdef _LIBC
221 _IO_funlockfile (stderr);
222# ifdef __libc_ptf_call
223 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
224# endif
225#endif
226}
227
228/* Sometimes we want to have at most one error per line. This
229 variable controls whether this mode is selected or not. */
230int error_one_per_line;
231
232void
233error_at_line (int status, int errnum, const char *file_name,
234 unsigned int line_number, const char *message, ...)
235{
236 va_list args;
237
238 if (error_one_per_line)
239 {
240 static const char *old_file_name;
241 static unsigned int old_line_number;
242
243 if (old_line_number == line_number
244 && (file_name == old_file_name
245 || strcmp (old_file_name, file_name) == 0))
246 /* Simply return and print nothing. */
247 return;
248
249 old_file_name = file_name;
250 old_line_number = line_number;
251 }
252
253#if defined _LIBC && defined __libc_ptf_call
254 /* We do not want this call to be cut short by a thread
255 cancellation. Therefore disable cancellation for now. */
256 int state = PTHREAD_CANCEL_ENABLE;
257 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
258 0);
259#endif
260
261 fflush (stdout);
262#ifdef _LIBC
263 _IO_flockfile (stderr);
264#endif
265 if (error_print_progname)
266 (*error_print_progname) ();
267 else
268 {
269#if _LIBC
270 if (_IO_fwide (stderr, 0) > 0)
271 __fwprintf (stderr, L"%s: ", program_name);
272 else
273#endif
274 fprintf (stderr, "%s:", program_name);
275 }
276
277 if (file_name != NULL)
278 {
279#if _LIBC
280 if (_IO_fwide (stderr, 0) > 0)
281 __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
282 else
283#endif
284 fprintf (stderr, "%s:%d: ", file_name, line_number);
285 }
286
287 va_start (args, message);
288 error_tail (status, errnum, message, args);
289
290#ifdef _LIBC
291 _IO_funlockfile (stderr);
292# ifdef __libc_ptf_call
293 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
294# endif
295#endif
296}
297
298#ifdef _LIBC
299/* Make the weak alias. */
300# undef error
301# undef error_at_line
302weak_alias (__error, error)
303weak_alias (__error_at_line, error_at_line)
304#endif