err.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* $Id$ */
  2. /*
  3. * err.c
  4. *
  5. * Adapted from OpenBSD libc *err* *warn* code.
  6. *
  7. * Copyright (c) 2001-2010 Aaron Turner.
  8. *
  9. * Copyright (c) 2013-2018 Fred Klassen - AppNeta
  10. *
  11. * Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  12. *
  13. * Copyright (c) 1993
  14. * The Regents of the University of California. All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. * 3. All advertising materials mentioning features or use of this software
  25. * must display the following acknowledgement:
  26. * This product includes software developed by the University of
  27. * California, Berkeley and its contributors.
  28. * 4. Neither the name of the University nor the names of its contributors
  29. * may be used to endorse or promote products derived from this software
  30. * without specific prior written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  33. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  36. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  38. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  42. * SUCH DAMAGE.
  43. */
  44. #include "config.h"
  45. #include "defines.h"
  46. #include "common.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <stdarg.h>
  50. #include <string.h>
  51. #include <errno.h>
  52. #ifdef DEBUG
  53. extern int debug;
  54. #endif
  55. /**
  56. * writes a notice message to stderr. Always forces a newline
  57. */
  58. void
  59. notice(const char *fmt, ...)
  60. {
  61. va_list ap;
  62. va_start(ap, fmt);
  63. if (fmt != NULL)
  64. (void)vfprintf(stderr, fmt, ap);
  65. (void)fprintf(stderr, "\n");
  66. va_end(ap);
  67. fflush(NULL);
  68. }
  69. /**
  70. * Inner call to dbgx() which prints the function, line & function along
  71. * with the message to stderr. Always forces a newline.
  72. *
  73. * You don't actually want to call this! use dbgx() instead!
  74. */
  75. #ifdef DEBUG
  76. void
  77. _our_verbose_dbgx(int dbg_level, const char *fmt, const char *function,
  78. const int line, const char *file, ...)
  79. {
  80. va_list ap;
  81. if (debug < dbg_level)
  82. return;
  83. fprintf(stderr, "DEBUG%d in %s:%s() line %d: ", dbg_level, file,
  84. function, line);
  85. va_start(ap, file);
  86. if (fmt != NULL)
  87. (void)vfprintf(stderr, fmt, ap);
  88. (void)fprintf(stderr, "\n");
  89. va_end(ap);
  90. fflush(NULL);
  91. }
  92. #endif
  93. /**
  94. * Inner call to errx() which when in DEBUG mode, prints the function, line & file
  95. * along with the actual error message to stderr. Alawys forces a newline
  96. */
  97. #ifdef DEBUG
  98. void
  99. _our_verbose_errx(int eval, const char *fmt, const char *function, const int line, const char *file, ...) {
  100. #else
  101. void
  102. _our_verbose_errx(int eval, const char *fmt, ...) {
  103. #endif
  104. va_list ap;
  105. #ifdef DEBUG
  106. fprintf(stderr, "\nFatal Error in %s:%s() line %d:\n", file, function, line);
  107. va_start(ap, file);
  108. #else
  109. fprintf(stderr, "\nFatal Error: ");
  110. va_start(ap, fmt);
  111. #endif
  112. if (fmt != NULL)
  113. (void)vfprintf(stderr, fmt, ap);
  114. (void)fprintf(stderr, "\n");
  115. va_end(ap);
  116. exit(eval);
  117. }
  118. /**
  119. * Inner call to warnx() which when in DEBUG mode, prints the function, line & file
  120. * along with the actual warning to stderr. Alawys forces a newline
  121. */
  122. #ifdef DEBUG
  123. void
  124. _our_verbose_warnx(const char *fmt, const char *function, const int line, const char *file, ...) {
  125. #else
  126. void
  127. _our_verbose_warnx(const char *fmt, ...) {
  128. #endif
  129. va_list ap;
  130. #ifdef DEBUG
  131. fprintf(stderr, "Warning in %s:%s() line %d:\n", file, function, line);
  132. va_start(ap, file);
  133. #else
  134. fprintf(stderr, "Warning: ");
  135. va_start(ap, fmt);
  136. #endif
  137. if (fmt != NULL)
  138. (void)vfprintf(stderr, fmt, ap);
  139. (void)fprintf(stderr, "\n");
  140. va_end(ap);
  141. }