home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / server / fakesyslog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  3.0 KB  |  183 lines

  1. #ifndef lint
  2. static char    *sccsid = "@(#)$Header: fakesyslog.c,v 1.5 90/01/15 01:04:55 sob 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 "../common/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 FAKEAPPEND
  25. #include <sys/file.h>
  26. #endif
  27.  
  28. #ifdef FCNTL
  29. #include <fcntl.h>
  30. #endif
  31.  
  32. extern    int    errno;
  33. extern    int    sys_nerr;
  34. extern    char    *sys_errlist[];
  35.  
  36. static FILE    *logfp;
  37. static int    failed = 0;
  38. static char    *ident = "syslog";
  39. static int     opt = 0;
  40. #ifndef BSD_42
  41. static int    fac = 0;
  42. #endif
  43.  
  44. extern char    *strcpy(), *strcat(), *ctime();
  45. extern time_t    time();
  46.  
  47. resetlog()
  48. {
  49.     closelog();
  50.     failed = 0;
  51.     if (logfp == NULL) {
  52. #ifdef BSD_42
  53.         openlog(ident, opt);
  54. #else
  55.         openlog(ident, opt, fac);
  56. #endif
  57.         if (logfp == NULL) {
  58.             failed = 1;
  59.             return;
  60.         }
  61.     }
  62. }
  63.  
  64. #ifdef BSD_42
  65. openlog(newident,logopt)
  66.     char *newident;
  67.     int logopt;
  68. #else
  69. openlog(newident,logopt,facility)
  70.     char *newident;
  71.     int logopt, facility;
  72. #endif
  73. {
  74. #ifdef FAKEAPPEND
  75. /*
  76.  * why can't stdio give us the capability of O_APPEND?
  77.  */
  78.     int fd;
  79.  
  80.     fd = open(FAKESYSLOG, O_WRONLY|O_APPEND, 0664);
  81.     if (fd < 0)
  82.         logfp = NULL;
  83.     else
  84.         logfp = fdopen(fd, "a");
  85. #else
  86.     logfp = fopen(FAKESYSLOG, "a");
  87. #endif
  88.  
  89.     (void)signal(SIGHUP, resetlog);
  90.  
  91.     if (newident && *newident)
  92.         ident = newident;
  93.     opt = logopt;
  94. #ifndef BSD_42
  95.     fac = facility;
  96. #endif
  97. }
  98.  
  99. closelog()
  100. {
  101.     if (logfp) {
  102.         (void)fclose(logfp);
  103.         failed = 0;
  104.         logfp = NULL;
  105.     }
  106. }
  107.  
  108. /*ARGSUSED*/
  109. setlogmask(maskpri)
  110.     int maskpri;
  111. {
  112. }
  113.  
  114. syslog(pri, msg, x1, x2, x3, x4, x5, x6)
  115.     int    pri;
  116.     char    *msg, *x1, *x2, *x3, *x4, *x5, *x6;
  117. {
  118.     char        buf[1024];
  119.     char        *cp, *bp;
  120.     time_t        clock;
  121.  
  122.     if (failed)
  123.         return;
  124.  
  125.     if (logfp == NULL) {
  126. #ifdef BSD_42
  127.         openlog(ident, opt);
  128. #else
  129.         openlog(ident, opt, fac);
  130. #endif
  131.         if (logfp == NULL) {
  132.             failed = 1;
  133.             return;
  134.         }
  135.     }
  136.  
  137.     (void) time(&clock);
  138.     (void) strcpy(buf, ctime(&clock)+4);
  139.     *(bp = buf + 16) = '\0';
  140.  
  141.     (void) sprintf(bp, "localhost %s", ident ? ident : "");
  142.     bp += strlen(bp);
  143.  
  144.     if (opt&LOG_PID) {
  145.         /* don't cache getpid() - who knows when we'll fork() */
  146.         (void) sprintf(bp, "[%d]", getpid());
  147.         bp += strlen(bp);
  148.     }
  149.  
  150.     if (ident) {
  151.         (void) strcat(bp, ": ");
  152.         bp += 2;
  153.     } else {
  154.         (void) strcat(bp, " ");
  155.         bp ++;
  156.     }
  157.  
  158.     for (cp = msg; *cp; cp++) {
  159.         if (*cp == '%' && cp[1] == 'm') {
  160.             *bp = '\0';
  161.             if (errno >= sys_nerr || errno < 0) {
  162.                 char    work[32];
  163.                 (void)sprintf(work, "unknown error #%d", errno);
  164.                 (void)strcat(bp, work);
  165.             } else
  166.                 (void)strcat(bp, sys_errlist[errno]);
  167.             bp = buf + strlen(buf);
  168.             cp++;
  169.         } else {
  170.             *bp++ = *cp;
  171.         }
  172.     }
  173.     *bp = '\0';
  174.     /* Ah, the semantic security of C ... */
  175.     if (bp[-1] != '\n')
  176.         (void) strcat(bp, "\n");
  177.  
  178.     fprintf(logfp, buf, x1, x2, x3, x4, x5, x6);
  179.     (void) fflush(logfp);
  180. }
  181.  
  182. #endif
  183.