error.c 3.0 KB

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