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