Annotation of 43BSDTahoe/ucb/logger.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1983 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that the above copyright notice and this paragraph are
        !             7:  * duplicated in all such forms and that any documentation,
        !             8:  * advertising materials, and other materials related to such
        !             9:  * distribution and use acknowledge that the software was developed
        !            10:  * by the University of California, Berkeley.  The name of the
        !            11:  * University may not be used to endorse or promote products derived
        !            12:  * from this software without specific prior written permission.
        !            13:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            14:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            15:  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            16:  */
        !            17: 
        !            18: #ifndef lint
        !            19: char copyright[] =
        !            20: "@(#) Copyright (c) 1983 Regents of the University of California.\n\
        !            21:  All rights reserved.\n";
        !            22: #endif /* not lint */
        !            23: 
        !            24: #ifndef lint
        !            25: static char sccsid[] = "@(#)logger.c   6.8 (Berkeley) 6/29/88";
        !            26: #endif /* not lint */
        !            27: 
        !            28: #include <stdio.h>
        !            29: #include <syslog.h>
        !            30: #include <ctype.h>
        !            31: 
        !            32: /*
        !            33: **  LOGGER -- read and log utility
        !            34: **
        !            35: **     This routine reads from an input and arranges to write the
        !            36: **     result on the system log, along with a useful tag.
        !            37: */
        !            38: 
        !            39: main(argc, argv)
        !            40:        int argc;
        !            41:        char **argv;
        !            42: {
        !            43:        extern char *optarg;
        !            44:        extern int optind;
        !            45:        int pri = LOG_NOTICE;
        !            46:        int ch, logflags = 0;
        !            47:        char *tag, buf[200], *getlogin();
        !            48: 
        !            49:        tag = NULL;
        !            50:        while ((ch = getopt(argc, argv, "f:ip:t:")) != EOF)
        !            51:                switch((char)ch) {
        !            52:                case 'f':               /* file to log */
        !            53:                        if (freopen(optarg, "r", stdin) == NULL) {
        !            54:                                fprintf("logger: ");
        !            55:                                perror(optarg);
        !            56:                                exit(1);
        !            57:                        }
        !            58:                        break;
        !            59:                case 'i':               /* log process id also */
        !            60:                        logflags |= LOG_PID;
        !            61:                        break;
        !            62:                case 'p':               /* priority */
        !            63:                        pri = pencode(optarg);
        !            64:                        break;
        !            65:                case 't':               /* tag */
        !            66:                        tag = optarg;
        !            67:                        break;
        !            68:                case '?':
        !            69:                default:
        !            70:                        usage();
        !            71:                }
        !            72:        argc -= optind;
        !            73:        argv += optind;
        !            74: 
        !            75:        /* setup for logging */
        !            76:        openlog(tag ? tag : getlogin(), logflags, 0);
        !            77:        (void) fclose(stdout);
        !            78: 
        !            79:        /* log input line if appropriate */
        !            80:        if (argc > 0) {
        !            81:                register char *p, *endp;
        !            82:                int len;
        !            83: 
        !            84:                for (p = buf, endp = buf + sizeof(buf) - 1;;) {
        !            85:                        len = strlen(*argv);
        !            86:                        if (p + len < endp && p > buf) {
        !            87:                                *--p = '\0';
        !            88:                                syslog(pri, buf);
        !            89:                                p = buf;
        !            90:                        }
        !            91:                        if (len > sizeof(buf) - 1) {
        !            92:                                syslog(pri, *argv++);
        !            93:                                if (!--argc)
        !            94:                                        break;
        !            95:                        } else {
        !            96:                                bcopy(*argv++, p, len);
        !            97:                                p += len;
        !            98:                                if (!--argc)
        !            99:                                        break;
        !           100:                                *p++ = ' ';
        !           101:                                *--p = '\0';
        !           102:                        }
        !           103:                }
        !           104:                if (p != buf) {
        !           105:                        *p = '\0';
        !           106:                        syslog(pri, buf);
        !           107:                }
        !           108:                exit(0);
        !           109:        }
        !           110: 
        !           111:        /* main loop */
        !           112:        while (fgets(buf, sizeof(buf), stdin) != NULL)
        !           113:                syslog(pri, buf);
        !           114: 
        !           115:        exit(0);
        !           116: }
        !           117: 
        !           118: 
        !           119: struct code {
        !           120:        char    *c_name;
        !           121:        int     c_val;
        !           122: };
        !           123: 
        !           124: struct code    PriNames[] = {
        !           125:        "panic",        LOG_EMERG,
        !           126:        "emerg",        LOG_EMERG,
        !           127:        "alert",        LOG_ALERT,
        !           128:        "crit",         LOG_CRIT,
        !           129:        "err",          LOG_ERR,
        !           130:        "error",        LOG_ERR,
        !           131:        "warn",         LOG_WARNING,
        !           132:        "warning",      LOG_WARNING,
        !           133:        "notice",       LOG_NOTICE,
        !           134:        "info",         LOG_INFO,
        !           135:        "debug",        LOG_DEBUG,
        !           136:        NULL,           -1
        !           137: };
        !           138: 
        !           139: struct code    FacNames[] = {
        !           140:        "kern",         LOG_KERN,
        !           141:        "user",         LOG_USER,
        !           142:        "mail",         LOG_MAIL,
        !           143:        "daemon",       LOG_DAEMON,
        !           144:        "auth",         LOG_AUTH,
        !           145:        "security",     LOG_AUTH,
        !           146:        "syslog",       LOG_SYSLOG,
        !           147:        "lpr",          LOG_LPR,
        !           148:        "news",         LOG_NEWS,
        !           149:        "uucp",         LOG_UUCP,
        !           150:        "local0",       LOG_LOCAL0,
        !           151:        "local1",       LOG_LOCAL1,
        !           152:        "local2",       LOG_LOCAL2,
        !           153:        "local3",       LOG_LOCAL3,
        !           154:        "local4",       LOG_LOCAL4,
        !           155:        "local5",       LOG_LOCAL5,
        !           156:        "local6",       LOG_LOCAL6,
        !           157:        "local7",       LOG_LOCAL7,
        !           158:        NULL,           -1
        !           159: };
        !           160: 
        !           161: 
        !           162: /*
        !           163:  *  Decode a symbolic name to a numeric value
        !           164:  */
        !           165: 
        !           166: pencode(s)
        !           167:        register char *s;
        !           168: {
        !           169:        char *save;
        !           170:        int fac, lev;
        !           171: 
        !           172:        for (save = s; *s && *s != '.'; ++s);
        !           173:        if (*s) {
        !           174:                *s = '\0';
        !           175:                fac = decode(save, FacNames);
        !           176:                if (fac < 0)
        !           177:                        bailout("unknown facility name: ", save);
        !           178:                *s++ = '.';
        !           179:        }
        !           180:        else {
        !           181:                fac = 0;
        !           182:                s = save;
        !           183:        }
        !           184:        lev = decode(s, PriNames);
        !           185:        if (lev < 0)
        !           186:                bailout("unknown priority name: ", save);
        !           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: 
        !           197:        if (isdigit(*name))
        !           198:                return (atoi(name));
        !           199: 
        !           200:        for (c = codetab; c->c_name; c++)
        !           201:                if (!strcasecmp(name, c->c_name))
        !           202:                        return (c->c_val);
        !           203: 
        !           204:        return (-1);
        !           205: }
        !           206: 
        !           207: bailout(msg, arg)
        !           208:        char *msg, *arg;
        !           209: {
        !           210:        fprintf(stderr, "logger: %s%s\n", msg, arg);
        !           211:        exit(1);
        !           212: }
        !           213: 
        !           214: usage()
        !           215: {
        !           216:        fputs("logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n",
        !           217:            stderr);
        !           218:        exit(1);
        !           219: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.