PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/gnu/usr.bin/grep/error.c

https://bitbucket.org/freebsd/freebsd-head/
C | 276 lines | 201 code | 32 blank | 43 comment | 32 complexity | 57f0239759427daf6c69912d5956fe38 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 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 Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. 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. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #if HAVE_LIBINTL_H
  23. # include <libintl.h>
  24. #endif
  25. #if HAVE_VPRINTF || HAVE_DOPRNT || _LIBC
  26. # if __STDC__
  27. # include <stdarg.h>
  28. # define VA_START(args, lastarg) va_start(args, lastarg)
  29. # else
  30. # include <varargs.h>
  31. # define VA_START(args, lastarg) va_start(args)
  32. # endif
  33. #else
  34. # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
  35. # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  36. #endif
  37. #if STDC_HEADERS || _LIBC
  38. # include <stdlib.h>
  39. # include <string.h>
  40. #else
  41. void exit ();
  42. #endif
  43. #include "error.h"
  44. #ifndef HAVE_DECL_STRERROR_R
  45. "this configure-time declaration test was not run"
  46. #endif
  47. #if !HAVE_DECL_STRERROR_R
  48. char *strerror_r ();
  49. #endif
  50. #ifndef _
  51. # define _(String) String
  52. #endif
  53. /* If NULL, error will flush stdout, then print on stderr the program
  54. name, a colon and a space. Otherwise, error will call this
  55. function without parameters instead. */
  56. void (*error_print_progname) (
  57. #if __STDC__ - 0
  58. void
  59. #endif
  60. );
  61. /* This variable is incremented each time `error' is called. */
  62. unsigned int error_message_count;
  63. #ifdef _LIBC
  64. /* In the GNU C library, there is a predefined variable for this. */
  65. # define program_name program_invocation_name
  66. # include <errno.h>
  67. /* In GNU libc we want do not want to use the common name `error' directly.
  68. Instead make it a weak alias. */
  69. # define error __error
  70. # define error_at_line __error_at_line
  71. # ifdef USE_IN_LIBIO
  72. # include <libio/iolibio.h>
  73. # define fflush(s) _IO_fflush (s)
  74. # endif
  75. #else /* not _LIBC */
  76. /* The calling program should define program_name and set it to the
  77. name of the executing program. */
  78. extern char *program_name;
  79. # ifdef HAVE_STRERROR_R
  80. # define __strerror_r strerror_r
  81. # else
  82. # if HAVE_STRERROR
  83. # ifndef strerror /* On some systems, strerror is a macro */
  84. char *strerror ();
  85. # endif
  86. # else
  87. static char *
  88. private_strerror (errnum)
  89. int errnum;
  90. {
  91. extern char *sys_errlist[];
  92. extern int sys_nerr;
  93. if (errnum > 0 && errnum <= sys_nerr)
  94. return _(sys_errlist[errnum]);
  95. return _("Unknown system error");
  96. }
  97. # define strerror private_strerror
  98. # endif /* HAVE_STRERROR */
  99. # endif /* HAVE_STRERROR_R */
  100. #endif /* not _LIBC */
  101. /* Print the program name and error message MESSAGE, which is a printf-style
  102. format string with optional args.
  103. If ERRNUM is nonzero, print its corresponding system error message.
  104. Exit with status STATUS if it is nonzero. */
  105. /* VARARGS */
  106. void
  107. #if defined VA_START && __STDC__
  108. error (int status, int errnum, const char *message, ...)
  109. #else
  110. error (status, errnum, message, va_alist)
  111. int status;
  112. int errnum;
  113. char *message;
  114. va_dcl
  115. #endif
  116. {
  117. #ifdef VA_START
  118. va_list args;
  119. #endif
  120. if (error_print_progname)
  121. (*error_print_progname) ();
  122. else
  123. {
  124. fflush (stdout);
  125. fprintf (stderr, "%s: ", program_name);
  126. }
  127. #ifdef VA_START
  128. VA_START (args, message);
  129. # if HAVE_VPRINTF || _LIBC
  130. vfprintf (stderr, message, args);
  131. # else
  132. _doprnt (message, args, stderr);
  133. # endif
  134. va_end (args);
  135. #else
  136. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  137. #endif
  138. ++error_message_count;
  139. if (errnum)
  140. {
  141. #if defined HAVE_STRERROR_R || _LIBC
  142. char errbuf[1024];
  143. # if HAVE_WORKING_STRERROR_R || _LIBC
  144. fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
  145. # else
  146. /* Don't use __strerror_r's return value because on some systems
  147. (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
  148. __strerror_r (errnum, errbuf, sizeof errbuf);
  149. fprintf (stderr, ": %s", errbuf);
  150. # endif
  151. #else
  152. fprintf (stderr, ": %s", strerror (errnum));
  153. #endif
  154. }
  155. putc ('\n', stderr);
  156. fflush (stderr);
  157. if (status)
  158. exit (status);
  159. }
  160. /* Sometimes we want to have at most one error per line. This
  161. variable controls whether this mode is selected or not. */
  162. int error_one_per_line;
  163. void
  164. #if defined VA_START && __STDC__
  165. error_at_line (int status, int errnum, const char *file_name,
  166. unsigned int line_number, const char *message, ...)
  167. #else
  168. error_at_line (status, errnum, file_name, line_number, message, va_alist)
  169. int status;
  170. int errnum;
  171. const char *file_name;
  172. unsigned int line_number;
  173. char *message;
  174. va_dcl
  175. #endif
  176. {
  177. #ifdef VA_START
  178. va_list args;
  179. #endif
  180. if (error_one_per_line)
  181. {
  182. static const char *old_file_name;
  183. static unsigned int old_line_number;
  184. if (old_line_number == line_number &&
  185. (file_name == old_file_name || !strcmp (old_file_name, file_name)))
  186. /* Simply return and print nothing. */
  187. return;
  188. old_file_name = file_name;
  189. old_line_number = line_number;
  190. }
  191. if (error_print_progname)
  192. (*error_print_progname) ();
  193. else
  194. {
  195. fflush (stdout);
  196. fprintf (stderr, "%s:", program_name);
  197. }
  198. if (file_name != NULL)
  199. fprintf (stderr, "%s:%d: ", file_name, line_number);
  200. #ifdef VA_START
  201. VA_START (args, message);
  202. # if HAVE_VPRINTF || _LIBC
  203. vfprintf (stderr, message, args);
  204. # else
  205. _doprnt (message, args, stderr);
  206. # endif
  207. va_end (args);
  208. #else
  209. fprintf (stderr, message, a1, a2, a3, a4, a5, a6, a7, a8);
  210. #endif
  211. ++error_message_count;
  212. if (errnum)
  213. {
  214. #if defined HAVE_STRERROR_R || _LIBC
  215. char errbuf[1024];
  216. # if HAVE_WORKING_STRERROR_R || _LIBC
  217. fprintf (stderr, ": %s", __strerror_r (errnum, errbuf, sizeof errbuf));
  218. # else
  219. /* Don't use __strerror_r's return value because on some systems
  220. (at least DEC UNIX 4.0[A-D]) strerror_r returns `int'. */
  221. __strerror_r (errnum, errbuf, sizeof errbuf);
  222. fprintf (stderr, ": %s", errbuf);
  223. # endif
  224. #else
  225. fprintf (stderr, ": %s", strerror (errnum));
  226. #endif
  227. }
  228. putc ('\n', stderr);
  229. fflush (stderr);
  230. if (status)
  231. exit (status);
  232. }
  233. #ifdef _LIBC
  234. /* Make the weak alias. */
  235. # undef error
  236. # undef error_at_line
  237. weak_alias (__error, error)
  238. weak_alias (__error_at_line, error_at_line)
  239. #endif