home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-A.06 / NETKIT-A / NetKit-A-0.06 / tcp_wrapper-6.3 / fakelog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-27  |  2.2 KB  |  111 lines

  1.  /*
  2.   * This module intercepts syslog() library calls and redirects their output
  3.   * to the standard error stream. For interactive testing. Not for critical
  4.   * applications, because it will happily write beyond the end of buffers.
  5.   * 
  6.   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  7.   */
  8.  
  9. #ifndef lint
  10. static char sccsid[] = "@(#) fakelog.c 1.2 93/09/25 12:56:20";
  11. #endif
  12.  
  13. #include <stdio.h>
  14.  
  15.  /*
  16.   * What follows is an attempt to unify varargs.h and stdarg.h. I'd rather
  17.   * have this than #ifdefs all over the code.
  18.   */
  19.  
  20. #ifdef __STDC__
  21. #include <stdarg.h>
  22. #define VARARGS(func,type,arg) func(type arg, ...)
  23. #define VASTART(ap,type,name)  va_start(ap,name)
  24. #define VAEND(ap)              va_end(ap)
  25. #else
  26. #include <varargs.h>
  27. #define VARARGS(func,type,arg) func(va_alist) va_dcl
  28. #define VASTART(ap,type,name)  {type name; va_start(ap); name = va_arg(ap, type)
  29. #define VAEND(ap)              va_end(ap);}
  30. #endif
  31.  
  32. extern int errno;
  33. extern char *sys_errlist[];
  34. extern int sys_nerr;
  35. extern char *strcpy();
  36.  
  37. /* percentm - replace %m by error message associated with value in err */
  38.  
  39. char   *percentm(buf, str, err)
  40. char   *buf;
  41. char   *str;
  42. int     err;
  43. {
  44.     char   *ip = str;
  45.     char   *op = buf;
  46.  
  47.     while (*ip) {
  48.     switch (*ip) {
  49.     case '%':
  50.         switch (ip[1]) {
  51.         case '\0':                /* don't fall off end */
  52.         *op++ = *ip++;
  53.         break;
  54.         case 'm':                /* replace %m */
  55.         if (err < sys_nerr && err > 0)
  56.             strcpy(op, sys_errlist[err]);
  57.         else
  58.             sprintf(op, "Unknown error %d", err);
  59.         op += strlen(op);
  60.         ip += 2;
  61.         break;
  62.         default:                /* leave %<any> alone */
  63.         *op++ = *ip++, *op++ = *ip++;
  64.         break;
  65.         }
  66.     default:
  67.         *op++ = *ip++;
  68.     }
  69.     }
  70.     *op = 0;
  71.     return (buf);
  72. }
  73.  
  74. /* openlog - dummy */
  75.  
  76. /* ARGSUSED */
  77.  
  78. openlog(name, logopt, facility)
  79. char   *name;
  80. int     logopt;
  81. int     facility;
  82. {
  83.     /* void */
  84. }
  85.  
  86. /* syslog - append record to system log -- not */
  87.  
  88. /* VARARGS */
  89.  
  90. VARARGS(syslog, int, severity)
  91. {
  92.     va_list ap;
  93.     char   *fmt;
  94.     int     err = errno;
  95.     char    buf[BUFSIZ];
  96.  
  97.     VASTART(ap, int, severity);
  98.     fmt = va_arg(ap, char *);
  99.     fprintf(stderr, " ");
  100.     vfprintf(stderr, percentm(buf, fmt, err), ap);
  101.     fprintf(stderr, "\n");
  102.     VAEND(ap);
  103. }
  104.  
  105. /* closelog - dummy */
  106.  
  107. closelog()
  108. {
  109.     /* void */
  110. }
  111.