err.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* 4.4BSD utility functions for error messages.
  2. Copyright (C) 1995-2026 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. <https://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_internal (const char *format, __gnuc_va_list ap,
  34. unsigned int mode_flags)
  35. {
  36. flockfile (stderr);
  37. __fxprintf (stderr, "%s: ", __progname);
  38. if (format != NULL)
  39. __vfxprintf (stderr, format, ap, mode_flags);
  40. __fxprintf (stderr, "\n");
  41. funlockfile (stderr);
  42. }
  43. void
  44. __vwarn_internal (const char *format, __gnuc_va_list ap,
  45. unsigned int mode_flags)
  46. {
  47. int error = errno;
  48. flockfile (stderr);
  49. if (format != NULL)
  50. {
  51. __fxprintf (stderr, "%s: ", __progname);
  52. __vfxprintf (stderr, format, ap, mode_flags);
  53. __set_errno (error);
  54. __fxprintf (stderr, ": %m\n");
  55. }
  56. else
  57. {
  58. __set_errno (error);
  59. __fxprintf (stderr, "%s: %m\n", __progname);
  60. }
  61. funlockfile (stderr);
  62. }
  63. void
  64. vwarn (const char *format, __gnuc_va_list ap)
  65. {
  66. __vwarn_internal (format, ap, 0);
  67. }
  68. libc_hidden_def (vwarn)
  69. void
  70. vwarnx (const char *format, __gnuc_va_list ap)
  71. {
  72. __vwarnx_internal (format, ap, 0);
  73. }
  74. libc_hidden_def (vwarnx)
  75. void
  76. warn (const char *format, ...)
  77. {
  78. VA (vwarn (format, ap))
  79. }
  80. libc_hidden_def (warn)
  81. void
  82. warnx (const char *format, ...)
  83. {
  84. VA (vwarnx (format, ap))
  85. }
  86. libc_hidden_def (warnx)
  87. void
  88. verr (int status, const char *format, __gnuc_va_list ap)
  89. {
  90. vwarn (format, ap);
  91. exit (status);
  92. }
  93. libc_hidden_def (verr)
  94. void
  95. verrx (int status, const char *format, __gnuc_va_list ap)
  96. {
  97. vwarnx (format, ap);
  98. exit (status);
  99. }
  100. libc_hidden_def (verrx)
  101. void
  102. err (int status, const char *format, ...)
  103. {
  104. VA (verr (status, format, ap))
  105. }
  106. void
  107. errx (int status, const char *format, ...)
  108. {
  109. VA (verrx (status, format, ap))
  110. }