1
1
/* Error handler for noninteractive utilities
2
Copyright (C) 1990-1998, 2000-2007, 2009 Free Software Foundation, Inc.
2
Copyright (C) 1990-1998, 2000-2007, 2009-2011 Free Software Foundation, Inc.
3
3
This file is part of the GNU C Library.
5
5
This program is free software: you can redistribute it and/or modify
70
70
extern void __error (int status, int errnum, const char *message, ...)
71
71
__attribute__ ((__format__ (__printf__, 3, 4)));
72
72
extern void __error_at_line (int status, int errnum, const char *file_name,
73
unsigned int line_number, const char *message,
73
unsigned int line_number, const char *message,
75
75
__attribute__ ((__format__ (__printf__, 5, 6)));;
76
76
# define error __error
77
77
# define error_at_line __error_at_line
86
86
#else /* not _LIBC */
88
88
# include <fcntl.h>
90
# if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
91
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
92
/* Get declarations of the Win32 API functions. */
93
# define WIN32_LEAN_AND_MEAN
97
/* The gnulib override of fcntl is not needed in this file. */
100
# if !HAVE_DECL_STRERROR_R
91
101
# ifndef HAVE_DECL_STRERROR_R
92
102
"this configure-time declaration test was not run"
104
# if STRERROR_R_CHAR_P
94
105
char *strerror_r ();
97
111
/* The calling program should define program_name and set it to the
101
115
# if HAVE_STRERROR_R || defined strerror_r
102
116
# define __strerror_r strerror_r
103
# endif /* HAVE_STRERROR_R || defined strerror_r */
104
#endif /* not _LIBC */
117
# endif /* HAVE_STRERROR_R || defined strerror_r */
118
#endif /* not _LIBC */
121
/* Return non-zero if FD is open. */
125
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
126
/* On Win32: The initial state of unassigned standard file descriptors is
127
that they are open but point to an INVALID_HANDLE_VALUE. There is no
128
fcntl, and the gnulib replacement fcntl does not support F_GETFL. */
129
return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
132
# error Please port fcntl to your platform
134
return 0 <= fcntl (fd, F_GETFL);
145
# if GNULIB_FREOPEN_SAFER
146
/* Use of gnulib's freopen-safer module normally ensures that
148
whenever stdout is open. */
149
stdout_fd = STDOUT_FILENO;
151
/* POSIX states that fileno (stdout) after fclose is unspecified. But in
152
practice it is not a problem, because stdout is statically allocated and
153
the fd of a FILE stream is stored as a field in its allocated memory. */
154
stdout_fd = fileno (stdout);
156
/* POSIX states that fflush (stdout) after fclose is unspecified; it
157
is safe in glibc, but not on all other platforms. fflush (NULL)
158
is always defined, but too draconian. */
159
if (0 <= stdout_fd && is_open (stdout_fd))
107
165
print_errno_message (int errnum)
149
207
bool use_malloc = false;
153
if (__libc_use_alloca (len * sizeof (wchar_t)))
154
wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
160
wchar_t *p = (wchar_t *) realloc (wmessage,
161
len * sizeof (wchar_t));
165
fputws_unlocked (L"out of memory\n", stderr);
172
memset (&st, '\0', sizeof (st));
175
res = mbsrtowcs (wmessage, &tmp, len, &st);
179
if (__builtin_expect (len >= SIZE_MAX / 2, 0))
181
/* This really should not happen if everything is fine. */
211
if (__libc_use_alloca (len * sizeof (wchar_t)))
212
wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
218
wchar_t *p = (wchar_t *) realloc (wmessage,
219
len * sizeof (wchar_t));
223
fputws_unlocked (L"out of memory\n", stderr);
230
memset (&st, '\0', sizeof (st));
233
res = mbsrtowcs (wmessage, &tmp, len, &st);
237
if (__builtin_expect (len >= SIZE_MAX / 2, 0))
239
/* This really should not happen if everything is fine. */
189
247
if (res == (size_t) -1)
191
/* The string cannot be converted. */
197
wmessage = (wchar_t *) L"???";
249
/* The string cannot be converted. */
255
wmessage = (wchar_t *) L"???";
200
258
__vfwprintf (stderr, wmessage, args);
235
293
cancellation. Therefore disable cancellation for now. */
236
294
int state = PTHREAD_CANCEL_ENABLE;
237
295
__libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
241
#if !_LIBC && defined F_GETFL
242
/* POSIX states that fflush (stdout) after fclose is unspecified; it
243
is safe in glibc, but not on all other platforms. fflush (NULL)
244
is always defined, but too draconian. */
245
if (0 <= fcntl (1, F_GETFL))
249
301
_IO_flockfile (stderr);
278
330
error_at_line (int status, int errnum, const char *file_name,
279
unsigned int line_number, const char *message, ...)
331
unsigned int line_number, const char *message, ...)
286
338
static unsigned int old_line_number;
288
340
if (old_line_number == line_number
289
&& (file_name == old_file_name
290
|| strcmp (old_file_name, file_name) == 0))
291
/* Simply return and print nothing. */
341
&& (file_name == old_file_name
342
|| strcmp (old_file_name, file_name) == 0))
343
/* Simply return and print nothing. */
294
346
old_file_name = file_name;
295
347
old_line_number = line_number;
300
352
cancellation. Therefore disable cancellation for now. */
301
353
int state = PTHREAD_CANCEL_ENABLE;
302
354
__libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
306
#if !_LIBC && defined F_GETFL
307
/* POSIX states that fflush (stdout) after fclose is unspecified; it
308
is safe in glibc, but not on all other platforms. fflush (NULL)
309
is always defined, but too draconian. */
310
if (0 <= fcntl (1, F_GETFL))
314
360
_IO_flockfile (stderr);
328
374
__fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
329
file_name, line_number);
375
file_name, line_number);
331
377
fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
332
file_name, line_number);
378
file_name, line_number);
335
381
va_start (args, message);