PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/gnu-sort/lib/error.c

https://bitbucket.org/freebsd/freebsd-head/
C | 306 lines | 222 code | 45 blank | 39 comment | 35 complexity | 7d46cb2a3b588e16da73f026d9ac8c9e MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000-2002, 2003, 2004 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 2, or (at your option)
  7. 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 along
  13. with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  16. #ifdef HAVE_CONFIG_H
  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. #ifdef _LIBC
  25. # include <libintl.h>
  26. #else
  27. # include "gettext.h"
  28. #endif
  29. #ifdef _LIBC
  30. # include <wchar.h>
  31. # define mbsrtowcs __mbsrtowcs
  32. #endif
  33. #if !_LIBC
  34. # include "unlocked-io.h"
  35. #endif
  36. #ifndef _
  37. # define _(String) String
  38. #endif
  39. /* If NULL, error will flush stdout, then print on stderr the program
  40. name, a colon and a space. Otherwise, error will call this
  41. function without parameters instead. */
  42. void (*error_print_progname) (void);
  43. /* This variable is incremented each time `error' is called. */
  44. unsigned int error_message_count;
  45. #ifdef _LIBC
  46. /* In the GNU C library, there is a predefined variable for this. */
  47. # define program_name program_invocation_name
  48. # include <errno.h>
  49. # include <libio/libioP.h>
  50. /* In GNU libc we want do not want to use the common name `error' directly.
  51. Instead make it a weak alias. */
  52. extern void __error (int status, int errnum, const char *message, ...)
  53. __attribute__ ((__format__ (__printf__, 3, 4)));
  54. extern void __error_at_line (int status, int errnum, const char *file_name,
  55. unsigned int line_number, const char *message,
  56. ...)
  57. __attribute__ ((__format__ (__printf__, 5, 6)));;
  58. # define error __error
  59. # define error_at_line __error_at_line
  60. # include <libio/iolibio.h>
  61. # define fflush(s) INTUSE(_IO_fflush) (s)
  62. # undef putc
  63. # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
  64. # include <bits/libc-lock.h>
  65. #else /* not _LIBC */
  66. # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
  67. # ifndef HAVE_DECL_STRERROR_R
  68. "this configure-time declaration test was not run"
  69. # endif
  70. char *strerror_r ();
  71. # endif
  72. # ifndef SIZE_MAX
  73. # define SIZE_MAX ((size_t) -1)
  74. # endif
  75. /* The calling program should define program_name and set it to the
  76. name of the executing program. */
  77. extern char *program_name;
  78. # if HAVE_STRERROR_R || defined strerror_r
  79. # define __strerror_r strerror_r
  80. # endif
  81. #endif /* not _LIBC */
  82. static void
  83. print_errno_message (int errnum)
  84. {
  85. char const *s = NULL;
  86. #if defined HAVE_STRERROR_R || _LIBC
  87. char errbuf[1024];
  88. # if STRERROR_R_CHAR_P || _LIBC
  89. s = __strerror_r (errnum, errbuf, sizeof errbuf);
  90. # else
  91. if (__strerror_r (errnum, errbuf, sizeof errbuf) == 0)
  92. s = errbuf;
  93. # endif
  94. #endif
  95. #if !_LIBC
  96. if (! s && ! (s = strerror (errnum)))
  97. s = _("Unknown system error");
  98. #endif
  99. #if _LIBC
  100. if (_IO_fwide (stderr, 0) > 0)
  101. {
  102. __fwprintf (stderr, L": %s", s);
  103. return;
  104. }
  105. #endif
  106. fprintf (stderr, ": %s", s);
  107. }
  108. static void
  109. error_tail (int status, int errnum, const char *message, va_list args)
  110. {
  111. #if _LIBC
  112. if (_IO_fwide (stderr, 0) > 0)
  113. {
  114. # define ALLOCA_LIMIT 2000
  115. size_t len = strlen (message) + 1;
  116. const wchar_t *wmessage = L"out of memory";
  117. wchar_t *wbuf = (len < ALLOCA_LIMIT
  118. ? alloca (len * sizeof *wbuf)
  119. : len <= SIZE_MAX / sizeof *wbuf
  120. ? malloc (len * sizeof *wbuf)
  121. : NULL);
  122. if (wbuf)
  123. {
  124. size_t res;
  125. mbstate_t st;
  126. const char *tmp = message;
  127. memset (&st, '\0', sizeof (st));
  128. res = mbsrtowcs (wbuf, &tmp, len, &st);
  129. wmessage = res == (size_t) -1 ? L"???" : wbuf;
  130. }
  131. __vfwprintf (stderr, wmessage, args);
  132. if (! (len < ALLOCA_LIMIT))
  133. free (wbuf);
  134. }
  135. else
  136. #endif
  137. vfprintf (stderr, message, args);
  138. va_end (args);
  139. ++error_message_count;
  140. if (errnum)
  141. print_errno_message (errnum);
  142. #if _LIBC
  143. if (_IO_fwide (stderr, 0) > 0)
  144. putwc (L'\n', stderr);
  145. else
  146. #endif
  147. putc ('\n', stderr);
  148. fflush (stderr);
  149. if (status)
  150. exit (status);
  151. }
  152. /* Print the program name and error message MESSAGE, which is a printf-style
  153. format string with optional args.
  154. If ERRNUM is nonzero, print its corresponding system error message.
  155. Exit with status STATUS if it is nonzero. */
  156. void
  157. error (int status, int errnum, const char *message, ...)
  158. {
  159. va_list args;
  160. #if defined _LIBC && defined __libc_ptf_call
  161. /* We do not want this call to be cut short by a thread
  162. cancellation. Therefore disable cancellation for now. */
  163. int state = PTHREAD_CANCEL_ENABLE;
  164. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  165. 0);
  166. #endif
  167. fflush (stdout);
  168. #ifdef _LIBC
  169. _IO_flockfile (stderr);
  170. #endif
  171. if (error_print_progname)
  172. (*error_print_progname) ();
  173. else
  174. {
  175. #if _LIBC
  176. if (_IO_fwide (stderr, 0) > 0)
  177. __fwprintf (stderr, L"%s: ", program_name);
  178. else
  179. #endif
  180. fprintf (stderr, "%s: ", program_name);
  181. }
  182. va_start (args, message);
  183. error_tail (status, errnum, message, args);
  184. #ifdef _LIBC
  185. _IO_funlockfile (stderr);
  186. # ifdef __libc_ptf_call
  187. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  188. # endif
  189. #endif
  190. }
  191. /* Sometimes we want to have at most one error per line. This
  192. variable controls whether this mode is selected or not. */
  193. int error_one_per_line;
  194. void
  195. error_at_line (int status, int errnum, const char *file_name,
  196. unsigned int line_number, const char *message, ...)
  197. {
  198. va_list args;
  199. if (error_one_per_line)
  200. {
  201. static const char *old_file_name;
  202. static unsigned int old_line_number;
  203. if (old_line_number == line_number
  204. && (file_name == old_file_name
  205. || strcmp (old_file_name, file_name) == 0))
  206. /* Simply return and print nothing. */
  207. return;
  208. old_file_name = file_name;
  209. old_line_number = line_number;
  210. }
  211. #if defined _LIBC && defined __libc_ptf_call
  212. /* We do not want this call to be cut short by a thread
  213. cancellation. Therefore disable cancellation for now. */
  214. int state = PTHREAD_CANCEL_ENABLE;
  215. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  216. 0);
  217. #endif
  218. fflush (stdout);
  219. #ifdef _LIBC
  220. _IO_flockfile (stderr);
  221. #endif
  222. if (error_print_progname)
  223. (*error_print_progname) ();
  224. else
  225. {
  226. #if _LIBC
  227. if (_IO_fwide (stderr, 0) > 0)
  228. __fwprintf (stderr, L"%s: ", program_name);
  229. else
  230. #endif
  231. fprintf (stderr, "%s:", program_name);
  232. }
  233. if (file_name != NULL)
  234. {
  235. #if _LIBC
  236. if (_IO_fwide (stderr, 0) > 0)
  237. __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
  238. else
  239. #endif
  240. fprintf (stderr, "%s:%d: ", file_name, line_number);
  241. }
  242. va_start (args, message);
  243. error_tail (status, errnum, message, args);
  244. #ifdef _LIBC
  245. _IO_funlockfile (stderr);
  246. # ifdef __libc_ptf_call
  247. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  248. # endif
  249. #endif
  250. }
  251. #ifdef _LIBC
  252. /* Make the weak alias. */
  253. # undef error
  254. # undef error_at_line
  255. weak_alias (__error, error)
  256. weak_alias (__error_at_line, error_at_line)
  257. #endif