home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume26 / tulp-3.0.3 / part01 / fakesyslog.c < prev    next >
C/C++ Source or Header  |  1993-04-15  |  3KB  |  150 lines

  1. #ifndef lint
  2. static char    *sccsid = "@(#)$Id: fakesyslog.c,v 1.3 92/05/23 14:02:01 kim Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Fake syslog routines for systems that don't have syslog.
  7.  * Taken from an idea by Paul McKenny, <mckenny@sri-unix.arpa>.
  8.  * (Unfortunately, Paul, I can't distribute the real syslog code
  9.  * as you suggested ... sigh.)
  10.  *
  11.  * Warning: this file contains joe code that may offend you.
  12.  */
  13.  
  14. #include "conf.h"
  15.  
  16. #ifdef FAKESYSLOG
  17.  
  18. #include "fakesyslog.h"
  19.  
  20. #include <stdio.h>
  21. #include <sys/signal.h>
  22. #include <sys/types.h>
  23.  
  24. #ifdef FCNTL
  25. #include <fcntl.h>
  26. #endif
  27.  
  28. extern    int    errno;
  29. extern    int    sys_nerr;
  30. extern    char    *sys_errlist[];
  31.  
  32. static FILE    *logfp;
  33. static int    failed = 0;
  34. static char    *ident = "syslog";
  35. static int     opt = 0;
  36. static int    fac = 0;
  37.  
  38. extern char    *strcpy(), *strcat(), *ctime();
  39. extern time_t    time();
  40.  
  41. /* ARGSUSED */
  42. resetlog(notused)
  43.      int notused;
  44. {
  45.     closelog();
  46.     failed = 0;
  47.     if (logfp == NULL) {
  48.         openlog(ident, opt, fac);
  49.         if (logfp == NULL) {
  50.             failed = 1;
  51.             return;
  52.         }
  53.     }
  54. }
  55.  
  56. openlog(newident,logopt,facility)
  57.     char *newident;
  58.     int logopt, facility;
  59. {
  60.     logfp = fopen(FAKESYSLOG, "a");
  61.  
  62.     (void)signal(SIGHUP, resetlog);
  63.  
  64.     if (newident && *newident)
  65.         ident = newident;
  66.     opt = logopt;
  67.     fac = facility;
  68. }
  69.  
  70. closelog()
  71. {
  72.     if (logfp) {
  73.         (void)fclose(logfp);
  74.         failed = 0;
  75.         logfp = NULL;
  76.     }
  77. }
  78.  
  79. /*ARGSUSED*/
  80. setlogmask(maskpri)
  81.     int maskpri;
  82. {
  83. }
  84.  
  85. syslog(pri, msg, x1, x2, x3, x4, x5, x6)
  86.     int    pri;
  87.     char    *msg, *x1, *x2, *x3, *x4, *x5, *x6;
  88. {
  89.     char        buf[1024];
  90.     char        *cp, *bp;
  91.     time_t        clock;
  92.  
  93.     if (failed)
  94.         return;
  95.  
  96.     if (logfp == NULL) {
  97.         openlog(ident, opt, fac);
  98.         if (logfp == NULL) {
  99.             failed = 1;
  100.             return;
  101.         }
  102.     }
  103.  
  104.     (void) time(&clock);
  105.     (void) strcpy(buf, ctime(&clock)+4);
  106.     *(bp = buf + 16) = '\0';
  107.  
  108.     (void) sprintf(bp, "localhost %s", ident ? ident : "");
  109.     bp += strlen(bp);
  110.  
  111.     if (opt&LOG_PID) {
  112.         /* don't cache getpid() - who knows when we'll fork() */
  113.         (void) sprintf(bp, "[%d]", getpid());
  114.         bp += strlen(bp);
  115.     }
  116.  
  117.     if (ident) {
  118.         (void) strcat(bp, ": ");
  119.         bp += 2;
  120.     } else {
  121.         (void) strcat(bp, " ");
  122.         bp ++;
  123.     }
  124.  
  125.     for (cp = msg; *cp; cp++) {
  126.         if (*cp == '%' && cp[1] == 'm') {
  127.             *bp = '\0';
  128.             if (errno >= sys_nerr || errno < 0) {
  129.                 char    work[32];
  130.                 (void)sprintf(work, "unknown error #%d", errno);
  131.                 (void)strcat(bp, work);
  132.             } else
  133.                 (void)strcat(bp, sys_errlist[errno]);
  134.             bp = buf + strlen(buf);
  135.             cp++;
  136.         } else {
  137.             *bp++ = *cp;
  138.         }
  139.     }
  140.     *bp = '\0';
  141.     /* Ah, the semantic security of C ... */
  142.     if (bp[-1] != '\n')
  143.         (void) strcat(bp, "\n");
  144.  
  145.     fprintf(logfp, buf, x1, x2, x3, x4, x5, x6);
  146.     (void) fflush(logfp);
  147. }
  148.  
  149. #endif
  150.