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
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
34 * "@(#) Copyright (c) 1983 Regents of the University of California.\n\
35 * All rights reserved.\n";
39 * static char sccsid[] = "@(#)logger.c 6.8 (Berkeley) 6/29/88";
42 * Porting to Minix by G. Falzoni <gfalzoni@inwind.it>
45 #include <sys/types.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.
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
);
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
);
91 ** Name: int pencode(char *s);
92 ** Function: Decode a symbolic name (facility/priority)
93 ** to a numeric value.
100 for (save
= s
; *s
&& *s
!= '.'; ++s
);
104 fac
= decode(save
, facilitynames
);
106 fac
= decode(save
, FacNames
);
108 if (fac
< 0) bailout("unknown facility name:", save
);
115 lev
= decode(s
, prioritynames
);
117 lev
= decode(s
, PriNames
);
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;
132 static const char usage
[] =
133 "[-i] [-f file] [-p pri] [-t tag] [ message ... ]";
136 while ((ch
= getopt(argc
, argv
, "f:ip:t:")) != EOF
) {
138 case 'f': /* file to log */
139 if (freopen(optarg
, "r", stdin
) == NULL
) {
140 bailout(strerror(errno
), optarg
);
143 case 'i': /* log process id also */
146 case 'p': /* priority */
147 pri
= pencode(optarg
);
153 default: bailout(usage
, ""); break;
159 /* Setup for logging */
160 openlog(tag
? tag
: getlogin(), logflags
, 0);
163 if (argc
> 0) { /* Log input line if appropriate */
167 for (p
= buf
, endp
= buf
+ sizeof(buf
) - 1;;) {
169 if (p
+ len
< endp
&& p
> buf
) {
174 if (len
> sizeof(buf
) - 1) {
175 syslog(pri
, *argv
++);
178 memcpy(p
, *argv
++, len
);
189 } else /* Main loop */
190 while (fgets(buf
, sizeof(buf
), stdin
) != NULL
) syslog(pri
, buf
);