PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/error.c

https://github.com/neerajky/wget-1.51
C | 401 lines | 288 code | 56 blank | 57 comment | 49 complexity | 199c4561167ac2bd9bbc6c3bed6e911e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000-2007, 2009-2013 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  15. #if !_LIBC
  16. # include <config.h>
  17. #endif
  18. #include "error.h"
  19. #include <stdarg.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #if !_LIBC && ENABLE_NLS
  24. # include "gettext.h"
  25. # define _(msgid) gettext (msgid)
  26. #endif
  27. #ifdef _LIBC
  28. # include <libintl.h>
  29. # include <stdbool.h>
  30. # include <stdint.h>
  31. # include <wchar.h>
  32. # define mbsrtowcs __mbsrtowcs
  33. #endif
  34. #if USE_UNLOCKED_IO
  35. # include "unlocked-io.h"
  36. #endif
  37. #ifndef _
  38. # define _(String) String
  39. #endif
  40. /* If NULL, error will flush stdout, then print on stderr the program
  41. name, a colon and a space. Otherwise, error will call this
  42. function without parameters instead. */
  43. void (*error_print_progname) (void);
  44. /* This variable is incremented each time 'error' is called. */
  45. unsigned int error_message_count;
  46. #ifdef _LIBC
  47. /* In the GNU C library, there is a predefined variable for this. */
  48. # define program_name program_invocation_name
  49. # include <errno.h>
  50. # include <limits.h>
  51. # include <libio/libioP.h>
  52. /* In GNU libc we want do not want to use the common name 'error' directly.
  53. Instead make it a weak alias. */
  54. extern void __error (int status, int errnum, const char *message, ...)
  55. __attribute__ ((__format__ (__printf__, 3, 4)));
  56. extern void __error_at_line (int status, int errnum, const char *file_name,
  57. unsigned int line_number, const char *message,
  58. ...)
  59. __attribute__ ((__format__ (__printf__, 5, 6)));;
  60. # define error __error
  61. # define error_at_line __error_at_line
  62. # include <libio/iolibio.h>
  63. # define fflush(s) INTUSE(_IO_fflush) (s)
  64. # undef putc
  65. # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
  66. # include <bits/libc-lock.h>
  67. #else /* not _LIBC */
  68. # include <fcntl.h>
  69. # include <unistd.h>
  70. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  71. /* Get declarations of the native Windows API functions. */
  72. # define WIN32_LEAN_AND_MEAN
  73. # include <windows.h>
  74. /* Get _get_osfhandle. */
  75. # include "msvc-nothrow.h"
  76. # endif
  77. /* The gnulib override of fcntl is not needed in this file. */
  78. # undef fcntl
  79. # if !HAVE_DECL_STRERROR_R
  80. # ifndef HAVE_DECL_STRERROR_R
  81. "this configure-time declaration test was not run"
  82. # endif
  83. # if STRERROR_R_CHAR_P
  84. char *strerror_r ();
  85. # else
  86. int strerror_r ();
  87. # endif
  88. # endif
  89. /* The calling program should define program_name and set it to the
  90. name of the executing program. */
  91. extern char *program_name;
  92. # if HAVE_STRERROR_R || defined strerror_r
  93. # define __strerror_r strerror_r
  94. # endif /* HAVE_STRERROR_R || defined strerror_r */
  95. #endif /* not _LIBC */
  96. #if !_LIBC
  97. /* Return non-zero if FD is open. */
  98. static int
  99. is_open (int fd)
  100. {
  101. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  102. /* On native Windows: The initial state of unassigned standard file
  103. descriptors is that they are open but point to an INVALID_HANDLE_VALUE.
  104. There is no fcntl, and the gnulib replacement fcntl does not support
  105. F_GETFL. */
  106. return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
  107. # else
  108. # ifndef F_GETFL
  109. # error Please port fcntl to your platform
  110. # endif
  111. return 0 <= fcntl (fd, F_GETFL);
  112. # endif
  113. }
  114. #endif
  115. static void
  116. flush_stdout (void)
  117. {
  118. #if !_LIBC
  119. int stdout_fd;
  120. # if GNULIB_FREOPEN_SAFER
  121. /* Use of gnulib's freopen-safer module normally ensures that
  122. fileno (stdout) == 1
  123. whenever stdout is open. */
  124. stdout_fd = STDOUT_FILENO;
  125. # else
  126. /* POSIX states that fileno (stdout) after fclose is unspecified. But in
  127. practice it is not a problem, because stdout is statically allocated and
  128. the fd of a FILE stream is stored as a field in its allocated memory. */
  129. stdout_fd = fileno (stdout);
  130. # endif
  131. /* POSIX states that fflush (stdout) after fclose is unspecified; it
  132. is safe in glibc, but not on all other platforms. fflush (NULL)
  133. is always defined, but too draconian. */
  134. if (0 <= stdout_fd && is_open (stdout_fd))
  135. #endif
  136. fflush (stdout);
  137. }
  138. static void
  139. print_errno_message (int errnum)
  140. {
  141. char const *s;
  142. #if defined HAVE_STRERROR_R || _LIBC
  143. char errbuf[1024];
  144. # if STRERROR_R_CHAR_P || _LIBC
  145. s = __strerror_r (errnum, errbuf, sizeof errbuf);
  146. # else
  147. if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
  148. s = errbuf;
  149. else
  150. s = 0;
  151. # endif
  152. #else
  153. s = strerror (errnum);
  154. #endif
  155. #if !_LIBC
  156. if (! s)
  157. s = _("Unknown system error");
  158. #endif
  159. #if _LIBC
  160. __fxprintf (NULL, ": %s", s);
  161. #else
  162. fprintf (stderr, ": %s", s);
  163. #endif
  164. }
  165. static void _GL_ATTRIBUTE_FORMAT_PRINTF (3, 0) _GL_ARG_NONNULL ((3))
  166. error_tail (int status, int errnum, const char *message, va_list args)
  167. {
  168. #if _LIBC
  169. if (_IO_fwide (stderr, 0) > 0)
  170. {
  171. # define ALLOCA_LIMIT 2000
  172. size_t len = strlen (message) + 1;
  173. wchar_t *wmessage = NULL;
  174. mbstate_t st;
  175. size_t res;
  176. const char *tmp;
  177. bool use_malloc = false;
  178. while (1)
  179. {
  180. if (__libc_use_alloca (len * sizeof (wchar_t)))
  181. wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
  182. else
  183. {
  184. if (!use_malloc)
  185. wmessage = NULL;
  186. wchar_t *p = (wchar_t *) realloc (wmessage,
  187. len * sizeof (wchar_t));
  188. if (p == NULL)
  189. {
  190. free (wmessage);
  191. fputws_unlocked (L"out of memory\n", stderr);
  192. return;
  193. }
  194. wmessage = p;
  195. use_malloc = true;
  196. }
  197. memset (&st, '\0', sizeof (st));
  198. tmp = message;
  199. res = mbsrtowcs (wmessage, &tmp, len, &st);
  200. if (res != len)
  201. break;
  202. if (__builtin_expect (len >= SIZE_MAX / 2, 0))
  203. {
  204. /* This really should not happen if everything is fine. */
  205. res = (size_t) -1;
  206. break;
  207. }
  208. len *= 2;
  209. }
  210. if (res == (size_t) -1)
  211. {
  212. /* The string cannot be converted. */
  213. if (use_malloc)
  214. {
  215. free (wmessage);
  216. use_malloc = false;
  217. }
  218. wmessage = (wchar_t *) L"???";
  219. }
  220. __vfwprintf (stderr, wmessage, args);
  221. if (use_malloc)
  222. free (wmessage);
  223. }
  224. else
  225. #endif
  226. vfprintf (stderr, message, args);
  227. va_end (args);
  228. ++error_message_count;
  229. if (errnum)
  230. print_errno_message (errnum);
  231. #if _LIBC
  232. __fxprintf (NULL, "\n");
  233. #else
  234. putc ('\n', stderr);
  235. #endif
  236. fflush (stderr);
  237. if (status)
  238. exit (status);
  239. }
  240. /* Print the program name and error message MESSAGE, which is a printf-style
  241. format string with optional args.
  242. If ERRNUM is nonzero, print its corresponding system error message.
  243. Exit with status STATUS if it is nonzero. */
  244. void
  245. error (int status, int errnum, const char *message, ...)
  246. {
  247. va_list args;
  248. #if defined _LIBC && defined __libc_ptf_call
  249. /* We do not want this call to be cut short by a thread
  250. cancellation. Therefore disable cancellation for now. */
  251. int state = PTHREAD_CANCEL_ENABLE;
  252. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  253. 0);
  254. #endif
  255. flush_stdout ();
  256. #ifdef _LIBC
  257. _IO_flockfile (stderr);
  258. #endif
  259. if (error_print_progname)
  260. (*error_print_progname) ();
  261. else
  262. {
  263. #if _LIBC
  264. __fxprintf (NULL, "%s: ", program_name);
  265. #else
  266. fprintf (stderr, "%s: ", program_name);
  267. #endif
  268. }
  269. va_start (args, message);
  270. error_tail (status, errnum, message, args);
  271. #ifdef _LIBC
  272. _IO_funlockfile (stderr);
  273. # ifdef __libc_ptf_call
  274. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  275. # endif
  276. #endif
  277. }
  278. /* Sometimes we want to have at most one error per line. This
  279. variable controls whether this mode is selected or not. */
  280. int error_one_per_line;
  281. void
  282. error_at_line (int status, int errnum, const char *file_name,
  283. unsigned int line_number, const char *message, ...)
  284. {
  285. va_list args;
  286. if (error_one_per_line)
  287. {
  288. static const char *old_file_name;
  289. static unsigned int old_line_number;
  290. if (old_line_number == line_number
  291. && (file_name == old_file_name
  292. || strcmp (old_file_name, file_name) == 0))
  293. /* Simply return and print nothing. */
  294. return;
  295. old_file_name = file_name;
  296. old_line_number = line_number;
  297. }
  298. #if defined _LIBC && defined __libc_ptf_call
  299. /* We do not want this call to be cut short by a thread
  300. cancellation. Therefore disable cancellation for now. */
  301. int state = PTHREAD_CANCEL_ENABLE;
  302. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  303. 0);
  304. #endif
  305. flush_stdout ();
  306. #ifdef _LIBC
  307. _IO_flockfile (stderr);
  308. #endif
  309. if (error_print_progname)
  310. (*error_print_progname) ();
  311. else
  312. {
  313. #if _LIBC
  314. __fxprintf (NULL, "%s:", program_name);
  315. #else
  316. fprintf (stderr, "%s:", program_name);
  317. #endif
  318. }
  319. #if _LIBC
  320. __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
  321. file_name, line_number);
  322. #else
  323. fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
  324. file_name, line_number);
  325. #endif
  326. va_start (args, message);
  327. error_tail (status, errnum, message, args);
  328. #ifdef _LIBC
  329. _IO_funlockfile (stderr);
  330. # ifdef __libc_ptf_call
  331. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  332. # endif
  333. #endif
  334. }
  335. #ifdef _LIBC
  336. /* Make the weak alias. */
  337. # undef error
  338. # undef error_at_line
  339. weak_alias (__error, error)
  340. weak_alias (__error_at_line, error_at_line)
  341. #endif