error.c 8.8 KB

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