2 * Mini logger implementation for busybox
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
31 #if !defined BB_SYSLOGD
34 #include <sys/syslog.h>
37 /* We have to do this since the header file defines static
38 * structues. Argh.... bad libc, bad, bad...
40 #include <sys/syslog.h>
41 typedef struct _code {
45 extern CODE prioritynames[];
46 extern CODE facilitynames[];
49 static const char logger_usage[] =
50 "logger [OPTION]... [MESSAGE]\n\n"
51 "Write MESSAGE to the system log. If MESSAGE is '-', log stdin.\n\n"
53 "\t-s\tLog to stderr as well as the system log.\n"
54 "\t-t\tLog using the specified tag (defaults to user name).\n"
55 "\t-p\tEnter the message with the specified priority.\n"
56 "\t\tThis may be numerical or a ``facility.level'' pair.\n";
59 /* Decode a symbolic name to a numeric value
60 * this function is based on code
61 * Copyright (c) 1983, 1993
62 * The Regents of the University of California. All rights reserved.
65 decode(char* name, CODE* codetab)
71 for (c = codetab; c->c_name; c++) {
72 if (!strcasecmp(name, c->c_name)) {
80 /* Decode a symbolic name to a numeric value
81 * this function is based on code
82 * Copyright (c) 1983, 1993
83 * The Regents of the University of California. All rights reserved.
89 int lev, fac=LOG_USER;
91 for (save = s; *s && *s != '.'; ++s);
94 fac = decode(save, facilitynames);
96 fprintf(stderr, "unknown facility name: %s\n", save);
104 lev = decode(s, prioritynames);
106 fprintf(stderr, "unknown priority name: %s\n", save);
109 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
113 extern int logger_main(int argc, char **argv)
115 int pri = LOG_USER|LOG_NOTICE;
117 int fromStdinFlag=FALSE;
118 int stopLookingAtMeLikeThat=FALSE;
119 char *message, buf[1024], name[128];
121 /* Fill out the name string early (may be overwritten later */
122 my_getpwuid(name, geteuid());
124 /* Parse any options */
125 while (--argc > 0 && **(++argv) == '-') {
126 if (*((*argv)+1) == '\0') {
129 stopLookingAtMeLikeThat=FALSE;
130 while (*(++(*argv)) && stopLookingAtMeLikeThat==FALSE) {
133 option |= LOG_PERROR;
139 pri = pencode(*(++argv));
140 stopLookingAtMeLikeThat=TRUE;
146 strncpy(name, *(++argv), sizeof(name));
147 stopLookingAtMeLikeThat=TRUE;
155 if (fromStdinFlag==TRUE) {
156 /* read from stdin */
158 while ((c = getc(stdin)) != EOF && i<sizeof(buf)) {
166 fprintf(stderr, "No message\n");
171 openlog( name, option, (pri | LOG_FACMASK));
172 syslog( pri, message);