error.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 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. /* Adjusted slightly by Erik Andersen <andersen@uclibc.org> */
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "error.h"
  24. /* This variable is incremented each time `error' is called. */
  25. unsigned int error_message_count;
  26. /* Sometimes we want to have at most one error per line. This
  27. variable controls whether this mode is selected or not. */
  28. int error_one_per_line;
  29. /* If NULL, error will flush stdout, then print on stderr the program
  30. name, a colon and a space. Otherwise, error will call this
  31. function without parameters instead. */
  32. void (*error_print_progname) (void) = NULL;
  33. void __error (int status, int errnum, const char *message, ...)
  34. {
  35. va_list args;
  36. fflush (stdout);
  37. va_start (args, message);
  38. vfprintf (stderr, message, args);
  39. va_end (args);
  40. ++error_message_count;
  41. if (errnum) {
  42. fprintf (stderr, ": %s", strerror (errnum));
  43. }
  44. putc ('\n', stderr);
  45. if (status)
  46. exit (status);
  47. }
  48. void __error_at_line (int status, int errnum, const char *file_name,
  49. unsigned int line_number, const char *message, ...)
  50. {
  51. va_list args;
  52. if (error_one_per_line) {
  53. static const char *old_file_name;
  54. static unsigned int old_line_number;
  55. if (old_line_number == line_number &&
  56. (file_name == old_file_name || !strcmp (old_file_name, file_name)))
  57. /* Simply return and print nothing. */
  58. return;
  59. old_file_name = file_name;
  60. old_line_number = line_number;
  61. }
  62. fflush (stdout);
  63. if (file_name != NULL)
  64. fprintf (stderr, "%s:%d: ", file_name, line_number);
  65. va_start (args, message);
  66. vfprintf (stderr, message, args);
  67. va_end (args);
  68. ++error_message_count;
  69. if (errnum) {
  70. fprintf (stderr, ": %s", strerror (errnum));
  71. }
  72. putc ('\n', stderr);
  73. if (status)
  74. exit (status);
  75. }
  76. /* Use the weaks here in an effort at controlling namespace pollution */
  77. #undef error
  78. #undef error_at_line
  79. weak_alias (__error, error)
  80. weak_alias (__error_at_line, error_at_line)
  81. #include "err.h"
  82. #include "errno.h"
  83. /* NORETURN */
  84. void verr (int status, const char *message, va_list args)
  85. {
  86. fflush (stdout);
  87. vfprintf (stderr, message, args);
  88. if (errno) {
  89. fprintf (stderr, ": %s", strerror (errno));
  90. }
  91. putc ('\n', stderr);
  92. if (status)
  93. exit (status);
  94. }
  95. /* NORETURN */
  96. void verrx (int status, const char *message, va_list args)
  97. {
  98. fflush (stdout);
  99. vfprintf (stderr, message, args);
  100. if (status)
  101. exit (status);
  102. }
  103. void vwarn (const char *message, va_list args)
  104. {
  105. verr (0, message, args);
  106. }
  107. void vwarnx (const char *message, va_list args)
  108. {
  109. verrx (0, message, args);
  110. }
  111. void err (int status, const char *message, ...)
  112. {
  113. va_list args;
  114. va_start (args, message);
  115. verr (status, message, args);
  116. va_end (args);
  117. }
  118. void errx (int status, const char *message, ...)
  119. {
  120. va_list args;
  121. va_start (args, message);
  122. verrx (status, message, args);
  123. va_end (args);
  124. }
  125. void warn (const char *message, ...)
  126. {
  127. va_list args;
  128. va_start (args, message);
  129. verr (0, message, args);
  130. va_end (args);
  131. }
  132. void warnx (const char *message, ...)
  133. {
  134. va_list args;
  135. va_start (args, message);
  136. verrx (0, message, args);
  137. va_end (args);
  138. }