error.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Error handler for noninteractive utilities
  2. Copyright (C) 1990-1998, 2000-2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  17. /* Adjusted slightly by Erik Andersen <andersen@uclibc.org> */
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <error.h>
  23. libc_hidden_proto(strcmp)
  24. libc_hidden_proto(strerror)
  25. libc_hidden_proto(fprintf)
  26. libc_hidden_proto(exit)
  27. libc_hidden_proto(putc)
  28. libc_hidden_proto(vfprintf)
  29. libc_hidden_proto(fflush)
  30. libc_hidden_proto(fputc)
  31. libc_hidden_proto(__fputc_unlocked)
  32. libc_hidden_proto(stdout)
  33. libc_hidden_proto(stderr)
  34. /* This variable is incremented each time `error' is called. */
  35. unsigned int error_message_count = 0;
  36. /* Sometimes we want to have at most one error per line. This
  37. variable controls whether this mode is selected or not. */
  38. int error_one_per_line;
  39. /* If NULL, error will flush stdout, then print on stderr the program
  40. name, a colon and a space. Otherwise, error will call this
  41. function without parameters instead. */
  42. /* void (*error_print_progname) (void) = NULL; */
  43. extern __typeof(error) __error attribute_hidden;
  44. void __error (int status, int errnum, const char *message, ...)
  45. {
  46. va_list args;
  47. fflush (stdout);
  48. va_start (args, message);
  49. vfprintf (stderr, message, args);
  50. va_end (args);
  51. ++error_message_count;
  52. if (errnum) {
  53. fprintf (stderr, ": %s", strerror (errnum));
  54. }
  55. putc ('\n', stderr);
  56. if (status)
  57. exit (status);
  58. }
  59. extern __typeof(error_at_line) __error_at_line attribute_hidden;
  60. void __error_at_line (int status, int errnum, const char *file_name,
  61. unsigned int line_number, const char *message, ...)
  62. {
  63. va_list args;
  64. if (error_one_per_line) {
  65. static const char *old_file_name;
  66. static unsigned int old_line_number;
  67. if (old_line_number == line_number &&
  68. (file_name == old_file_name || !strcmp (old_file_name, file_name)))
  69. /* Simply return and print nothing. */
  70. return;
  71. old_file_name = file_name;
  72. old_line_number = line_number;
  73. }
  74. fflush (stdout);
  75. if (file_name != NULL)
  76. fprintf (stderr, "%s:%d: ", file_name, line_number);
  77. va_start (args, message);
  78. vfprintf (stderr, message, args);
  79. va_end (args);
  80. ++error_message_count;
  81. if (errnum) {
  82. fprintf (stderr, ": %s", strerror (errnum));
  83. }
  84. putc ('\n', stderr);
  85. if (status)
  86. exit (status);
  87. }
  88. /* psm: keep this weak, too many use this in common code */
  89. weak_alias(__error,error)
  90. strong_alias(__error_at_line,error_at_line)