home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / YP-CLIEN.TAR / yp.bin / error.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-12  |  6.0 KB  |  321 lines

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