PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/screensavers/rsxs-0.9/lib/error.c

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