error.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000, 2001 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library. Its master source is NOT part of
  4. the C library, however. The master source lives in /gd/gnu/lib.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. /* Written by David MacKenzie <[email protected]>. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <libintl.h>
  23. #ifdef _LIBC
  24. # include <wchar.h>
  25. # define mbsrtowcs __mbsrtowcs
  26. #endif
  27. #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
  28. # if __STDC__
  29. # include <stdarg.h>
  30. # define VA_START(args, lastarg) va_start(args, lastarg)
  31. # else
  32. # include <varargs.h>
  33. # define VA_START(args, lastarg) va_start(args)
  34. # endif
  35. #else
  36. # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
  37. # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  38. #endif
  39. #if STDC_HEADERS || _LIBC
  40. # include <stdlib.h>
  41. # include <string.h>
  42. #else
  43. void exit ();
  44. #endif
  45. #include "error.h"
  46. #ifndef _
  47. # define _(String) String
  48. #endif
  49. /* If NULL, error will flush stdout, then print on stderr the program
  50. name, a colon and a space. Otherwise, error will call this
  51. function without parameters instead. */
  52. void (*error_print_progname) (
  53. #if __STDC__ - 0
  54. void
  55. #endif
  56. );
  57. /* This variable is incremented each time `error' is called. */
  58. unsigned int error_message_count;
  59. #ifdef _LIBC
  60. /* In the GNU C library, there is a predefined variable for this. */
  61. # define program_name program_invocation_name
  62. # include <errno.h>
  63. /* In GNU libc we want do not want to use the common name `error' directly.
  64. Instead make it a weak alias. */
  65. extern void __error (int status, int errnum, const char *message, ...)
  66. __attribute__ ((__format__ (__printf__, 3, 4)));
  67. extern void __error_at_line (int status, int errnum, const char *file_name,
  68. unsigned int line_number, const char *message,
  69. ...)
  70. __attribute__ ((__format__ (__printf__, 5, 6)));;
  71. # define error __error
  72. # define error_at_line __error_at_line
  73. # ifdef USE_IN_LIBIO
  74. # include <libio/iolibio.h>
  75. # define fflush(s) _IO_fflush (s)
  76. # endif
  77. #else /* not _LIBC */
  78. /* The calling program should define program_name and set it to the
  79. name of the executing program. */
  80. extern char *program_name;
  81. # ifdef HAVE_STRERROR_R
  82. # define __strerror_r strerror_r
  83. # else
  84. # if HAVE_STRERROR
  85. # ifndef strerror /* On some systems, strerror is a macro */
  86. char *strerror ();
  87. # endif
  88. # else
  89. static char *
  90. private_strerror (errnum)
  91. int errnum;
  92. {
  93. extern char *sys_errlist[];
  94. extern int sys_nerr;
  95. if (errnum > 0 && errnum <= sys_nerr)
  96. return _(sys_errlist[errnum]);
  97. return _("Unknown system error");
  98. }
  99. # define strerror private_strerror
  100. # endif /* HAVE_STRERROR */
  101. # endif /* HAVE_STRERROR_R */
  102. #endif /* not _LIBC */
  103. #ifdef VA_START
  104. static void
  105. error_tail (int status, int errnum, const char *message, va_list args)
  106. {
  107. # if HAVE_VPRINTF || _LIBC
  108. # if _LIBC && USE_IN_LIBIO
  109. if (_IO_fwide (stderr, 0) > 0)
  110. {
  111. # define ALLOCA_LIMIT 2000
  112. size_t len = strlen (message) + 1;
  113. wchar_t *wmessage = NULL;
  114. mbstate_t st;
  115. size_t res;
  116. const char *tmp;
  117. do
  118. {
  119. if (len < ALLOCA_LIMIT)
  120. wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
  121. else
  122. {
  123. if (wmessage != NULL && len / 2 < ALLOCA_LIMIT)
  124. wmessage = NULL;
  125. wmessage = (wchar_t *) realloc (wmessage,
  126. len * sizeof (wchar_t));
  127. if (wmessage == NULL)
  128. {
  129. fputws_unlocked (L"out of memory\n", stderr);
  130. return;
  131. }
  132. }
  133. memset (&st, '\0', sizeof (st));
  134. tmp =message;
  135. }
  136. while ((res = mbsrtowcs (wmessage, &tmp, len, &st)) == len);
  137. if (res == (size_t) -1)
  138. /* The string cannot be converted. */
  139. wmessage = (wchar_t *) L"???";
  140. __vfwprintf (stderr, wmessage, args);
  141. }
  142. else
  143. # endif
  144. vfprintf (stderr, message, args);
  145. # else
  146. _doprnt (message, args, stderr);
  147. # endif
  148. va_end (args);
  149. ++error_message_count;
  150. if (errnum)
  151. {
  152. #if defined HAVE_STRERROR_R || _LIBC
  153. char errbuf[1024];
  154. char *s = __strerror_r (errnum, errbuf, sizeof errbuf);
  155. # if _LIBC && USE_IN_LIBIO
  156. if (_IO_fwide (stderr, 0) > 0)
  157. __fwprintf (stderr, L": %s", s);
  158. else
  159. # endif
  160. fprintf (stderr, ": %s", s);
  161. #else
  162. fprintf (stderr, ": %s", strerror (errnum));
  163. #endif
  164. }
  165. #if _LIBC && USE_IN_LIBIO
  166. if (_IO_fwide (stderr, 0) > 0)
  167. putwc (L'\n', stderr);
  168. else
  169. #endif
  170. putc ('\n', stderr);
  171. fflush (stderr);
  172. if (status)
  173. exit (status);
  174. }
  175. #endif
  176. /* Print the program name and error message MESSAGE, which is a printf-style
  177. format string with optional args.
  178. If ERRNUM is nonzero, print its corresponding system error message.
  179. Exit with status STATUS if it is nonzero. */
  180. /* VARARGS */
  181. void
  182. #if defined VA_START && __STDC__
  183. error (int status, int errnum, const char *message, ...)
  184. #else
  185. error (status, errnum, message, va_alist)
  186. int status;
  187. int errnum;
  188. char *message;
  189. va_dcl
  190. #endif
  191. {
  192. #ifdef VA_START
  193. va_list args;
  194. #endif
  195. fflush (stdout);
  196. #ifdef _LIBC
  197. # ifdef USE_IN_LIBIO
  198. _IO_flockfile (stderr);
  199. # else
  200. __flockfile (stderr);
  201. # endif
  202. #endif
  203. if (error_print_progname)
  204. (*error_print_progname) ();
  205. else
  206. {
  207. #if _LIBC && USE_IN_LIBIO
  208. if (_IO_fwide (stderr, 0) > 0)
  209. __fwprintf (stderr, L"%s: ", program_name);
  210. else
  211. #endif
  212. fprintf (stderr, "%s: ", program_name);
  213. }
  214. #ifdef VA_START
  215. VA_START (args, message);
  216. error_tail (status, errnum, message, args);
  217. #else
  218. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  219. ++error_message_count;
  220. if (errnum)
  221. fprintf (stderr, ": %s", strerror (errnum));
  222. putc ('\n', stderr);
  223. fflush (stderr);
  224. if (status)
  225. exit (status);
  226. #endif
  227. #ifdef _LIBC
  228. # ifdef USE_IN_LIBIO
  229. _IO_funlockfile (stderr);
  230. # else
  231. __funlockfile (stderr);
  232. # endif
  233. #endif
  234. }
  235. /* Sometimes we want to have at most one error per line. This
  236. variable controls whether this mode is selected or not. */
  237. int error_one_per_line;
  238. void
  239. #if defined VA_START && __STDC__
  240. error_at_line (int status, int errnum, const char *file_name,
  241. unsigned int line_number, const char *message, ...)
  242. #else
  243. error_at_line (status, errnum, file_name, line_number, message, va_alist)
  244. int status;
  245. int errnum;
  246. const char *file_name;
  247. unsigned int line_number;
  248. char *message;
  249. va_dcl
  250. #endif
  251. {
  252. #ifdef VA_START
  253. va_list args;
  254. #endif
  255. if (error_one_per_line)
  256. {
  257. static const char *old_file_name;
  258. static unsigned int old_line_number;
  259. if (old_line_number == line_number
  260. && (file_name == old_file_name
  261. || strcmp (old_file_name, file_name) == 0))
  262. /* Simply return and print nothing. */
  263. return;
  264. old_file_name = file_name;
  265. old_line_number = line_number;
  266. }
  267. fflush (stdout);
  268. #ifdef _LIBC
  269. # ifdef USE_IN_LIBIO
  270. _IO_flockfile (stderr);
  271. # else
  272. __flockfile (stderr);
  273. # endif
  274. #endif
  275. if (error_print_progname)
  276. (*error_print_progname) ();
  277. else
  278. {
  279. #if _LIBC && USE_IN_LIBIO
  280. if (_IO_fwide (stderr, 0) > 0)
  281. __fwprintf (stderr, L"%s: ", program_name);
  282. else
  283. #endif
  284. fprintf (stderr, "%s:", program_name);
  285. }
  286. if (file_name != NULL)
  287. {
  288. #if _LIBC && USE_IN_LIBIO
  289. if (_IO_fwide (stderr, 0) > 0)
  290. __fwprintf (stderr, L"%s:%d: ", file_name, line_number);
  291. else
  292. #endif
  293. fprintf (stderr, "%s:%d: ", file_name, line_number);
  294. }
  295. #ifdef VA_START
  296. VA_START (args, message);
  297. error_tail (status, errnum, message, args);
  298. #else
  299. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  300. ++error_message_count;
  301. if (errnum)
  302. fprintf (stderr, ": %s", strerror (errnum));
  303. putc ('\n', stderr);
  304. fflush (stderr);
  305. if (status)
  306. exit (status);
  307. #endif
  308. #ifdef _LIBC
  309. # ifdef USE_IN_LIBIO
  310. _IO_funlockfile (stderr);
  311. # else
  312. __funlockfile (stderr);
  313. # endif
  314. #endif
  315. }
  316. #ifdef _LIBC
  317. /* Make the weak alias. */
  318. # undef error
  319. # undef error_at_line
  320. weak_alias (__error, error)
  321. weak_alias (__error_at_line, error_at_line)
  322. #endif