error.c 10.0 KB

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