home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume30 / log_tcp / part02 / percent_x.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-20  |  2.1 KB  |  77 lines

  1.  /*
  2.   * percent_x() takes a string and performs %a (host address), %c (client
  3.   * info), %h (host name or address), %d (daemon name), %p (process id) and
  4.   * %u (user name) substitutions. It aborts the program when the result of
  5.   * expansion would overflow the output buffer.
  6.   * 
  7.   * Diagnostics are reported through syslog(3).
  8.   * 
  9.   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  10.   */
  11.  
  12. #ifndef lint
  13. static char sccsid[] = "@(#) percent_x.c 1.1 92/06/11 22:21:49";
  14. #endif
  15.  
  16. /* System libraries. */
  17.  
  18. #include <stdio.h>
  19. #include <syslog.h>
  20.  
  21. extern char *strncpy();
  22. extern void exit();
  23.  
  24. /* Local stuff. */
  25.  
  26. #include "log_tcp.h"
  27.  
  28. /* percent_x - do %<char> expansion, abort if result buffer is too small */
  29.  
  30. void    percent_x(result, result_len, str, daemon, client, pid)
  31. char   *result;
  32. int     result_len;
  33. char   *str;
  34. char   *daemon;
  35. struct from_host *client;
  36. int     pid;
  37. {
  38.     char   *end = result + result_len - 1;    /* end of result buffer */
  39.     char   *expansion;
  40.     int     expansion_len;
  41.     char    pid_buf[10];
  42.  
  43.     /*
  44.      * %a becomes the client address; %c all user and host information we
  45.      * have about the client; %d the daemon process name; %h the client host
  46.      * name or address; %p the daemon process id; %u the remote user name; %%
  47.      * becomes a %, and %other is ignored. We terminate with a diagnostic if
  48.      * we would overflow the result buffer.
  49.      */
  50.  
  51.     while (*str) {
  52.     if (*str == '%') {
  53.         str++;
  54.         expansion =
  55.         *str == 'a' ? (str++, client->addr) :
  56.         *str == 'c' ? (str++, hosts_info(client)) :
  57.         *str == 'd' ? (str++, daemon) :
  58.         *str == 'h' ? (str++, FROM_HOST(client)) :
  59.         *str == 'p' ? (str++, sprintf(pid_buf, "%d", pid), pid_buf) :
  60.         *str == 'u' ? (str++, client->user) :
  61.         *str == '%' ? (str++, "%") :
  62.         *str == 0 ? "" : (str++, "");
  63.         expansion_len = strlen(expansion);
  64.     } else {
  65.         expansion = str++;
  66.         expansion_len = 1;
  67.     }
  68.     if (result + expansion_len >= end) {
  69.         syslog(LOG_ERR, "shell command too long: %30s...", result);
  70.         exit(0);
  71.     }
  72.     strncpy(result, expansion, expansion_len);
  73.     result += expansion_len;
  74.     }
  75.     *result = 0;
  76. }
  77.