Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / gnulib / error.c
blobed9dba0d27c6a3e7c4a8a8938ca27685e6af97af
1 /* Error handler for noninteractive utilities
2 Copyright (C) 1990-1998, 2000-2007, 2009-2010 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20 #if !_LIBC
21 # include <config.h>
22 #endif
24 #include "error.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #if !_LIBC && ENABLE_NLS
32 # include "gettext.h"
33 # define _(msgid) gettext (msgid)
34 #endif
36 #ifdef _LIBC
37 # include <libintl.h>
38 # include <stdbool.h>
39 # include <stdint.h>
40 # include <wchar.h>
41 # define mbsrtowcs __mbsrtowcs
42 #endif
44 #if USE_UNLOCKED_IO
45 # include "unlocked-io.h"
46 #endif
48 #ifndef _
49 # define _(String) String
50 #endif
52 /* If NULL, error will flush stdout, then print on stderr the program
53 name, a colon and a space. Otherwise, error will call this
54 function without parameters instead. */
55 void (*error_print_progname) (void);
57 /* This variable is incremented each time `error' is called. */
58 unsigned int error_message_count;
60 #ifdef _LIBC
61 /* In the GNU C library, there is a predefined variable for this. */
63 # define program_name program_invocation_name
64 # include <errno.h>
65 # include <limits.h>
66 # include <libio/libioP.h>
68 /* In GNU libc we want do not want to use the common name `error' directly.
69 Instead make it a weak alias. */
70 extern void __error (int status, int errnum, const char *message, ...)
71 __attribute__ ((__format__ (__printf__, 3, 4)));
72 extern void __error_at_line (int status, int errnum, const char *file_name,
73 unsigned int line_number, const char *message,
74 ...)
75 __attribute__ ((__format__ (__printf__, 5, 6)));;
76 # define error __error
77 # define error_at_line __error_at_line
79 # include <libio/iolibio.h>
80 # define fflush(s) INTUSE(_IO_fflush) (s)
81 # undef putc
82 # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
84 # include <bits/libc-lock.h>
86 #else /* not _LIBC */
88 # include <fcntl.h>
89 # include <unistd.h>
91 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
92 /* Get declarations of the Win32 API functions. */
93 # define WIN32_LEAN_AND_MEAN
94 # include <windows.h>
95 # endif
97 /* The gnulib override of fcntl is not needed in this file. */
98 # undef fcntl
100 # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
101 # ifndef HAVE_DECL_STRERROR_R
102 "this configure-time declaration test was not run"
103 # endif
104 char *strerror_r ();
105 # endif
107 /* The calling program should define program_name and set it to the
108 name of the executing program. */
109 extern char *program_name;
111 # if HAVE_STRERROR_R || defined strerror_r
112 # define __strerror_r strerror_r
113 # endif /* HAVE_STRERROR_R || defined strerror_r */
114 #endif /* not _LIBC */
116 #if !_LIBC
117 /* Return non-zero if FD is open. */
118 static inline int
119 is_open (int fd)
121 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
122 /* On Win32: The initial state of unassigned standard file descriptors is
123 that they are open but point to an INVALID_HANDLE_VALUE. There is no
124 fcntl, and the gnulib replacement fcntl does not support F_GETFL. */
125 return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
126 # else
127 # ifndef F_GETFL
128 # error Please port fcntl to your platform
129 # endif
130 return 0 <= fcntl (fd, F_GETFL);
131 # endif
133 #endif
135 static inline void
136 flush_stdout (void)
138 #if !_LIBC
139 int stdout_fd;
141 # if GNULIB_FREOPEN_SAFER
142 /* Use of gnulib's freopen-safer module normally ensures that
143 fileno (stdout) == 1
144 whenever stdout is open. */
145 stdout_fd = STDOUT_FILENO;
146 # else
147 /* POSIX states that fileno (stdout) after fclose is unspecified. But in
148 practice it is not a problem, because stdout is statically allocated and
149 the fd of a FILE stream is stored as a field in its allocated memory. */
150 stdout_fd = fileno (stdout);
151 # endif
152 /* POSIX states that fflush (stdout) after fclose is unspecified; it
153 is safe in glibc, but not on all other platforms. fflush (NULL)
154 is always defined, but too draconian. */
155 if (0 <= stdout_fd && is_open (stdout_fd))
156 #endif
157 fflush (stdout);
160 static void
161 print_errno_message (int errnum)
163 char const *s;
165 #if defined HAVE_STRERROR_R || _LIBC
166 char errbuf[1024];
167 # if STRERROR_R_CHAR_P || _LIBC
168 s = __strerror_r (errnum, errbuf, sizeof errbuf);
169 # else
170 if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
171 s = errbuf;
172 else
173 s = 0;
174 # endif
175 #else
176 s = strerror (errnum);
177 #endif
179 #if !_LIBC
180 if (! s)
181 s = _("Unknown system error");
182 #endif
184 #if _LIBC
185 __fxprintf (NULL, ": %s", s);
186 #else
187 fprintf (stderr, ": %s", s);
188 #endif
191 static void
192 error_tail (int status, int errnum, const char *message, va_list args)
194 #if _LIBC
195 if (_IO_fwide (stderr, 0) > 0)
197 # define ALLOCA_LIMIT 2000
198 size_t len = strlen (message) + 1;
199 wchar_t *wmessage = NULL;
200 mbstate_t st;
201 size_t res;
202 const char *tmp;
203 bool use_malloc = false;
205 while (1)
207 if (__libc_use_alloca (len * sizeof (wchar_t)))
208 wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
209 else
211 if (!use_malloc)
212 wmessage = NULL;
214 wchar_t *p = (wchar_t *) realloc (wmessage,
215 len * sizeof (wchar_t));
216 if (p == NULL)
218 free (wmessage);
219 fputws_unlocked (L"out of memory\n", stderr);
220 return;
222 wmessage = p;
223 use_malloc = true;
226 memset (&st, '\0', sizeof (st));
227 tmp = message;
229 res = mbsrtowcs (wmessage, &tmp, len, &st);
230 if (res != len)
231 break;
233 if (__builtin_expect (len >= SIZE_MAX / 2, 0))
235 /* This really should not happen if everything is fine. */
236 res = (size_t) -1;
237 break;
240 len *= 2;
243 if (res == (size_t) -1)
245 /* The string cannot be converted. */
246 if (use_malloc)
248 free (wmessage);
249 use_malloc = false;
251 wmessage = (wchar_t *) L"???";
254 __vfwprintf (stderr, wmessage, args);
256 if (use_malloc)
257 free (wmessage);
259 else
260 #endif
261 vfprintf (stderr, message, args);
262 va_end (args);
264 ++error_message_count;
265 if (errnum)
266 print_errno_message (errnum);
267 #if _LIBC
268 __fxprintf (NULL, "\n");
269 #else
270 putc ('\n', stderr);
271 #endif
272 fflush (stderr);
273 if (status)
274 exit (status);
278 /* Print the program name and error message MESSAGE, which is a printf-style
279 format string with optional args.
280 If ERRNUM is nonzero, print its corresponding system error message.
281 Exit with status STATUS if it is nonzero. */
282 void
283 error (int status, int errnum, const char *message, ...)
285 va_list args;
287 #if defined _LIBC && defined __libc_ptf_call
288 /* We do not want this call to be cut short by a thread
289 cancellation. Therefore disable cancellation for now. */
290 int state = PTHREAD_CANCEL_ENABLE;
291 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
293 #endif
295 flush_stdout ();
296 #ifdef _LIBC
297 _IO_flockfile (stderr);
298 #endif
299 if (error_print_progname)
300 (*error_print_progname) ();
301 else
303 #if _LIBC
304 __fxprintf (NULL, "%s: ", program_name);
305 #else
306 fprintf (stderr, "%s: ", program_name);
307 #endif
310 va_start (args, message);
311 error_tail (status, errnum, message, args);
313 #ifdef _LIBC
314 _IO_funlockfile (stderr);
315 # ifdef __libc_ptf_call
316 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
317 # endif
318 #endif
321 /* Sometimes we want to have at most one error per line. This
322 variable controls whether this mode is selected or not. */
323 int error_one_per_line;
325 void
326 error_at_line (int status, int errnum, const char *file_name,
327 unsigned int line_number, const char *message, ...)
329 va_list args;
331 if (error_one_per_line)
333 static const char *old_file_name;
334 static unsigned int old_line_number;
336 if (old_line_number == line_number
337 && (file_name == old_file_name
338 || strcmp (old_file_name, file_name) == 0))
339 /* Simply return and print nothing. */
340 return;
342 old_file_name = file_name;
343 old_line_number = line_number;
346 #if defined _LIBC && defined __libc_ptf_call
347 /* We do not want this call to be cut short by a thread
348 cancellation. Therefore disable cancellation for now. */
349 int state = PTHREAD_CANCEL_ENABLE;
350 __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
352 #endif
354 flush_stdout ();
355 #ifdef _LIBC
356 _IO_flockfile (stderr);
357 #endif
358 if (error_print_progname)
359 (*error_print_progname) ();
360 else
362 #if _LIBC
363 __fxprintf (NULL, "%s:", program_name);
364 #else
365 fprintf (stderr, "%s:", program_name);
366 #endif
369 #if _LIBC
370 __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
371 file_name, line_number);
372 #else
373 fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
374 file_name, line_number);
375 #endif
377 va_start (args, message);
378 error_tail (status, errnum, message, args);
380 #ifdef _LIBC
381 _IO_funlockfile (stderr);
382 # ifdef __libc_ptf_call
383 __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
384 # endif
385 #endif
388 #ifdef _LIBC
389 /* Make the weak alias. */
390 # undef error
391 # undef error_at_line
392 weak_alias (__error, error)
393 weak_alias (__error_at_line, error_at_line)
394 #endif