2
0

error.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Portions created by SGI are Copyright (C) 2000 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  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. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Silicon Graphics, Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  24. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include "st.h"
  37. /*
  38. * Simple error reporting functions.
  39. * Suggested in W. Richard Stevens' "Advanced Programming in UNIX
  40. * Environment".
  41. */
  42. #define MAXLINE 4096 /* max line length */
  43. static void err_doit(int, int, const char *, va_list);
  44. /*
  45. * Nonfatal error related to a system call.
  46. * Print a message and return.
  47. */
  48. void err_sys_report(int fd, const char *fmt, ...)
  49. {
  50. va_list ap;
  51. va_start(ap, fmt);
  52. err_doit(fd, 1, fmt, ap);
  53. va_end(ap);
  54. }
  55. /*
  56. * Fatal error related to a system call.
  57. * Print a message and terminate.
  58. */
  59. void err_sys_quit(int fd, const char *fmt, ...)
  60. {
  61. va_list ap;
  62. va_start(ap, fmt);
  63. err_doit(fd, 1, fmt, ap);
  64. va_end(ap);
  65. exit(1);
  66. }
  67. /*
  68. * Fatal error related to a system call.
  69. * Print a message, dump core, and terminate.
  70. */
  71. void err_sys_dump(int fd, const char *fmt, ...)
  72. {
  73. va_list ap;
  74. va_start(ap, fmt);
  75. err_doit(fd, 1, fmt, ap);
  76. va_end(ap);
  77. abort(); /* dump core and terminate */
  78. exit(1); /* shouldn't get here */
  79. }
  80. /*
  81. * Nonfatal error unrelated to a system call.
  82. * Print a message and return.
  83. */
  84. void err_report(int fd, const char *fmt, ...)
  85. {
  86. va_list ap;
  87. va_start(ap, fmt);
  88. err_doit(fd, 0, fmt, ap);
  89. va_end(ap);
  90. }
  91. /*
  92. * Fatal error unrelated to a system call.
  93. * Print a message and terminate.
  94. */
  95. void err_quit(int fd, const char *fmt, ...)
  96. {
  97. va_list ap;
  98. va_start(ap, fmt);
  99. err_doit(fd, 0, fmt, ap);
  100. va_end(ap);
  101. exit(1);
  102. }
  103. /*
  104. * Return a pointer to a string containing current time.
  105. */
  106. char *err_tstamp(void)
  107. {
  108. static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  109. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  110. static char str[32];
  111. static time_t lastt = 0;
  112. struct tm *tmp;
  113. time_t currt = st_time();
  114. if (currt == lastt)
  115. return str;
  116. tmp = localtime(&currt);
  117. sprintf(str, "[%02d/%s/%d:%02d:%02d:%02d] ", tmp->tm_mday,
  118. months[tmp->tm_mon], 1900 + tmp->tm_year, tmp->tm_hour,
  119. tmp->tm_min, tmp->tm_sec);
  120. lastt = currt;
  121. return str;
  122. }
  123. /*
  124. * Print a message and return to caller.
  125. * Caller specifies "errnoflag".
  126. */
  127. static void err_doit(int fd, int errnoflag, const char *fmt, va_list ap)
  128. {
  129. int errno_save;
  130. char buf[MAXLINE];
  131. errno_save = errno; /* value caller might want printed */
  132. strcpy(buf, err_tstamp()); /* prepend a message with time stamp */
  133. vsprintf(buf + strlen(buf), fmt, ap);
  134. if (errnoflag)
  135. sprintf(buf + strlen(buf), ": %s\n", strerror(errno_save));
  136. else
  137. strcat(buf, "\n");
  138. write(fd, buf, strlen(buf));
  139. errno = errno_save;
  140. }