add opendir alias
[minix.git] / commands / logger / logger.c
blob68aa271b67d0d206509fc8827dca47822377fce5
1 /* Copyright (c) 1983, 1988, 1993
2 * The Regents of the University of California. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by the University of
15 * California, Berkeley and its contributors.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
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.
32 * #ifndef lint
33 * char copyright[] =
34 * "@(#) Copyright (c) 1983 Regents of the University of California.\n\
35 * All rights reserved.\n";
36 * #endif
38 * #ifndef lint
39 * static char sccsid[] = "@(#)logger.c 6.8 (Berkeley) 6/29/88";
40 * #endif
42 * Porting to Minix by G. Falzoni <gfalzoni@inwind.it>
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <ctype.h>
50 #include <stdlib.h>
51 #include <string.h>
54 ** LOGGER -- read and log utility
56 ** This program reads from an input and arranges to write the
57 ** result on the system log, along with a useful tag.
60 #define SYSLOG_NAMES
61 #include <syslog.h>
64 ** Name: void bailout(char *msg, char *arg);
65 ** Function: Handles error exit.
67 void bailout(const char *msg, const char *arg)
70 fprintf(stderr, "logger: %s %s\n", msg, arg);
71 exit(EXIT_FAILURE);
75 ** Name: int decode(char *name, struct code * codetab);
76 ** Function: Decodes a name to the equivalent priority/facility.
78 int decode(char *name, const struct _code * codetab)
80 const struct _code *c;
82 if (isdigit(*name)) return(atoi(name));
84 for (c = codetab; c->c_name; c++)
85 if (!strcasecmp(name, c->c_name)) return(c->c_val);
87 return(-1);
91 ** Name: int pencode(char *s);
92 ** Function: Decode a symbolic name (facility/priority)
93 ** to a numeric value.
95 int pencode(char *s)
97 char *save;
98 int fac, lev;
100 for (save = s; *s && *s != '.'; ++s);
101 if (*s) {
102 *s = '\0';
103 #ifdef __NBSD_LIBC
104 fac = decode(save, facilitynames);
105 #else
106 fac = decode(save, FacNames);
107 #endif
108 if (fac < 0) bailout("unknown facility name:", save);
109 *s++ = '.';
110 } else {
111 fac = 0;
112 s = save;
114 #ifdef __NBSD_LIBC
115 lev = decode(s, prioritynames);
116 #else
117 lev = decode(s, PriNames);
118 #endif
119 if (lev < 0) bailout("unknown priority name:", save);
120 return((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
124 ** Name: int main(int argc, char **argv);
125 ** Function: Main entry for logger.
127 int main(int argc, char **argv)
129 int pri = LOG_NOTICE;
130 int ch, logflags = 0;
131 char *tag, buf[200];
132 static const char usage[] =
133 "[-i] [-f file] [-p pri] [-t tag] [ message ... ]";
135 tag = NULL;
136 while ((ch = getopt(argc, argv, "f:ip:t:")) != EOF) {
137 switch ((char) ch) {
138 case 'f': /* file to log */
139 if (freopen(optarg, "r", stdin) == NULL) {
140 bailout(strerror(errno), optarg);
142 break;
143 case 'i': /* log process id also */
144 logflags |= LOG_PID;
145 break;
146 case 'p': /* priority */
147 pri = pencode(optarg);
148 break;
149 case 't': /* tag */
150 tag = optarg;
151 break;
152 case '?':
153 default: bailout(usage, ""); break;
156 argc -= optind;
157 argv += optind;
159 /* Setup for logging */
160 openlog(tag ? tag : getlogin(), logflags, 0);
161 fclose(stdout);
163 if (argc > 0) { /* Log input line if appropriate */
164 char *p, *endp;
165 int len;
167 for (p = buf, endp = buf + sizeof(buf) - 1;;) {
168 len = strlen(*argv);
169 if (p + len < endp && p > buf) {
170 *--p = '\0';
171 syslog(pri, buf);
172 p = buf;
174 if (len > sizeof(buf) - 1) {
175 syslog(pri, *argv++);
176 if (!--argc) break;
177 } else {
178 memcpy(p, *argv++, len);
179 p += len;
180 if (!--argc) break;
181 *p++ = ' ';
182 *--p = '\0';
185 if (p != buf) {
186 *p = '\0';
187 syslog(pri, buf);
189 } else /* Main loop */
190 while (fgets(buf, sizeof(buf), stdin) != NULL) syslog(pri, buf);
192 return EXIT_SUCCESS;
195 /** logger.c **/