error.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 !defined(_WIN32) && !defined(_WIN64)
  135. if (0 <= stdout_fd && is_open (stdout_fd))
  136. #endif
  137. #endif
  138. fflush (stdout);
  139. }
  140. static void
  141. print_errno_message (int errnum)
  142. {
  143. char const *s;
  144. #if defined HAVE_STRERROR_R || _LIBC
  145. char errbuf[1024];
  146. # if STRERROR_R_CHAR_P || _LIBC
  147. s = __strerror_r (errnum, errbuf, sizeof errbuf);
  148. # else
  149. if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
  150. s = errbuf;
  151. else
  152. s = 0;
  153. # endif
  154. #else
  155. s = strerror (errnum);
  156. #endif
  157. #if !_LIBC
  158. if (! s)
  159. s = _("Unknown system error");
  160. #endif
  161. #if _LIBC
  162. __fxprintf (NULL, ": %s", s);
  163. #else
  164. fprintf (stderr, ": %s", s);
  165. #endif
  166. }
  167. static void
  168. error_tail (int status, int errnum, const char *message, va_list args)
  169. {
  170. #if _LIBC
  171. if (_IO_fwide (stderr, 0) > 0)
  172. {
  173. # define ALLOCA_LIMIT 2000
  174. size_t len = strlen (message) + 1;
  175. wchar_t *wmessage = NULL;
  176. mbstate_t st;
  177. size_t res;
  178. const char *tmp;
  179. bool use_malloc = false;
  180. while (1)
  181. {
  182. if (__libc_use_alloca (len * sizeof (wchar_t)))
  183. wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
  184. else
  185. {
  186. if (!use_malloc)
  187. wmessage = NULL;
  188. wchar_t *p = (wchar_t *) realloc (wmessage,
  189. len * sizeof (wchar_t));
  190. if (p == NULL)
  191. {
  192. free (wmessage);
  193. fputws_unlocked (L"out of memory\n", stderr);
  194. return;
  195. }
  196. wmessage = p;
  197. use_malloc = true;
  198. }
  199. memset (&st, '\0', sizeof (st));
  200. tmp = message;
  201. res = mbsrtowcs (wmessage, &tmp, len, &st);
  202. if (res != len)
  203. break;
  204. if (__builtin_expect (len >= SIZE_MAX / 2, 0))
  205. {
  206. /* This really should not happen if everything is fine. */
  207. res = (size_t) -1;
  208. break;
  209. }
  210. len *= 2;
  211. }
  212. if (res == (size_t) -1)
  213. {
  214. /* The string cannot be converted. */
  215. if (use_malloc)
  216. {
  217. free (wmessage);
  218. use_malloc = false;
  219. }
  220. wmessage = (wchar_t *) L"???";
  221. }
  222. __vfwprintf (stderr, wmessage, args);
  223. if (use_malloc)
  224. free (wmessage);
  225. }
  226. else
  227. #endif
  228. vfprintf (stderr, message, args);
  229. va_end (args);
  230. ++error_message_count;
  231. if (errnum)
  232. print_errno_message (errnum);
  233. #if _LIBC
  234. __fxprintf (NULL, "\n");
  235. #else
  236. putc ('\n', stderr);
  237. #endif
  238. fflush (stderr);
  239. if (status)
  240. exit (status);
  241. }
  242. /* Print the program name and error message MESSAGE, which is a printf-style
  243. format string with optional args.
  244. If ERRNUM is nonzero, print its corresponding system error message.
  245. Exit with status STATUS if it is nonzero. */
  246. void
  247. error (int status, int errnum, const char *message, ...)
  248. {
  249. va_list args;
  250. #if defined _LIBC && defined __libc_ptf_call
  251. /* We do not want this call to be cut short by a thread
  252. cancellation. Therefore disable cancellation for now. */
  253. int state = PTHREAD_CANCEL_ENABLE;
  254. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  255. 0);
  256. #endif
  257. flush_stdout ();
  258. #ifdef _LIBC
  259. _IO_flockfile (stderr);
  260. #endif
  261. if (error_print_progname)
  262. (*error_print_progname) ();
  263. else
  264. {
  265. #if _LIBC
  266. __fxprintf (NULL, "%s: ", program_name);
  267. #else
  268. fprintf (stderr, "%s: ", program_name);
  269. #endif
  270. }
  271. va_start (args, message);
  272. error_tail (status, errnum, message, args);
  273. #ifdef _LIBC
  274. _IO_funlockfile (stderr);
  275. # ifdef __libc_ptf_call
  276. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  277. # endif
  278. #endif
  279. }
  280. /* Sometimes we want to have at most one error per line. This
  281. variable controls whether this mode is selected or not. */
  282. int error_one_per_line;
  283. void
  284. error_at_line (int status, int errnum, const char *file_name,
  285. unsigned int line_number, const char *message, ...)
  286. {
  287. va_list args;
  288. if (error_one_per_line)
  289. {
  290. static const char *old_file_name;
  291. static unsigned int old_line_number;
  292. if (old_line_number == line_number
  293. && (file_name == old_file_name
  294. || strcmp (old_file_name, file_name) == 0))
  295. /* Simply return and print nothing. */
  296. return;
  297. old_file_name = file_name;
  298. old_line_number = line_number;
  299. }
  300. #if defined _LIBC && defined __libc_ptf_call
  301. /* We do not want this call to be cut short by a thread
  302. cancellation. Therefore disable cancellation for now. */
  303. int state = PTHREAD_CANCEL_ENABLE;
  304. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  305. 0);
  306. #endif
  307. flush_stdout ();
  308. #ifdef _LIBC
  309. _IO_flockfile (stderr);
  310. #endif
  311. if (error_print_progname)
  312. (*error_print_progname) ();
  313. else
  314. {
  315. #if _LIBC
  316. __fxprintf (NULL, "%s:", program_name);
  317. #else
  318. fprintf (stderr, "%s:", program_name);
  319. #endif
  320. }
  321. #if _LIBC
  322. __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
  323. file_name, line_number);
  324. #else
  325. fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
  326. file_name, line_number);
  327. #endif
  328. va_start (args, message);
  329. error_tail (status, errnum, message, args);
  330. #ifdef _LIBC
  331. _IO_funlockfile (stderr);
  332. # ifdef __libc_ptf_call
  333. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  334. # endif
  335. #endif
  336. }
  337. #ifdef _LIBC
  338. /* Make the weak alias. */
  339. # undef error
  340. # undef error_at_line
  341. weak_alias (__error, error)
  342. weak_alias (__error_at_line, error_at_line)
  343. #endif