err.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * 4.4BSD error printing functions.
  31. */
  32. #include <unistd.h>
  33. #include <stdio.h>
  34. #include <errno.h>
  35. #include <string.h>
  36. #include "host-err.h"
  37. #ifdef NEED_ERR
  38. /*
  39. * This is initialized by hostcompat_init
  40. */
  41. extern const char *hostcompat_progname;
  42. /*
  43. * Common routine for all the *err* and *warn* functions.
  44. */
  45. static
  46. void
  47. hostcompat_printerr(int use_errno, const char *fmt, va_list ap)
  48. {
  49. const char *errmsg;
  50. /*
  51. * Get the error message for the current errno.
  52. * Do this early, before doing anything that might change the
  53. * value in errno.
  54. */
  55. errmsg = strerror(errno);
  56. /*
  57. * Look up the program name.
  58. * Strictly speaking we should pull off the rightmost
  59. * path component of argv[0] and use that as the program
  60. * name (this is how BSD err* prints) but it doesn't make
  61. * much difference.
  62. */
  63. if (hostcompat_progname != NULL) {
  64. fprintf(stderr, "%s: ", hostcompat_progname);
  65. }
  66. else {
  67. fprintf(stderr, "libhostcompat: hostcompat_init not called\n");
  68. fprintf(stderr, "libhostcompat-program: ");
  69. }
  70. /* process the printf format and args */
  71. vfprintf(stderr, fmt, ap);
  72. if (use_errno) {
  73. /* if we're using errno, print the error string from above. */
  74. fprintf(stderr, ": %s\n", errmsg);
  75. }
  76. else {
  77. /* otherwise, just a newline. */
  78. fprintf(stderr, "\n");
  79. }
  80. }
  81. /*
  82. * The va_list versions of the warn/err functions.
  83. */
  84. /* warn/vwarn: use errno, don't exit */
  85. void
  86. vwarn(const char *fmt, va_list ap)
  87. {
  88. hostcompat_printerr(1, fmt, ap);
  89. }
  90. /* warnx/vwarnx: don't use errno, don't exit */
  91. void
  92. vwarnx(const char *fmt, va_list ap)
  93. {
  94. hostcompat_printerr(0, fmt, ap);
  95. }
  96. /* err/verr: use errno, then exit */
  97. void
  98. verr(int exitcode, const char *fmt, va_list ap)
  99. {
  100. hostcompat_printerr(1, fmt, ap);
  101. exit(exitcode);
  102. }
  103. /* errx/verrx: don't use errno, but do then exit */
  104. void
  105. verrx(int exitcode, const char *fmt, va_list ap)
  106. {
  107. hostcompat_printerr(0, fmt, ap);
  108. exit(exitcode);
  109. }
  110. /*
  111. * The non-va_list versions of the warn/err functions.
  112. * Just hand off to the va_list versions.
  113. */
  114. void
  115. warn(const char *fmt, ...)
  116. {
  117. va_list ap;
  118. va_start(ap, fmt);
  119. vwarn(fmt, ap);
  120. va_end(ap);
  121. }
  122. void
  123. warnx(const char *fmt, ...)
  124. {
  125. va_list ap;
  126. va_start(ap, fmt);
  127. vwarnx(fmt, ap);
  128. va_end(ap);
  129. }
  130. void
  131. err(int exitcode, const char *fmt, ...)
  132. {
  133. va_list ap;
  134. va_start(ap, fmt);
  135. verr(exitcode, fmt, ap);
  136. va_end(ap);
  137. }
  138. void
  139. errx(int exitcode, const char *fmt, ...)
  140. {
  141. va_list ap;
  142. va_start(ap, fmt);
  143. verrx(exitcode, fmt, ap);
  144. va_end(ap);
  145. }
  146. #endif /* NEED_ERR */