home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / s / slurp103.zip / FAKESYSL.C < prev    next >
C/C++ Source or Header  |  1992-12-20  |  3KB  |  179 lines

  1. /*
  2.  * Fake syslog routines for systems that don't have syslog.
  3.  * Taken from an idea by Paul McKenny, <mckenny@sri-unix.arpa>.
  4.  * (Unfortunately, Paul, I can't distribute the real syslog code
  5.  * as you suggested ... sigh.)
  6.  *
  7.  * Warning: this file contains joe code that may offend you.
  8.  */
  9.  
  10. #include "slurp.h"
  11.  
  12. #ifdef FAKESYSLOG
  13.  
  14. #include "fakesyslog.h"
  15.  
  16. #include <stdio.h>
  17. #include <sys/signal.h>
  18. #include <sys/types.h>
  19.  
  20. #ifdef FAKEAPPEND
  21. #include <sys/file.h>
  22. #endif
  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. #ifndef BSD_42
  37. static int    fac = 0;
  38. #endif
  39.  
  40. extern char    *strcpy(), *strcat(), *ctime();
  41. extern time_t    time();
  42.  
  43. resetlog()
  44. {
  45.     closelog();
  46.     failed = 0;
  47.     if (logfp == NULL) {
  48. #ifdef BSD_42
  49.         openlog(ident, opt);
  50. #else
  51.         openlog(ident, opt, fac);
  52. #endif
  53.         if (logfp == NULL) {
  54.             failed = 1;
  55.             return;
  56.         }
  57.     }
  58. }
  59.  
  60. #ifdef BSD_42
  61. openlog(newident,logopt)
  62.     char *newident;
  63.     int logopt;
  64. #else
  65. openlog(newident,logopt,facility)
  66.     char *newident;
  67.     int logopt, facility;
  68. #endif
  69. {
  70. #ifdef FAKEAPPEND
  71. /*
  72.  * why can't stdio give us the capability of O_APPEND?
  73.  */
  74.     int fd;
  75.  
  76.     fd = open(FAKESYSLOG, O_WRONLY|O_APPEND, 0664);
  77.     if (fd < 0)
  78.         logfp = NULL;
  79.     else
  80.         logfp = fdopen(fd, "a");
  81. #else
  82.     logfp = fopen(FAKESYSLOG, "a");
  83. #endif
  84.  
  85.     (void)signal(SIGHUP, resetlog);
  86.  
  87.     if (newident && *newident)
  88.         ident = newident;
  89.     opt = logopt;
  90. #ifndef BSD_42
  91.     fac = facility;
  92. #endif
  93. }
  94.  
  95. closelog()
  96. {
  97.     if (logfp) {
  98.         (void)fclose(logfp);
  99.         failed = 0;
  100.         logfp = NULL;
  101.     }
  102. }
  103.  
  104. /*ARGSUSED*/
  105. setlogmask(maskpri)
  106.     int maskpri;
  107. {
  108. }
  109.  
  110. syslog(pri, msg, x1, x2, x3, x4, x5, x6)
  111.     int    pri;
  112.     char    *msg, *x1, *x2, *x3, *x4, *x5, *x6;
  113. {
  114.     char        buf[1024];
  115.     char        *cp, *bp;
  116.     time_t        clock;
  117.  
  118.     if (failed)
  119.         return;
  120.  
  121.     if (logfp == NULL) {
  122. #ifdef BSD_42
  123.         openlog(ident, opt);
  124. #else
  125.         openlog(ident, opt, fac);
  126. #endif
  127.         if (logfp == NULL) {
  128.             failed = 1;
  129.             return;
  130.         }
  131.     }
  132.  
  133.     (void) time(&clock);
  134.     (void) strcpy(buf, ctime(&clock)+4);
  135.     *(bp = buf + 16) = '\0';
  136.  
  137.     (void) sprintf(bp, "localhost %s", ident ? ident : "");
  138.     bp += strlen(bp);
  139.  
  140.     if (opt&LOG_PID) {
  141.         /* don't cache getpid() - who knows when we'll fork() */
  142.         (void) sprintf(bp, "[%d]", getpid());
  143.         bp += strlen(bp);
  144.     }
  145.  
  146.     if (ident) {
  147.         (void) strcat(bp, ": ");
  148.         bp += 2;
  149.     } else {
  150.         (void) strcat(bp, " ");
  151.         bp ++;
  152.     }
  153.  
  154.     for (cp = msg; *cp; cp++) {
  155.         if (*cp == '%' && cp[1] == 'm') {
  156.             *bp = '\0';
  157.             if (errno >= sys_nerr || errno < 0) {
  158.                 char    work[32];
  159.                 (void)sprintf(work, "unknown error #%d", errno);
  160.                 (void)strcat(bp, work);
  161.             } else
  162.                 (void)strcat(bp, sys_errlist[errno]);
  163.             bp = buf + strlen(buf);
  164.             cp++;
  165.         } else {
  166.             *bp++ = *cp;
  167.         }
  168.     }
  169.     *bp = '\0';
  170.     /* Ah, the semantic security of C ... */
  171.     if (bp[-1] != '\n')
  172.         (void) strcat(bp, "\n");
  173.  
  174.     fprintf(logfp, buf, x1, x2, x3, x4, x5, x6);
  175.     (void) fflush(logfp);
  176. }
  177.  
  178. #endif
  179.