home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / slurp / part02 / fakesyslog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-12  |  3.1 KB  |  189 lines

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