1: /*
   2:  * Copyright (c) 1983 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if !defined(lint) && defined(DOSCCS)
   8: char copyright[] =
   9: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  10:  All rights reserved.\n";
  11: 
  12: static char sccsid[] = "@(#)logger.c	6.2.1 (2.11BSD) 1997/10/2";
  13: #endif
  14: 
  15: #include <stdio.h>
  16: #include <syslog.h>
  17: #include <unistd.h>
  18: #include <ctype.h>
  19: 
  20: /*
  21: **  LOGGER -- read and log utility
  22: **
  23: **	This routine reads from an input and arranges to write the
  24: **	result on the system log, along with a useful tag.
  25: */
  26: 
  27: main(argc, argv)
  28:     int argc;
  29:     char **argv;
  30: {
  31:     char buf[200];
  32:     char *tag;
  33:     register char *p;
  34:     int pri = LOG_NOTICE;
  35:     int logflags = 0;
  36: 
  37:     /* initialize */
  38:     tag = getlogin();
  39: 
  40:     /* crack arguments */
  41:     while (--argc > 0)
  42:     {
  43:         p = *++argv;
  44:         if (*p != '-')
  45:             break;
  46: 
  47:         switch (*++p)
  48:         {
  49:           case '\0':        /* dummy */
  50:             /* this can be used to give null parameters */
  51:             break;
  52: 
  53:           case 't':     /* tag */
  54:             if (argc > 1 && argv[1][0] != '-')
  55:             {
  56:                 argc--;
  57:                 tag = *++argv;
  58:             }
  59:             else
  60:                 tag = NULL;
  61:             break;
  62: 
  63:           case 'p':     /* priority */
  64:             if (argc > 1 && argv[1][0] != '-')
  65:             {
  66:                 argc--;
  67:                 pri = pencode(*++argv);
  68:             }
  69:             break;
  70: 
  71:           case 'i':     /* log process id also */
  72:             logflags |= LOG_PID;
  73:             break;
  74: 
  75:           case 'f':     /* file to log */
  76:             if (argc > 1 && argv[1][0] != '-')
  77:             {
  78:                 argc--;
  79:                 if (freopen(*++argv, "r", stdin) == NULL)
  80:                 {
  81:                     fprintf("logger: ");
  82:                     perror(*argv);
  83:                     exit(1);
  84:                 }
  85:             }
  86:             break;
  87: 
  88:           default:
  89:             fprintf(stderr, "logger: unknown flag -%s\n", p);
  90:             break;
  91:         }
  92:     }
  93: 
  94:     /* setup for logging */
  95:     openlog(tag, logflags, 0);
  96:     (void) fclose(stdout);
  97: 
  98:     /* log input line if appropriate */
  99:     if (argc > 0)
 100:     {
 101:         char buf[120];
 102: 
 103:         buf[0] = '\0';
 104:         while (argc-- > 0)
 105:         {
 106:             strcat(buf, " ");
 107:             strcat(buf, *argv++);
 108:         }
 109:         syslog(pri, buf + 1);
 110:         exit(0);
 111:     }
 112: 
 113:     /* main loop */
 114:     while (fgets(buf, sizeof buf, stdin) != NULL)
 115:         syslog(pri, buf);
 116: 
 117:     exit(0);
 118: }
 119: 
 120: 
 121: struct code {
 122:     char    *c_name;
 123:     int c_val;
 124: };
 125: 
 126: struct code PriNames[] = {
 127:     "panic",    LOG_EMERG,
 128:     "emerg",    LOG_EMERG,
 129:     "alert",    LOG_ALERT,
 130:     "crit",     LOG_CRIT,
 131:     "err",      LOG_ERR,
 132:     "error",    LOG_ERR,
 133:     "warn",     LOG_WARNING,
 134:     "warning",  LOG_WARNING,
 135:     "notice",   LOG_NOTICE,
 136:     "info",     LOG_INFO,
 137:     "debug",    LOG_DEBUG,
 138:     NULL,       -1
 139: };
 140: 
 141: struct code FacNames[] = {
 142:     "kern",     LOG_KERN,
 143:     "user",     LOG_USER,
 144:     "mail",     LOG_MAIL,
 145:     "daemon",   LOG_DAEMON,
 146:     "auth",     LOG_AUTH,
 147:     "security", LOG_AUTH,
 148:     "local0",   LOG_LOCAL0,
 149:     "local1",   LOG_LOCAL1,
 150:     "local2",   LOG_LOCAL2,
 151:     "local3",   LOG_LOCAL3,
 152:     "local4",   LOG_LOCAL4,
 153:     "local5",   LOG_LOCAL5,
 154:     "local6",   LOG_LOCAL6,
 155:     "local7",   LOG_LOCAL7,
 156:     NULL,       -1
 157: };
 158: 
 159: 
 160: /*
 161:  *  Decode a symbolic name to a numeric value
 162:  */
 163: 
 164: pencode(s)
 165:     register char *s;
 166: {
 167:     register char *p;
 168:     int lev;
 169:     int fac;
 170:     char buf[100];
 171: 
 172:     for (p = buf; *s && *s != '.'; )
 173:         *p++ = *s++;
 174:     *p = '\0';
 175:     if (*s++) {
 176:         fac = decode(buf, FacNames);
 177:         if (fac < 0)
 178:             bailout("unknown facility name: ", buf);
 179:         for (p = buf; *p++ = *s++; )
 180:             continue;
 181:     } else
 182:         fac = 0;
 183:     lev = decode(buf, PriNames);
 184:     if (lev < 0)
 185:         bailout("unknown priority name: ", buf);
 186: 
 187:     return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
 188: }
 189: 
 190: 
 191: decode(name, codetab)
 192:     char *name;
 193:     struct code *codetab;
 194: {
 195:     register struct code *c;
 196:     register char *p;
 197:     char buf[40];
 198: 
 199:     if (isdigit(*name))
 200:         return (atoi(name));
 201: 
 202:     (void) strcpy(buf, name);
 203:     for (p = buf; *p; p++)
 204:         if (isupper(*p))
 205:             *p = tolower(*p);
 206:     for (c = codetab; c->c_name; c++)
 207:         if (!strcmp(buf, c->c_name))
 208:             return (c->c_val);
 209: 
 210:     return (-1);
 211: }
 212: 
 213: bailout(a, b)
 214:     char *a, *b;
 215: {
 216:     fprintf(stderr, "logger: %s%s\n", a, b);
 217:     exit(1);
 218: }

Defined functions

bailout defined in line 213; used 2 times
decode defined in line 191; used 2 times
main defined in line 27; never used
pencode defined in line 164; used 1 times
  • in line 67

Defined variables

FacNames defined in line 141; used 1 times
PriNames defined in line 126; used 1 times
copyright defined in line 8; never used
sccsid defined in line 12; never used

Defined struct's

code defined in line 121; used 8 times
Last modified: 1997-10-03
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2843
Valid CSS Valid XHTML 1.0 Strict