error.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Error handler for noninteractive utilities
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
  7. /* Adjusted slightly by Erik Andersen <andersen@uclibc.org> */
  8. #include <stdio.h>
  9. #include <stdarg.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <error.h>
  13. libc_hidden_proto(strcmp)
  14. libc_hidden_proto(strerror)
  15. libc_hidden_proto(fprintf)
  16. libc_hidden_proto(exit)
  17. libc_hidden_proto(putc)
  18. libc_hidden_proto(vfprintf)
  19. libc_hidden_proto(fflush)
  20. libc_hidden_proto(fputc)
  21. libc_hidden_proto(__fputc_unlocked)
  22. libc_hidden_proto(stdout)
  23. libc_hidden_proto(stderr)
  24. /* This variable is incremented each time `error' is called. */
  25. unsigned int error_message_count = 0;
  26. /* Sometimes we want to have at most one error per line. This
  27. variable controls whether this mode is selected or not. */
  28. int error_one_per_line;
  29. /* If NULL, error will flush stdout, then print on stderr the program
  30. name, a colon and a space. Otherwise, error will call this
  31. function without parameters instead. */
  32. void (*error_print_progname) (void) = NULL;
  33. extern __typeof(error) __error attribute_hidden;
  34. void __error (int status, int errnum, const char *message, ...)
  35. {
  36. va_list args;
  37. fflush (stdout);
  38. va_start (args, message);
  39. vfprintf (stderr, message, args);
  40. va_end (args);
  41. ++error_message_count;
  42. if (errnum) {
  43. fprintf (stderr, ": %s", strerror (errnum));
  44. }
  45. putc ('\n', stderr);
  46. if (status)
  47. exit (status);
  48. }
  49. extern __typeof(error_at_line) __error_at_line attribute_hidden;
  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 (file_name != NULL)
  66. fprintf (stderr, "%s:%d: ", file_name, line_number);
  67. va_start (args, message);
  68. vfprintf (stderr, message, args);
  69. va_end (args);
  70. ++error_message_count;
  71. if (errnum) {
  72. fprintf (stderr, ": %s", strerror (errnum));
  73. }
  74. putc ('\n', stderr);
  75. if (status)
  76. exit (status);
  77. }
  78. /* psm: keep this weak, too many use this in common code */
  79. weak_alias(__error,error)
  80. strong_alias(__error_at_line,error_at_line)