error.c 9.2 KB

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