[BACK]Return to logger.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / logger

Annotation of src/usr.bin/logger/logger.c, Revision 1.1

1.1     ! deraadt     1: /*     $NetBSD: logger.c,v 1.4 1994/12/22 06:27:00 jtc Exp $   */
        !             2:
        !             3: /*
        !             4:  * Copyright (c) 1983, 1993
        !             5:  *     The Regents of the University of California.  All rights reserved.
        !             6:  *
        !             7:  * Redistribution and use in source and binary forms, with or without
        !             8:  * modification, are permitted provided that the following conditions
        !             9:  * are met:
        !            10:  * 1. Redistributions of source code must retain the above copyright
        !            11:  *    notice, this list of conditions and the following disclaimer.
        !            12:  * 2. Redistributions in binary form must reproduce the above copyright
        !            13:  *    notice, this list of conditions and the following disclaimer in the
        !            14:  *    documentation and/or other materials provided with the distribution.
        !            15:  * 3. All advertising materials mentioning features or use of this software
        !            16:  *    must display the following acknowledgement:
        !            17:  *     This product includes software developed by the University of
        !            18:  *     California, Berkeley and its contributors.
        !            19:  * 4. Neither the name of the University nor the names of its contributors
        !            20:  *    may be used to endorse or promote products derived from this software
        !            21:  *    without specific prior written permission.
        !            22:  *
        !            23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            33:  * SUCH DAMAGE.
        !            34:  */
        !            35:
        !            36: #ifndef lint
        !            37: static char copyright[] =
        !            38: "@(#) Copyright (c) 1983, 1993\n\
        !            39:        The Regents of the University of California.  All rights reserved.\n";
        !            40: #endif /* not lint */
        !            41:
        !            42: #ifndef lint
        !            43: #if 0
        !            44: static char sccsid[] = "@(#)logger.c   8.1 (Berkeley) 6/6/93";
        !            45: #endif
        !            46: static char rcsid[] = "$NetBSD: logger.c,v 1.4 1994/12/22 06:27:00 jtc Exp $";
        !            47: #endif /* not lint */
        !            48:
        !            49: #include <errno.h>
        !            50: #include <unistd.h>
        !            51: #include <stdlib.h>
        !            52: #include <stdio.h>
        !            53: #include <ctype.h>
        !            54: #include <string.h>
        !            55:
        !            56: #define        SYSLOG_NAMES
        !            57: #include <syslog.h>
        !            58:
        !            59: int    decode __P((char *, CODE *));
        !            60: int    pencode __P((char *));
        !            61: void   usage __P((void));
        !            62:
        !            63: /*
        !            64:  * logger -- read and log utility
        !            65:  *
        !            66:  *     Reads from an input and arranges to write the result on the system
        !            67:  *     log.
        !            68:  */
        !            69: int
        !            70: main(argc, argv)
        !            71:        int argc;
        !            72:        char *argv[];
        !            73: {
        !            74:        int ch, logflags, pri;
        !            75:        char *tag, buf[1024];
        !            76:
        !            77:        tag = NULL;
        !            78:        pri = LOG_NOTICE;
        !            79:        logflags = 0;
        !            80:        while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
        !            81:                switch((char)ch) {
        !            82:                case 'f':               /* file to log */
        !            83:                        if (freopen(optarg, "r", stdin) == NULL) {
        !            84:                                (void)fprintf(stderr, "logger: %s: %s.\n",
        !            85:                                    optarg, strerror(errno));
        !            86:                                exit(1);
        !            87:                        }
        !            88:                        break;
        !            89:                case 'i':               /* log process id also */
        !            90:                        logflags |= LOG_PID;
        !            91:                        break;
        !            92:                case 'p':               /* priority */
        !            93:                        pri = pencode(optarg);
        !            94:                        break;
        !            95:                case 's':               /* log to standard error */
        !            96:                        logflags |= LOG_PERROR;
        !            97:                        break;
        !            98:                case 't':               /* tag */
        !            99:                        tag = optarg;
        !           100:                        break;
        !           101:                case '?':
        !           102:                default:
        !           103:                        usage();
        !           104:                }
        !           105:        argc -= optind;
        !           106:        argv += optind;
        !           107:
        !           108:        /* setup for logging */
        !           109:        openlog(tag ? tag : getlogin(), logflags, 0);
        !           110:        (void) fclose(stdout);
        !           111:
        !           112:        /* log input line if appropriate */
        !           113:        if (argc > 0) {
        !           114:                register char *p, *endp;
        !           115:                int len;
        !           116:
        !           117:                for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
        !           118:                        len = strlen(*argv);
        !           119:                        if (p + len > endp && p > buf) {
        !           120:                                syslog(pri, "%s", buf);
        !           121:                                p = buf;
        !           122:                        }
        !           123:                        if (len > sizeof(buf) - 1)
        !           124:                                syslog(pri, "%s", *argv++);
        !           125:                        else {
        !           126:                                if (p != buf)
        !           127:                                        *p++ = ' ';
        !           128:                                bcopy(*argv++, p, len);
        !           129:                                *(p += len) = '\0';
        !           130:                        }
        !           131:                }
        !           132:                if (p != buf)
        !           133:                        syslog(pri, "%s", buf);
        !           134:        } else
        !           135:                while (fgets(buf, sizeof(buf), stdin) != NULL)
        !           136:                        syslog(pri, "%s", buf);
        !           137:        exit(0);
        !           138: }
        !           139:
        !           140: /*
        !           141:  *  Decode a symbolic name to a numeric value
        !           142:  */
        !           143: int
        !           144: pencode(s)
        !           145:        register char *s;
        !           146: {
        !           147:        char *save;
        !           148:        int fac, lev;
        !           149:
        !           150:        for (save = s; *s && *s != '.'; ++s);
        !           151:        if (*s) {
        !           152:                *s = '\0';
        !           153:                fac = decode(save, facilitynames);
        !           154:                if (fac < 0) {
        !           155:                        (void)fprintf(stderr,
        !           156:                            "logger: unknown facility name: %s.\n", save);
        !           157:                        exit(1);
        !           158:                }
        !           159:                *s++ = '.';
        !           160:        }
        !           161:        else {
        !           162:                fac = 0;
        !           163:                s = save;
        !           164:        }
        !           165:        lev = decode(s, prioritynames);
        !           166:        if (lev < 0) {
        !           167:                (void)fprintf(stderr,
        !           168:                    "logger: unknown priority name: %s.\n", save);
        !           169:                exit(1);
        !           170:        }
        !           171:        return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
        !           172: }
        !           173:
        !           174: int
        !           175: decode(name, codetab)
        !           176:        char *name;
        !           177:        CODE *codetab;
        !           178: {
        !           179:        register CODE *c;
        !           180:
        !           181:        if (isdigit(*name))
        !           182:                return (atoi(name));
        !           183:
        !           184:        for (c = codetab; c->c_name; c++)
        !           185:                if (!strcasecmp(name, c->c_name))
        !           186:                        return (c->c_val);
        !           187:
        !           188:        return (-1);
        !           189: }
        !           190:
        !           191: void
        !           192: usage()
        !           193: {
        !           194:        (void)fprintf(stderr,
        !           195:            "logger: [-is] [-f file] [-p pri] [-t tag] [ message ... ]\n");
        !           196:        exit(1);
        !           197: }