error.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  16. /* Adjusted slightly by Erik Andersen <andersen@uclibc.org> */
  17. #include <stdio.h>
  18. #include <stdarg.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <error.h>
  22. /* This variable is incremented each time `error' is called. */
  23. unsigned int error_message_count = 0;
  24. /* Sometimes we want to have at most one error per line. This
  25. variable controls whether this mode is selected or not. */
  26. int error_one_per_line;
  27. /* If NULL, error will flush stdout, then print on stderr the program
  28. name, a colon and a space. Otherwise, error will call this
  29. function without parameters instead. */
  30. void (*error_print_progname) (void) = NULL;
  31. void error (int status, int errnum, const char *message, ...)
  32. {
  33. va_list args;
  34. fflush (stdout);
  35. if (error_print_progname)
  36. (*error_print_progname) ();
  37. else
  38. fprintf (stderr, "%s: ", __uclibc_progname);
  39. va_start (args, message);
  40. vfprintf (stderr, message, args);
  41. va_end (args);
  42. ++error_message_count;
  43. if (errnum) {
  44. fprintf (stderr, ": %s", strerror (errnum));
  45. }
  46. putc ('\n', stderr);
  47. if (status)
  48. exit (status);
  49. }
  50. void error_at_line (int status, int errnum, const char *file_name,
  51. unsigned int line_number, const char *message, ...)
  52. {
  53. va_list args;
  54. if (error_one_per_line) {
  55. static const char *old_file_name;
  56. static unsigned int old_line_number;
  57. if (old_line_number == line_number &&
  58. (file_name == old_file_name || !strcmp (old_file_name, file_name)))
  59. /* Simply return and print nothing. */
  60. return;
  61. old_file_name = file_name;
  62. old_line_number = line_number;
  63. }
  64. fflush (stdout);
  65. if (error_print_progname)
  66. (*error_print_progname) ();
  67. else
  68. fprintf (stderr, "%s:", __uclibc_progname);
  69. if (file_name != NULL)
  70. fprintf (stderr, "%s:%d: ", file_name, line_number);
  71. va_start (args, message);
  72. vfprintf (stderr, message, args);
  73. va_end (args);
  74. ++error_message_count;
  75. if (errnum) {
  76. fprintf (stderr, ": %s", strerror (errnum));
  77. }
  78. putc ('\n', stderr);
  79. if (status)
  80. exit (status);
  81. }