PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/lib/libparted/common/lib/error.c

https://bitbucket.org/illumos/illumos-gate/
C | 339 lines | 248 code | 50 blank | 41 comment | 39 complexity | 78adb5f0ab144877ffebe89f29ef8898 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, AGPL-1.0, AGPL-3.0, BSD-3-Clause, GPL-3.0, LGPL-2.1, BSD-2-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, 0BSD
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  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. #endif
  35. #if USE_UNLOCKED_IO
  36. # include "unlocked-io.h"
  37. #endif
  38. #ifndef _
  39. # define _(String) String
  40. #endif
  41. /* If NULL, error will flush stdout, then print on stderr the program
  42. name, a colon and a space. Otherwise, error will call this
  43. function without parameters instead. */
  44. void (*error_print_progname) (void);
  45. /* This variable is incremented each time `error' is called. */
  46. unsigned int error_message_count;
  47. #ifdef _LIBC
  48. /* In the GNU C library, there is a predefined variable for this. */
  49. # define program_name program_invocation_name
  50. # include <errno.h>
  51. # include <limits.h>
  52. # include <libio/libioP.h>
  53. /* In GNU libc we want do not want to use the common name `error' directly.
  54. Instead make it a weak alias. */
  55. extern void __error (int status, int errnum, const char *message, ...)
  56. __attribute__ ((__format__ (__printf__, 3, 4)));
  57. extern void __error_at_line (int status, int errnum, const char *file_name,
  58. unsigned int line_number, const char *message,
  59. ...)
  60. __attribute__ ((__format__ (__printf__, 5, 6)));;
  61. # define error __error
  62. # define error_at_line __error_at_line
  63. # include <libio/iolibio.h>
  64. # define fflush(s) INTUSE(_IO_fflush) (s)
  65. # undef putc
  66. # define putc(c, fp) INTUSE(_IO_putc) (c, fp)
  67. # include <bits/libc-lock.h>
  68. #else /* not _LIBC */
  69. # if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
  70. # ifndef HAVE_DECL_STRERROR_R
  71. "this configure-time declaration test was not run"
  72. # endif
  73. char *strerror_r ();
  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 /* HAVE_STRERROR_R || defined strerror_r */
  81. #endif /* not _LIBC */
  82. static void
  83. print_errno_message (int errnum)
  84. {
  85. char const *s;
  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. else
  94. s = 0;
  95. # endif
  96. #else
  97. s = strerror (errnum);
  98. #endif
  99. #if !_LIBC
  100. if (! s)
  101. s = _("Unknown system error");
  102. #endif
  103. #if _LIBC
  104. __fxprintf (NULL, ": %s", s);
  105. #else
  106. fprintf (stderr, ": %s", s);
  107. #endif
  108. }
  109. static void
  110. error_tail (int status, int errnum, const char *message, va_list args)
  111. {
  112. #if _LIBC
  113. if (_IO_fwide (stderr, 0) > 0)
  114. {
  115. # define ALLOCA_LIMIT 2000
  116. size_t len = strlen (message) + 1;
  117. wchar_t *wmessage = NULL;
  118. mbstate_t st;
  119. size_t res;
  120. const char *tmp;
  121. bool use_malloc = false;
  122. while (1)
  123. {
  124. if (__libc_use_alloca (len * sizeof (wchar_t)))
  125. wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
  126. else
  127. {
  128. if (!use_malloc)
  129. wmessage = NULL;
  130. wchar_t *p = (wchar_t *) realloc (wmessage,
  131. len * sizeof (wchar_t));
  132. if (p == NULL)
  133. {
  134. free (wmessage);
  135. fputws_unlocked (L"out of memory\n", stderr);
  136. return;
  137. }
  138. wmessage = p;
  139. use_malloc = true;
  140. }
  141. memset (&st, '\0', sizeof (st));
  142. tmp = message;
  143. res = mbsrtowcs (wmessage, &tmp, len, &st);
  144. if (res != len)
  145. break;
  146. if (__builtin_expect (len >= SIZE_MAX / 2, 0))
  147. {
  148. /* This really should not happen if everything is fine. */
  149. res = (size_t) -1;
  150. break;
  151. }
  152. len *= 2;
  153. }
  154. if (res == (size_t) -1)
  155. {
  156. /* The string cannot be converted. */
  157. if (use_malloc)
  158. {
  159. free (wmessage);
  160. use_malloc = false;
  161. }
  162. wmessage = (wchar_t *) L"???";
  163. }
  164. __vfwprintf (stderr, wmessage, args);
  165. if (use_malloc)
  166. free (wmessage);
  167. }
  168. else
  169. #endif
  170. vfprintf (stderr, message, args);
  171. va_end (args);
  172. ++error_message_count;
  173. if (errnum)
  174. print_errno_message (errnum);
  175. #if _LIBC
  176. __fxprintf (NULL, "\n");
  177. #else
  178. putc ('\n', stderr);
  179. #endif
  180. fflush (stderr);
  181. if (status)
  182. exit (status);
  183. }
  184. /* Print the program name and error message MESSAGE, which is a printf-style
  185. format string with optional args.
  186. If ERRNUM is nonzero, print its corresponding system error message.
  187. Exit with status STATUS if it is nonzero. */
  188. void
  189. error (int status, int errnum, const char *message, ...)
  190. {
  191. va_list args;
  192. #if defined _LIBC && defined __libc_ptf_call
  193. /* We do not want this call to be cut short by a thread
  194. cancellation. Therefore disable cancellation for now. */
  195. int state = PTHREAD_CANCEL_ENABLE;
  196. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  197. 0);
  198. #endif
  199. fflush (stdout);
  200. #ifdef _LIBC
  201. _IO_flockfile (stderr);
  202. #endif
  203. if (error_print_progname)
  204. (*error_print_progname) ();
  205. else
  206. {
  207. #if _LIBC
  208. __fxprintf (NULL, "%s: ", program_name);
  209. #else
  210. fprintf (stderr, "%s: ", program_name);
  211. #endif
  212. }
  213. va_start (args, message);
  214. error_tail (status, errnum, message, args);
  215. #ifdef _LIBC
  216. _IO_funlockfile (stderr);
  217. # ifdef __libc_ptf_call
  218. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  219. # endif
  220. #endif
  221. }
  222. /* Sometimes we want to have at most one error per line. This
  223. variable controls whether this mode is selected or not. */
  224. int error_one_per_line;
  225. void
  226. error_at_line (int status, int errnum, const char *file_name,
  227. unsigned int line_number, const char *message, ...)
  228. {
  229. va_list args;
  230. if (error_one_per_line)
  231. {
  232. static const char *old_file_name;
  233. static unsigned int old_line_number;
  234. if (old_line_number == line_number
  235. && (file_name == old_file_name
  236. || strcmp (old_file_name, file_name) == 0))
  237. /* Simply return and print nothing. */
  238. return;
  239. old_file_name = file_name;
  240. old_line_number = line_number;
  241. }
  242. #if defined _LIBC && defined __libc_ptf_call
  243. /* We do not want this call to be cut short by a thread
  244. cancellation. Therefore disable cancellation for now. */
  245. int state = PTHREAD_CANCEL_ENABLE;
  246. __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
  247. 0);
  248. #endif
  249. fflush (stdout);
  250. #ifdef _LIBC
  251. _IO_flockfile (stderr);
  252. #endif
  253. if (error_print_progname)
  254. (*error_print_progname) ();
  255. else
  256. {
  257. #if _LIBC
  258. __fxprintf (NULL, "%s:", program_name);
  259. #else
  260. fprintf (stderr, "%s:", program_name);
  261. #endif
  262. }
  263. #if _LIBC
  264. __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
  265. file_name, line_number);
  266. #else
  267. fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
  268. file_name, line_number);
  269. #endif
  270. va_start (args, message);
  271. error_tail (status, errnum, message, args);
  272. #ifdef _LIBC
  273. _IO_funlockfile (stderr);
  274. # ifdef __libc_ptf_call
  275. __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0);
  276. # endif
  277. #endif
  278. }
  279. #ifdef _LIBC
  280. /* Make the weak alias. */
  281. # undef error
  282. # undef error_at_line
  283. weak_alias (__error, error)
  284. weak_alias (__error_at_line, error_at_line)
  285. #endif