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