err.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* 4.4BSD utility functions for error messages.
  2. Copyright (C) 1995-2019 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. #include <stdarg.h>
  16. #include <err.h>
  17. #include <stdlib.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <wchar.h>
  22. #define flockfile(s) _IO_flockfile (s)
  23. #define funlockfile(s) _IO_funlockfile (s)
  24. extern char *__progname;
  25. #define VA(call) \
  26. { \
  27. va_list ap; \
  28. va_start (ap, format); \
  29. call; \
  30. va_end (ap); \
  31. }
  32. void
  33. vwarnx (const char *format, __gnuc_va_list ap)
  34. {
  35. flockfile (stderr);
  36. __fxprintf (stderr, "%s: ", __progname);
  37. if (format != NULL)
  38. __vfxprintf (stderr, format, ap);
  39. __fxprintf (stderr, "\n");
  40. funlockfile (stderr);
  41. }
  42. libc_hidden_def (vwarnx)
  43. void
  44. vwarn (const char *format, __gnuc_va_list ap)
  45. {
  46. int error = errno;
  47. flockfile (stderr);
  48. if (format != NULL)
  49. {
  50. __fxprintf (stderr, "%s: ", __progname);
  51. __vfxprintf (stderr, format, ap);
  52. __set_errno (error);
  53. __fxprintf (stderr, ": %m\n");
  54. }
  55. else
  56. {
  57. __set_errno (error);
  58. __fxprintf (stderr, "%s: %m\n", __progname);
  59. }
  60. funlockfile (stderr);
  61. }
  62. libc_hidden_def (vwarn)
  63. void
  64. warn (const char *format, ...)
  65. {
  66. VA (vwarn (format, ap))
  67. }
  68. libc_hidden_def (warn)
  69. void
  70. warnx (const char *format, ...)
  71. {
  72. VA (vwarnx (format, ap))
  73. }
  74. libc_hidden_def (warnx)
  75. void
  76. verr (int status, const char *format, __gnuc_va_list ap)
  77. {
  78. vwarn (format, ap);
  79. exit (status);
  80. }
  81. libc_hidden_def (verr)
  82. void
  83. verrx (int status, const char *format, __gnuc_va_list ap)
  84. {
  85. vwarnx (format, ap);
  86. exit (status);
  87. }
  88. libc_hidden_def (verrx)
  89. void
  90. err (int status, const char *format, ...)
  91. {
  92. VA (verr (status, format, ap))
  93. }
  94. void
  95. errx (int status, const char *format, ...)
  96. {
  97. VA (verrx (status, format, ap))
  98. }