home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / syslog.zip / logger.c < prev    next >
Text File  |  1994-11-28  |  4KB  |  178 lines

  1. /*  $Revision: 1.4 $
  2. **  Modified by Rich $alz <rsalz@osf.org> to be more portable to older
  3. **  systems.
  4. */
  5. /*
  6.  * Copyright (c) 1983 Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms are permitted
  10.  * provided that: (1) source distributions retain this entire copyright
  11.  * notice and comment, and (2) distributions including binaries display
  12.  * the following acknowledgement:  ``This product includes software
  13.  * developed by the University of California, Berkeley and its contributors''
  14.  * in the documentation or other materials provided with the distribution
  15.  * and in all advertising materials mentioning features or use of this
  16.  * software. Neither the name of the University nor the names of its
  17.  * contributors may be used to endorse or promote products derived
  18.  * from this software without specific prior written permission.
  19.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. #include <utils.h>
  28. #include <ctype.h>
  29. #define SYSLOG_NAMES
  30. #include "syslog.h"
  31. #include "getopt.h"
  32.  
  33. int pencode(char *);
  34. char *getlogin(void);
  35. int decode(char *, CODE *);
  36. void bailout(char *, char *);
  37. void usage(void);
  38.  
  39. /*
  40. **  LOGGER -- read and log utility
  41. **
  42. **    This routine reads from an input and arranges to write the
  43. **    result on the system log, along with a useful tag.
  44. */
  45.  
  46. char *getlogin(void)
  47. {
  48.     char *log;
  49.         log=getenv("LOGNAME");
  50.         if (log==NULL) log="root";
  51.         return log;
  52. }
  53.  
  54. main(int argc, char **argv)
  55. {
  56.     int pri = LOG_NOTICE;
  57.     int ch, logflags = 0;
  58.     char *tag, buf[1024];
  59.  
  60.     tag = NULL;
  61.     while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
  62.         switch((char)ch) {
  63.         case 'f':        /* file to log */
  64.             if (freopen(optarg, "r", stdin) == NULL) {
  65.                 (void)fprintf(stderr, "logger: %s: %s.\n",
  66.                     optarg, strerror(errno));
  67.                 exit(1);
  68.             }
  69.             break;
  70.         case 'i':        /* log process id also */
  71.             logflags |= LOG_PID;
  72.             break;
  73.         case 'p':        /* priority */
  74.             pri = pencode(optarg);
  75.             break;
  76.         case 's':        /* log to standard error */
  77.             logflags |= LOG_PERROR;
  78.             break;
  79.         case 't':        /* tag */
  80.             tag = optarg;
  81.             break;
  82.         case '?':
  83.         default:
  84.             usage();
  85.         }
  86.     argc -= optind;
  87.     argv += optind;
  88.  
  89.     /* setup for logging */
  90.     openlog(tag ? tag : getlogin(), logflags, 0);
  91.     (void) fclose(stdout);
  92.  
  93.     /* log input line if appropriate */
  94.     if (argc > 0) {
  95.         register char *p, *endp;
  96.         int len;
  97.  
  98.         for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
  99.             len = strlen(*argv);
  100.             if (p + len > endp && p > buf) {
  101.                 syslog(pri, "%s", buf);
  102.                 p = buf;
  103.             }
  104.             if (len > sizeof(buf) - 1)
  105.                 syslog(pri, "%s", *argv++);
  106.             else {
  107.                 if (p != buf)
  108.                     *p++ = ' ';
  109.                 bcopy(*argv++, p, len);
  110.                 *(p += len) = '\0';
  111.             }
  112.         }
  113.         if (p != buf)
  114.             syslog(pri, "%s", buf);
  115.         exit(0);
  116.     }
  117.  
  118.     /* main loop */
  119.     while (fgets(buf, sizeof(buf), stdin) != NULL)
  120.         syslog(pri, "%s", buf);
  121.  
  122.     exit(0);
  123. }
  124.  
  125.  
  126. /*
  127.  *  Decode a symbolic name to a numeric value
  128.  */
  129. int pencode(char *s)
  130. {
  131.     char *save;
  132.     int fac, lev;
  133.  
  134.     for (save = s; *s && *s != '.'; ++s);
  135.     if (*s) {
  136.         *s = '\0';
  137.         fac = decode(save, facilitynames);
  138.         if (fac < 0)
  139.             bailout("unknown facility name: ", save);
  140.         *s++ = '.';
  141.     }
  142.     else {
  143.         fac = 0;
  144.         s = save;
  145.     }
  146.     lev = decode(s, prioritynames);
  147.     if (lev < 0)
  148.         bailout("unknown priority name: ", save);
  149.     return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
  150. }
  151.  
  152. int decode(char *name, CODE *codetab)
  153. {
  154.     register CODE *c;
  155.  
  156.     if (isdigit(*name))
  157.         return (atoi(name));
  158.  
  159.     for (c = codetab; c->c_name; c++)
  160.         if (!strcmpi(name, c->c_name))
  161.             return (c->c_val);
  162.  
  163.     return (-1);
  164. }
  165.  
  166. void bailout(char *msg, char *arg)
  167. {
  168.     fprintf(stderr, "logger: %s%s\n", msg, arg);
  169.     exit(1);
  170. }
  171.  
  172. void usage(void)
  173. {
  174.     fputs("logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n",
  175.         stderr);
  176.     exit(1);
  177. }
  178.