error.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <errno.h> /* for definition of errno */
  2. #include <stdarg.h> /* ANSI C header file */
  3. #include "ourhdr.h"
  4. static void err_doit(int, const char *, va_list);
  5. char *pname = NULL; /* caller can set this from argv[0] */
  6. /* Nonfatal error related to a system call.
  7. * Print a message and return. */
  8. void
  9. /* $f err_ret $ */
  10. err_ret(const char *fmt, ...)
  11. {
  12. va_list ap;
  13. va_start(ap, fmt);
  14. err_doit(1, fmt, ap);
  15. va_end(ap);
  16. return;
  17. }
  18. /* Fatal error related to a system call.
  19. * Print a message and terminate. */
  20. void
  21. /* $f err_sys $ */
  22. err_sys(const char *fmt, ...)
  23. {
  24. va_list ap;
  25. va_start(ap, fmt);
  26. err_doit(1, fmt, ap);
  27. va_end(ap);
  28. exit(1);
  29. }
  30. /* Fatal error related to a system call.
  31. * Print a message, dump core, and terminate. */
  32. void
  33. /* $f err_dump $ */
  34. err_dump(const char *fmt, ...)
  35. {
  36. va_list ap;
  37. va_start(ap, fmt);
  38. err_doit(1, fmt, ap);
  39. va_end(ap);
  40. abort(); /* dump core and terminate */
  41. exit(1); /* shouldn't get here */
  42. }
  43. /* Nonfatal error unrelated to a system call.
  44. * Print a message and return. */
  45. void
  46. /* $f err_msg $ */
  47. err_msg(const char *fmt, ...)
  48. {
  49. va_list ap;
  50. va_start(ap, fmt);
  51. err_doit(0, fmt, ap);
  52. va_end(ap);
  53. return;
  54. }
  55. /* Fatal error unrelated to a system call.
  56. * Print a message and terminate. */
  57. void
  58. /* $f err_quit $ */
  59. err_quit(const char *fmt, ...)
  60. {
  61. va_list ap;
  62. va_start(ap, fmt);
  63. err_doit(0, fmt, ap);
  64. va_end(ap);
  65. exit(1);
  66. }
  67. /* Print a message and return to caller.
  68. * Caller specifies "errnoflag". */
  69. static void
  70. err_doit(int errnoflag, const char *fmt, va_list ap)
  71. {
  72. int errno_save;
  73. char buf[MAXLINE];
  74. errno_save = errno; /* value caller might want printed */
  75. vsprintf(buf, fmt, ap);
  76. if (errnoflag)
  77. sprintf(buf+strlen(buf), ": %s", strerror(errno_save));
  78. strcat(buf, "\n");
  79. fflush(stdout); /* in case stdout and stderr are the same */
  80. fputs(buf, stderr);
  81. fflush(stderr); /* SunOS 4.1.* doesn't grok NULL argument */
  82. return;
  83. }