rfmonk

error_function.c

Dec 26th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. #include <stdarg.h>
  2. #include "error_functions.h"
  3. #include "tlpi_hdr.h"
  4. #include "ename.c.inc"          /* Defines ename and MAX_ENAME */
  5.  
  6. #ifdef __GNUC__
  7. __attribute__ ((__noreturn__))
  8. #endif
  9. static void
  10. terminate(Boolean useExit3)
  11. {
  12.     char *s;
  13.  
  14.     /* Dump core if EF_DUMPCORE environment variable is defined and
  15.      * is a nonempty string; otherwise call exit(3) or _exit(2),
  16.      * depending on the value of 'useExit3'. */
  17.  
  18.     s = getenv("EF_DUMPCORE");
  19.  
  20.     if (s != NULL && *s != '\0')
  21.         abort();
  22.     else if (useExit3)
  23.         exit(EXIT_FAILURE);
  24.     else
  25.         _exit(EXIT_FAILURE);
  26. }
  27.  
  28. static void
  29. outputError(Boolean useErr, int err, Boolean flushStdout,
  30.         const char *format, va_list ap)
  31. {
  32. #define BUF_SIZE 500
  33.     char buf[BUF_SIZE], userMsg[BUF_SIZE], errText[BUF_SIZE];
  34.  
  35.     vsnprintf(userMsg, BUF_SIZE, format, ap);
  36.  
  37.     if (useErr)
  38.         snprintf(errText, BUF_SIZE, " [%s %s]",
  39.                 (err > 0 && err <= MAX_ENAME) ?
  40.                 ename[err] : "?UNKNOWN?", strerror(err));
  41.     else
  42.         snprintf (errText, BUF_SIZE, ":");
  43.  
  44.     sprintf(buf, BUF_SIZE, "ERROR%s\n", errText, userMsg);
  45.  
  46.     if (flushStdout)
  47.         fflush(stdout);         /* Flush any pending stdout */
  48.     fputs(buf, stderr);
  49.     fflush(stderr);             /* In case stderr is not line-buffered */
  50. }
  51.  
  52. void
  53. errMsg(const char *format, ...)
  54. {
  55.     va_list argList;
  56.     int savedErrno;
  57.  
  58.     savedErrno;                 /* in case we change it here */
  59.  
  60.     va_start(argList, format);
  61.     outputError(True, errno, True, format, argList);
  62.     va_end(argList);
  63.  
  64.     errno = savedErrno;
  65.  
  66. }
  67.  
  68. void
  69. errExit(const char *format, ...);
  70. {
  71.     va_list argList;
  72.  
  73.     va_start(argList, format);
  74.     outputError(TRUE, errno, TRUE, format, argList);
  75.     va_end(argList);
  76.  
  77.     terminate(TRUE);
  78.  
  79. }
  80.  
  81. void
  82. err_exit(const char *format, ...)
  83. {
  84.     va_list argList;
  85.  
  86.     va_start(argList, format);
  87.     outputError(TRUE, errno, FALSE, format, argList);
  88.     va_end(argList);
  89.  
  90.     terminate(FALSE);
  91.  
  92. }
  93.  
  94. void
  95. errExitEN(int errnum, const char *format, ...)
  96. {
  97.     va_list argList;
  98.  
  99.     va_start(argList, format);
  100.     outputError(TRUE, errnum, TRUE, format, argList);
  101.     va_end(argList);
  102.  
  103.     terminate(TRUE);
  104.  
  105. }
  106.  
  107. void
  108. fatal(const char *format, ...)
  109. {
  110.     va_list argList;
  111.  
  112.     va_start(argList, format);
  113.     outputError(FALSE, 0, TRUE, format, argList);
  114.     va_end(argList);
  115.  
  116.     terminate(TRUE);
  117.  
  118. }
  119.  
  120. void
  121. usageErr(const char *format, ...)
  122. {
  123.     va_list argList;
  124.    
  125.     fflush(stdout);                 /* flush any pending stdout */
  126.  
  127.     fprintf(stderr, "Usage: ");
  128.     va_start(argList, format);
  129.     vfprintf(stderr, format, argList);
  130.     va_end(argList);
  131.  
  132.     fflush(stderr);                 /* in case stderr is not line-buffered */
  133.     exit(EXIT_FAILURE);
  134.  
  135. }
  136.  
  137. void
  138. cmdLineErr(const char *format, ...)
  139. {
  140.     va_list argList;
  141.    
  142.     fflush(stdout);                 /* flush any pending stdout */
  143.  
  144.     fprintf(stderr, "Command-line usage error: ");
  145.     va_start(argList, format);
  146.     vfprintf(stderr, format, argList);
  147.     va_end(argList);
  148.  
  149.     fflush(stderr);                 /* in case stderr is not line-buffered */
  150.     exit(EXIT_FAILURE);
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment