error.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. libc_hidden_proto(strcmp)
  25. libc_hidden_proto(strerror)
  26. libc_hidden_proto(fprintf)
  27. libc_hidden_proto(exit)
  28. libc_hidden_proto(putc)
  29. libc_hidden_proto(vfprintf)
  30. libc_hidden_proto(fflush)
  31. libc_hidden_proto(fputc)
  32. libc_hidden_proto(__fputc_unlocked)
  33. libc_hidden_proto(stdout)
  34. libc_hidden_proto(stderr)
  35. /* This variable is incremented each time `error' is called. */
  36. unsigned int error_message_count = 0;
  37. /* Sometimes we want to have at most one error per line. This
  38. variable controls whether this mode is selected or not. */
  39. int error_one_per_line;
  40. /* If NULL, error will flush stdout, then print on stderr the program
  41. name, a colon and a space. Otherwise, error will call this
  42. function without parameters instead. */
  43. void (*error_print_progname) (void) = NULL;
  44. extern __typeof(error) __error attribute_hidden;
  45. void __error (int status, int errnum, const char *message, ...)
  46. {
  47. va_list args;
  48. fflush (stdout);
  49. va_start (args, message);
  50. vfprintf (stderr, message, args);
  51. va_end (args);
  52. ++error_message_count;
  53. if (errnum) {
  54. fprintf (stderr, ": %s", strerror (errnum));
  55. }
  56. putc ('\n', stderr);
  57. if (status)
  58. exit (status);
  59. }
  60. extern __typeof(error_at_line) __error_at_line attribute_hidden;
  61. void __error_at_line (int status, int errnum, const char *file_name,
  62. unsigned int line_number, const char *message, ...)
  63. {
  64. va_list args;
  65. if (error_one_per_line) {
  66. static const char *old_file_name;
  67. static unsigned int old_line_number;
  68. if (old_line_number == line_number &&
  69. (file_name == old_file_name || !strcmp (old_file_name, file_name)))
  70. /* Simply return and print nothing. */
  71. return;
  72. old_file_name = file_name;
  73. old_line_number = line_number;
  74. }
  75. fflush (stdout);
  76. if (file_name != NULL)
  77. fprintf (stderr, "%s:%d: ", file_name, line_number);
  78. va_start (args, message);
  79. vfprintf (stderr, message, args);
  80. va_end (args);
  81. ++error_message_count;
  82. if (errnum) {
  83. fprintf (stderr, ": %s", strerror (errnum));
  84. }
  85. putc ('\n', stderr);
  86. if (status)
  87. exit (status);
  88. }
  89. /* psm: keep this weak, to many use this outside of libc */
  90. weak_alias(__error,error)
  91. strong_alias(__error_at_line,error_at_line)