error.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #define strerror __strerror
  20. #define vfprintf __vfprintf
  21. #define fflush __fflush
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "error.h"
  27. extern int __putc(int c, FILE *stream) attribute_hidden;
  28. /* This variable is incremented each time `error' is called. */
  29. unsigned int error_message_count = 0;
  30. hidden_strong_alias(error_message_count, __error_message_count)
  31. /* Sometimes we want to have at most one error per line. This
  32. variable controls whether this mode is selected or not. */
  33. int error_one_per_line;
  34. /* If NULL, error will flush stdout, then print on stderr the program
  35. name, a colon and a space. Otherwise, error will call this
  36. function without parameters instead. */
  37. void (*error_print_progname) (void) = NULL;
  38. void __error (int status, int errnum, const char *message, ...)
  39. {
  40. va_list args;
  41. fflush (stdout);
  42. va_start (args, message);
  43. vfprintf (stderr, message, args);
  44. va_end (args);
  45. ++__error_message_count;
  46. if (errnum) {
  47. fprintf (stderr, ": %s", strerror (errnum));
  48. }
  49. __putc ('\n', stderr);
  50. if (status)
  51. __exit (status);
  52. }
  53. void __error_at_line (int status, int errnum, const char *file_name,
  54. unsigned int line_number, const char *message, ...)
  55. {
  56. va_list args;
  57. if (error_one_per_line) {
  58. static const char *old_file_name;
  59. static unsigned int old_line_number;
  60. if (old_line_number == line_number &&
  61. (file_name == old_file_name || !__strcmp (old_file_name, file_name)))
  62. /* Simply return and print nothing. */
  63. return;
  64. old_file_name = file_name;
  65. old_line_number = line_number;
  66. }
  67. fflush (stdout);
  68. if (file_name != NULL)
  69. fprintf (stderr, "%s:%d: ", file_name, line_number);
  70. va_start (args, message);
  71. vfprintf (stderr, message, args);
  72. va_end (args);
  73. ++__error_message_count;
  74. if (errnum) {
  75. fprintf (stderr, ": %s", strerror (errnum));
  76. }
  77. __putc ('\n', stderr);
  78. if (status)
  79. __exit (status);
  80. }
  81. /* Use the weaks here in an effort at controlling namespace pollution */
  82. #undef error
  83. #undef error_at_line
  84. weak_alias (__error, error)
  85. weak_alias (__error_at_line, error_at_line)