home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / src / gopher / gopher1.01 / mindexd / error.c < prev    next >
C/C++ Source or Header  |  1992-04-08  |  6KB  |  372 lines

  1. /*
  2.  * Error handling routines from Stevens: UNIX network programming
  3.  * pp 722-731
  4.  */
  5.  
  6. /*
  7.  * Error handling routines.
  8.  *
  9.  * The functions in this file are independent of any application
  10.  * variables, and may be used in any C program.  Either of the names
  11.  * CLIENT of SERVER may be defined when compiling this function.
  12.  * If neither are defined, we assume SERVER.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <varargs.h>
  17.  
  18. #ifdef CLIENT
  19. #ifdef SERVER
  20. /*no way jose, can't define both! (CLIENT and SERVER)*/
  21. #endif
  22. #endif
  23.  
  24. #ifndef CLIENT
  25. #ifndef SERVER
  26. #define CLIENT 1
  27. #endif
  28. #endif
  29.  
  30. #ifndef NULL
  31. #define NULL ((void *) 0)
  32. #endif
  33.  
  34. char *pname = NULL;
  35.  
  36. #ifdef CLIENT  /* these all output to stderr */
  37.  
  38. /*
  39.  * Fatal error.  Print a message and terminate.
  40.  * Don't dump core and don't print the system's errno value.
  41.  *
  42.  *     err_quit(str, arg1, arg2, .....)
  43.  *
  44.  * The string "str" must specify the conversion specification for any args.
  45.  *
  46.  */
  47.  
  48. /*VARARGS1*/
  49. err_quit(va_alist)
  50. va_dcl
  51. {
  52.     va_list args;
  53.     char *fmt;
  54.  
  55.     va_start(args);
  56.     if (pname != NULL)
  57.         fprintf(stderr, "%s: ",pname);
  58.     fmt = va_arg(args, char *);
  59.     printf(stderr, fmt, args);
  60.     fputc('\n', stderr);
  61.     va_end(args);
  62.  
  63.     exit(1);
  64. }
  65.  
  66. /*
  67.  * Fatal error related to a system call. Print the message and terminate.
  68.  * Don't dump core, but do print the systems errno value and its
  69.  * associated message.
  70.  *
  71.  *    err_sys(str, arg1, arg2, ...)
  72.  *
  73.  */
  74.  
  75. /*VARARGS1*/
  76. err_sys(va_alist)
  77. va_dcl
  78. {
  79.     va_list args;
  80.     char *fmt;
  81.  
  82.     va_start(args);
  83.     if (pname != NULL)
  84.         fprintf(stderr, "%s: ", pname);
  85.     fmt = va_arg(args, char *);
  86.     printf(stderr, fmt, args);
  87.     va_end(args);
  88.  
  89.     my_perror();
  90.  
  91.     exit(1);
  92. }
  93.  
  94.  
  95. /*
  96.  * Recoverable error. print a message and return to the caller.
  97.  *
  98.  *     err_ret(str, arg1, arg2, ...)
  99.  */
  100.  
  101. /*VARARGS1*/
  102. err_ret(va_alist)
  103. va_dcl
  104. {
  105.     va_list    args;
  106.     char    *fmt;
  107.  
  108.     va_start(args);
  109.     if (pname != NULL)
  110.         fprintf(stderr, "%s: ", pname);
  111.     fmt = va_arg(args, char *);
  112.     printf(stderr, fmt, args);
  113.     va_end(args);
  114.  
  115.     my_perror();
  116.  
  117.     fflush(stdout);
  118.     fflush(stderr);
  119.     return;
  120. }
  121.  
  122.  
  123. /*
  124.  * Fatal error.  Print a message, dump core and terminate.
  125.  *
  126.  *    err_dump(str, arg1, arg2, ....)
  127.  *
  128.  */
  129.  
  130. /*VARARGS1*/
  131. err_dump(va_alist)
  132. va_dcl
  133. {
  134.     va_list args;
  135.     char *fmt;
  136.  
  137.     va_start(args);
  138.     if (pname != NULL)
  139.         fprintf(stderr, "%s: ", pname);
  140.     fmt = va_arg(args, char *);
  141.     printf(stderr, fmt, args);
  142.     va_end(args);
  143.  
  144.     my_perror();
  145.  
  146.     fflush(stdout);        /* abort doesn't flush stdio buffers */
  147.     fflush(stderr);
  148.  
  149.     abort();        /* dump core and terminate */
  150.     exit(1);
  151. }
  152.  
  153. /*
  154.  * Print the UNIX errno value
  155.  */
  156.  
  157.  
  158. my_perror()
  159. {
  160.     char *sys_err_str();
  161.  
  162.     fprintf(stderr, " %s\n", sys_err_str());
  163. }
  164.  
  165. #endif /* CLIENT */
  166.  
  167. #ifdef SERVER
  168.  
  169. #ifdef BSD
  170.  /*
  171.   * Under BSD, these server routines use the syslog(3) facility.
  172.   * They don't append a newline for example.
  173.   */
  174.  
  175. #include <syslog.h>
  176.  
  177.  
  178. #else /* not BSD */
  179. /*
  180.  * There really ought to be a better way to handle server logging
  181.  * under System V
  182.  */
  183.  
  184. #define syslog(a,b)    fprintf(stderr, "%s\n", (b))
  185. #define openlog(a,b,c)    fprintf(stderr, "%s\n", (a))
  186.  
  187. #endif  /* BSD */
  188.  
  189. char emesgst[255] = {0};  /* used by all server routines */
  190.  
  191. /*
  192.  * Identify ourselves, for syslog() messages.
  193.  *
  194.  * LOG_PID is an option that says prepend each message with our pid.
  195.  * LOG_CONS is an option that says write to the console if unable to
  196.  * send the message to syslogd.
  197.  * LOG_DAEMON is our facility.
  198.  */
  199.  
  200. err_init(ident)
  201. char *ident);
  202. {
  203.     openlog(ident, (LOG_PID|LOG_CONS), LOG_DAEMON);
  204. }
  205.  
  206. /*
  207.  * Fatal error.  Print a message and terminate.
  208.  * Don't print the system's errno value.
  209.  *
  210.  *    err_quit(str, arg1, arg2, ...)
  211.  */
  212.  
  213. /*VARARGS1*/
  214. err_quit(va_alist)
  215. va_dcl
  216. {
  217.     va_list args;
  218.     char *fmt;
  219.  
  220.     va_start(args);
  221.     fmt = va_arg(args, char *);
  222.     vsprintf(emesgstr, fmt, args);
  223.     va_end(args);
  224.  
  225.     syslog(LOG_ERR, emesgstr);
  226.  
  227.     exit(1)
  228. }
  229.  
  230.  
  231. /*
  232.  * Fatal error related to a system call.  Print the message and terminate.
  233.  * Don't dump core, but do print the systems errno value and its associated
  234.  * message.
  235.  */
  236.  
  237. /*VARARGS*/
  238. err_sys(va_alist)
  239. va_dcl
  240. {
  241.     va_list args;
  242.     char *fmt;
  243.  
  244.     va_start(args);
  245.     fmt = va_arg(args, char *);
  246.     vsprintf(emesgstr, fmt, args);
  247.     va_end(args);
  248.  
  249.     my_perror();
  250.     syslog(LOG_ERR, emesgstr);
  251.  
  252.     exit(1);
  253. }
  254.  
  255.  
  256. /*
  257.  * Recoverable error.  Print a message, and return to caller.
  258.  */
  259.  
  260. /*VARARGS1*/
  261. err_ret(va_alist)
  262. va_dcl
  263. {
  264.     va_alist args;
  265.     char *fmt;
  266.  
  267.     va_start(args);
  268.     fmt = va_arg(args, char *);
  269.     vsprintf(emergstr, fmt, args);
  270.     va_end(args);
  271.  
  272.     my_perror();
  273.     syslog(LOG_ERR, emergstr);
  274.  
  275.     return;
  276. }
  277.  
  278.  
  279. /*
  280.  * Fatal error.  Print a message, dump core and terminate.
  281.  */
  282.  
  283. /*VARARGS1*/
  284. err_dump(va_alist)
  285. va_dcl
  286. {
  287.     va_list args;
  288.     char *fmt;
  289.  
  290.     va_start(args);
  291.     fmt = va_arg(args, char *);
  292.     vsprintf(emesgstr, fmt, args);
  293.     va_end(args);
  294.  
  295.     my_perror();
  296.     syslog(LOG_ERR, emesgstr);
  297.  
  298.     abort();
  299.     exit(1);
  300. }
  301.  
  302. /*
  303.  * Print the UNIX errno value.
  304.  * We just append it the the end of the emesgstr[] array.
  305.  */
  306.  
  307. void
  308. my_perror()
  309. {
  310.     int len;
  311.     char *sys_err_str();
  312.  
  313.     len = strlen(emesgstr);
  314.     sprintf(emesgstr + len, " %s", sys_err_str());
  315. }
  316.  
  317. #endif  /* SERVER */
  318.  
  319.  
  320. extern int errno;        /* UNIX error number */
  321. extern int sys_nerr;        /* # of error message strings in sys table */
  322. extern char *sys_errlist[];    /* the system error message table */
  323.  
  324. #ifdef SYS5
  325. int t_errno;
  326. int t_nerr;
  327. char *t_errlist[1];
  328. #endif
  329.  
  330. /*
  331.  * Return a string containing some additional operating system
  332.  * dependent information.
  333.  * Note that different versions of UNIX assign different meanings
  334.  * to the same value of "errno".  This means that if an error
  335.  * condition is being sent ot another UNIX system, we must interpret
  336.  * the errno value on the system that generated the error, and not
  337.  * just send the decimal value of errno to the other system.
  338.  */
  339.  
  340. char *
  341. sys_err_str()
  342. {
  343.     static char msgstr[200];
  344.  
  345.     if (errno != 0) {
  346.         if (errno >0 && errno < sys_nerr)
  347.             sprintf(msgstr, "(%s)", sys_errlist[errno]);
  348.         else
  349.             sprintf(msgstr, "(errno = %d)", errno);
  350.     } else {
  351.         msgstr[0] = '\0';
  352.     }
  353.  
  354. #ifdef SYS5
  355.  
  356.     if (t_errno != 0) {
  357.         char tmsgstr[100];
  358.  
  359.         if (t_errno > 0 && t_errno < sys_nerr)
  360.             sprintf(tmsgstr, " (%s)", t_errlist[t_errno]);
  361.         else
  362.             sprintf(tmsgstr, ", (t_errno = %d)", t_errno);
  363.  
  364.         strcat(msgstr, tmsgstr);
  365.     }
  366. #endif
  367.  
  368.     return(msgstr);
  369. }
  370.  
  371.  
  372.