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