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

unix.superglobalmegacorp.com

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