verror.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* va_list error handler for noninteractive utilities
  2. Copyright (C) 2006-2007, 2009-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Eric Blake. */
  14. #include <config.h>
  15. #include "verror.h"
  16. #include "xvasprintf.h"
  17. #include <errno.h>
  18. #include <stdarg.h>
  19. #include <stdlib.h>
  20. #if ENABLE_NLS
  21. # include "gettext.h"
  22. # define _(msgid) gettext (msgid)
  23. #endif
  24. #ifndef _
  25. # define _(String) String
  26. #endif
  27. /* Print a message with 'vfprintf (stderr, FORMAT, ARGS)';
  28. if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
  29. If STATUS is nonzero, terminate the program with 'exit (STATUS)'.
  30. Use the globals error_print_progname and error_message_count similarly
  31. to error(). */
  32. void
  33. verror (int status, int errnum, const char *format, va_list args)
  34. {
  35. verror_at_line (status, errnum, NULL, 0, format, args);
  36. }
  37. /* Print a message with 'vfprintf (stderr, FORMAT, ARGS)';
  38. if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
  39. If STATUS is nonzero, terminate the program with 'exit (STATUS)'.
  40. If FNAME is not NULL, prepend the message with "FNAME:LINENO:".
  41. Use the globals error_print_progname, error_message_count, and
  42. error_one_per_line similarly to error_at_line(). */
  43. void
  44. verror_at_line (int status, int errnum, const char *file,
  45. unsigned int line_number, const char *format, va_list args)
  46. {
  47. char *message = xvasprintf (format, args);
  48. if (message)
  49. {
  50. /* Until http://sourceware.org/bugzilla/show_bug.cgi?id=2997 is fixed,
  51. glibc violates GNU Coding Standards when the file argument to
  52. error_at_line is NULL. */
  53. if (file)
  54. error_at_line (status, errnum, file, line_number, "%s", message);
  55. else
  56. error (status, errnum, "%s", message);
  57. }
  58. else
  59. {
  60. /* EOVERFLOW, EINVAL, and EILSEQ from xvasprintf are signs of
  61. serious programmer errors. */
  62. error (0, errno, _("unable to display error message"));
  63. abort ();
  64. }
  65. free (message);
  66. }