PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/libiconv/srclib/error.c

http://github.com/bnoordhuis/node-iconv
C | 398 lines | 287 code | 56 blank | 55 comment | 49 complexity | 4351e0634ea594f59555dcaf5f8c79a3 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0, 0BSD
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000-2007, 2009-2011 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 Win32 API functions. */
  72. # define WIN32_LEAN_AND_MEAN
  73. # include <windows.h>
  74. # endif
  75. /* The gnulib override of fcntl is not needed in this file. */
  76. # undef fcntl
  77. # if !HAVE_DECL_STRERROR_R
  78. # ifndef HAVE_DECL_STRERROR_R
  79. "this configure-time declaration test was not run"
  80. # endif
  81. # if STRERROR_R_CHAR_P
  82. char *strerror_r ();
  83. # else
  84. int strerror_r ();
  85. # endif
  86. # endif
  87. /* The calling program should define program_name and set it to the
  88. name of the executing program. */
  89. extern char *program_name;
  90. # if HAVE_STRERROR_R || defined strerror_r
  91. # define __strerror_r strerror_r
  92. # endif /* HAVE_STRERROR_R || defined strerror_r */
  93. #endif /* not _LIBC */
  94. #if !_LIBC
  95. /* Return non-zero if FD is open. */
  96. static inline int
  97. is_open (int fd)
  98. {
  99. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  100. /* On Win32: The initial state of unassigned standard file descriptors is
  101. that they are open but point to an INVALID_HANDLE_VALUE. There is no
  102. fcntl, and the gnulib replacement fcntl does not support F_GETFL. */
  103. return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
  104. # else
  105. # ifndef F_GETFL
  106. # error Please port fcntl to your platform
  107. # endif
  108. return 0 <= fcntl (fd, F_GETFL);
  109. # endif
  110. }
  111. #endif
  112. static inline void
  113. flush_stdout (void)
  114. {
  115. #if !_LIBC
  116. int stdout_fd;
  117. # if GNULIB_FREOPEN_SAFER
  118. /* Use of gnulib's freopen-safer module normally ensures that
  119. fileno (stdout) == 1
  120. whenever stdout is open. */
  121. stdout_fd = STDOUT_FILENO;
  122. # else
  123. /* POSIX states that fileno (stdout) after fclose is unspecified. But in
  124. practice it is not a problem, because stdout is statically allocated and
  125. the fd of a FILE stream is stored as a field in its allocated memory. */
  126. stdout_fd = fileno (stdout);
  127. # endif
  128. /* POSIX states that fflush (stdout) after fclose is unspecified; it
  129. is safe in glibc, but not on all other platforms. fflush (NULL)
  130. is always defined, but too draconian. */
  131. if (0 <= stdout_fd && is_open (stdout_fd))
  132. #endif
  133. fflush (stdout);
  134. }
  135. static void
  136. print_errno_message (int errnum)
  137. {
  138. char const *s;
  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. #if !_LIBC
  153. if (! s)
  154. s = _("Unknown system error");
  155. #endif
  156. #if _LIBC
  157. __fxprintf (NULL, ": %s", s);
  158. #else
  159. fprintf (stderr, ": %s", s);
  160. #endif
  161. }
  162. static void
  163. error_tail (int status, int errnum, const char *message, va_list args)
  164. {
  165. #if _LIBC
  166. if (_IO_fwide (stderr, 0) > 0)
  167. {
  168. # define ALLOCA_LIMIT 2000
  169. size_t len = strlen (message) + 1;
  170. wchar_t *wmessage = NULL;
  171. mbstate_t st;
  172. size_t res;
  173. const char *tmp;
  174. bool use_malloc = false;
  175. while (1)
  176. {
  177. if (__libc_use_alloca (len * sizeof (wchar_t)))
  178. wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
  179. else
  180. {
  181. if (!use_malloc)
  182. wmessage = NULL;
  183. wchar_t *p = (wchar_t *) realloc (wmessage,
  184. len * sizeof (wchar_t));
  185. if (p == NULL)
  186. {
  187. free (wmessage);
  188. fputws_unlocked (L"out of memory\n", stderr);
  189. return;
  190. }
  191. wmessage = p;
  192. use_malloc = true;
  193. }
  194. memset (&st, '\0', sizeof (st));
  195. tmp = message;
  196. res = mbsrtowcs (wmessage, &tmp, len, &st);
  197. if (res != len)
  198. break;
  199. if (__builtin_expect (len >= SIZE_MAX / 2, 0))
  200. {
  201. /* This really should not happen if everything is fine. */
  202. res = (size_t) -1;
  203. break;
  204. }
  205. len *= 2;
  206. }
  207. if (res == (size_t) -1)
  208. {
  209. /* The string cannot be converted. */
  210. if (use_malloc)
  211. {
  212. free (wmessage);
  213. use_malloc = false;
  214. }
  215. wmessage = (wchar_t *) L"???";
  216. }
  217. __vfwprintf (stderr, wmessage, args);
  218. if (use_malloc)
  219. free (wmessage);
  220. }
  221. else
  222. #endif
  223. vfprintf (stderr, message, args);
  224. va_end (args);
  225. ++error_message_count;
  226. if (errnum)
  227. print_errno_message (errnum);
  228. #if _LIBC
  229. __fxprintf (NULL, "\n");
  230. #else
  231. putc ('\n', stderr);
  232. #endif
  233. fflush (stderr);
  234. if (status)
  235. exit (status);
  236. }
  237. /* Print the program name and error message MESSAGE, which is a printf-style
  238. format string with optional args.
  239. If ERRNUM is nonzero, print its corresponding system error message.
  240. Exit with status STATUS if it is nonzero. */
  241. void
  242. error (int status, int errnum, const char *message, ...)
  243. {
  244. va_list args;
  245. #if defined _LIBC && defined __libc_ptf_call
  246. /* We do not want this call to be cut short by a thread
  247. cancellation. Therefore disable cancellation for now. */
  248. int state = PTHREAD_CANCEL_ENABLE;
  249. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  250. 0);
  251. #endif
  252. flush_stdout ();
  253. #ifdef _LIBC
  254. _IO_flockfile (stderr);
  255. #endif
  256. if (error_print_progname)
  257. (*error_print_progname) ();
  258. else
  259. {
  260. #if _LIBC
  261. __fxprintf (NULL, "%s: ", program_name);
  262. #else
  263. fprintf (stderr, "%s: ", program_name);
  264. #endif
  265. }
  266. va_start (args, message);
  267. error_tail (status, errnum, message, args);
  268. #ifdef _LIBC
  269. _IO_funlockfile (stderr);
  270. # ifdef __libc_ptf_call
  271. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  272. # endif
  273. #endif
  274. }
  275. /* Sometimes we want to have at most one error per line. This
  276. variable controls whether this mode is selected or not. */
  277. int error_one_per_line;
  278. void
  279. error_at_line (int status, int errnum, const char *file_name,
  280. unsigned int line_number, const char *message, ...)
  281. {
  282. va_list args;
  283. if (error_one_per_line)
  284. {
  285. static const char *old_file_name;
  286. static unsigned int old_line_number;
  287. if (old_line_number == line_number
  288. && (file_name == old_file_name
  289. || strcmp (old_file_name, file_name) == 0))
  290. /* Simply return and print nothing. */
  291. return;
  292. old_file_name = file_name;
  293. old_line_number = line_number;
  294. }
  295. #if defined _LIBC && defined __libc_ptf_call
  296. /* We do not want this call to be cut short by a thread
  297. cancellation. Therefore disable cancellation for now. */
  298. int state = PTHREAD_CANCEL_ENABLE;
  299. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  300. 0);
  301. #endif
  302. flush_stdout ();
  303. #ifdef _LIBC
  304. _IO_flockfile (stderr);
  305. #endif
  306. if (error_print_progname)
  307. (*error_print_progname) ();
  308. else
  309. {
  310. #if _LIBC
  311. __fxprintf (NULL, "%s:", program_name);
  312. #else
  313. fprintf (stderr, "%s:", program_name);
  314. #endif
  315. }
  316. #if _LIBC
  317. __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
  318. file_name, line_number);
  319. #else
  320. fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
  321. file_name, line_number);
  322. #endif
  323. va_start (args, message);
  324. error_tail (status, errnum, message, args);
  325. #ifdef _LIBC
  326. _IO_funlockfile (stderr);
  327. # ifdef __libc_ptf_call
  328. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  329. # endif
  330. #endif
  331. }
  332. #ifdef _LIBC
  333. /* Make the weak alias. */
  334. # undef error
  335. # undef error_at_line
  336. weak_alias (__error, error)
  337. weak_alias (__error_at_line, error_at_line)
  338. #endif