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>
46 #include <sys/types.h>
55 ** LOGGER -- read and log utility
57 ** This program reads from an input and arranges to write the
58 ** result on the system log, along with a useful tag.
65 ** Name: void bailout(char *msg, char *arg);
66 ** Function: Handles error exit.
68 void bailout(const char *msg
, const char *arg
)
71 fprintf(stderr
, "logger: %s %s\n", msg
, arg
);
76 ** Name: int decode(char *name, struct code * codetab);
77 ** Function: Decodes a name to the equivalent priority/facility.
79 int decode(char *name
, const struct _code
* codetab
)
81 const struct _code
*c
;
83 if (isdigit(*name
)) return(atoi(name
));
85 for (c
= codetab
; c
->c_name
; c
++)
86 if (!strcasecmp(name
, c
->c_name
)) return(c
->c_val
);
92 ** Name: int pencode(char *s);
93 ** Function: Decode a symbolic name (facility/priority)
94 ** to a numeric value.
101 for (save
= s
; *s
&& *s
!= '.'; ++s
);
104 fac
= decode(save
, FacNames
);
105 if (fac
< 0) bailout("unknown facility name:", save
);
111 lev
= decode(s
, PriNames
);
112 if (lev
< 0) bailout("unknown priority name:", save
);
113 return((lev
& LOG_PRIMASK
) | (fac
& LOG_FACMASK
));
117 ** Name: int main(int argc, char **argv);
118 ** Function: Main entry for logger.
120 int main(int argc
, char **argv
)
122 int pri
= LOG_NOTICE
;
123 int ch
, logflags
= 0;
125 static const char usage
[] =
126 "[-i] [-f file] [-p pri] [-t tag] [ message ... ]";
129 while ((ch
= getopt(argc
, argv
, "f:ip:t:")) != EOF
) {
131 case 'f': /* file to log */
132 if (freopen(optarg
, "r", stdin
) == NULL
) {
133 bailout(strerror(errno
), optarg
);
136 case 'i': /* log process id also */
139 case 'p': /* priority */
140 pri
= pencode(optarg
);
146 default: bailout(usage
, ""); break;
152 /* Setup for logging */
153 openlog(tag
? tag
: getlogin(), logflags
, 0);
156 if (argc
> 0) { /* Log input line if appropriate */
160 for (p
= buf
, endp
= buf
+ sizeof(buf
) - 1;;) {
162 if (p
+ len
< endp
&& p
> buf
) {
167 if (len
> sizeof(buf
) - 1) {
168 syslog(pri
, *argv
++);
171 memcpy(p
, *argv
++, len
);
182 } else /* Main loop */
183 while (fgets(buf
, sizeof(buf
), stdin
) != NULL
) syslog(pri
, buf
);