error.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. static int error_one_per_line;
  29. void __error (int status, int errnum, const char *message, ...)
  30. {
  31. va_list args;
  32. fflush (stdout);
  33. va_start (args, message);
  34. vfprintf (stderr, message, args);
  35. va_end (args);
  36. ++error_message_count;
  37. if (errnum) {
  38. fprintf (stderr, ": %s", strerror (errnum));
  39. }
  40. putc ('\n', stderr);
  41. if (status)
  42. exit (status);
  43. }
  44. void __error_at_line (int status, int errnum, const char *file_name,
  45. unsigned int line_number, const char *message, ...)
  46. {
  47. va_list args;
  48. if (error_one_per_line) {
  49. static const char *old_file_name;
  50. static unsigned int old_line_number;
  51. if (old_line_number == line_number &&
  52. (file_name == old_file_name || !strcmp (old_file_name, file_name)))
  53. /* Simply return and print nothing. */
  54. return;
  55. old_file_name = file_name;
  56. old_line_number = line_number;
  57. }
  58. fflush (stdout);
  59. if (file_name != NULL)
  60. fprintf (stderr, "%s:%d: ", file_name, line_number);
  61. va_start (args, message);
  62. vfprintf (stderr, message, args);
  63. va_end (args);
  64. ++error_message_count;
  65. if (errnum) {
  66. fprintf (stderr, ": %s", strerror (errnum));
  67. }
  68. putc ('\n', stderr);
  69. if (status)
  70. exit (status);
  71. }
  72. /* Use the weaks here in an effort at controlling namespace pollution */
  73. #undef error
  74. #undef error_at_line
  75. weak_alias (__error, error)
  76. weak_alias (__error_at_line, error_at_line)