logger.c

Go to the documentation of this file.
00001 /* Copyright (c) 1983, 1988, 1993
00002  * The Regents of the University of California.  All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  * 1. Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  * 2. Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  * 3. All advertising materials mentioning features or use of this software
00013  *    must display the following acknowledgement:
00014  *      This product includes software developed by the University of
00015  *      California, Berkeley and its contributors.
00016  * 4. Neither the name of the University nor the names of its contributors
00017  *    may be used to endorse or promote products derived from this software
00018  *    without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00024  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00025  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00026  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00027  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00028  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00029  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * #ifndef lint
00033  * char copyright[] =
00034  * "@(#) Copyright (c) 1983 Regents of the University of California.\n\
00035  *  All rights reserved.\n";
00036  * #endif
00037  *
00038  * #ifndef lint
00039  * static char sccsid[] = "@(#)logger.c 6.8 (Berkeley) 6/29/88";
00040  * #endif
00041  *
00042  * Porting to Minix by G. Falzoni <gfalzoni@inwind.it>
00043  * $Id: logger.c,v 1.1 2006/04/03 13:07:42 beng Exp $
00044  */
00045 
00046 #include <sys/types.h>
00047 #include <unistd.h>
00048 #include <errno.h>
00049 #include <stdio.h>
00050 #include <ctype.h>
00051 #include <stdlib.h>
00052 #include <string.h>
00053 
00054 /*
00055 **      LOGGER -- read and log utility
00056 **
00057 **      This program reads from an input and arranges to write the
00058 **      result on the system log, along with a useful tag.
00059 */
00060 
00061 #define  SYSLOG_NAMES
00062 #include <syslog.h>
00063 
00064 /*
00065 **      Name:           void bailout(char *msg, char *arg);
00066 **      Function:       Handles error exit.
00067 */
00068 void bailout(const char *msg, const char *arg)
00069 {
00070 
00071   fprintf(stderr, "logger: %s %s\n", msg, arg);
00072   exit(EXIT_FAILURE);
00073 }
00074 
00075 /*
00076 **      Name:           int decode(char *name, struct code * codetab);
00077 **      Function:       Decodes a name to the equivalent priority/facility.
00078 */
00079 int decode(char *name, const struct _code * codetab)
00080 {
00081   const struct _code *c;
00082 
00083   if (isdigit(*name)) return(atoi(name));
00084 
00085   for (c = codetab; c->c_name; c++)
00086         if (!strcasecmp(name, c->c_name)) return(c->c_val);
00087 
00088   return(-1);
00089 }
00090 
00091 /*
00092 **      Name:           int pencode(char *s);
00093 **      Function:       Decode a symbolic name (facility/priority)
00094 **                      to a numeric value.
00095 */
00096 int pencode(char *s)
00097 {
00098   char *save;
00099   int fac, lev;
00100 
00101   for (save = s; *s && *s != '.'; ++s);
00102   if (*s) {
00103         *s = '\0';
00104         fac = decode(save, FacNames);
00105         if (fac < 0) bailout("unknown facility name:", save);
00106         *s++ = '.';
00107   } else {
00108         fac = 0;
00109         s = save;
00110   }
00111   lev = decode(s, PriNames);
00112   if (lev < 0) bailout("unknown priority name:", save);
00113   return((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
00114 }
00115 
00116 /*
00117 **      Name:           int main(int argc, char **argv);
00118 **      Function:       Main entry for logger.
00119 */
00120 int main(int argc, char **argv)
00121 {
00122   int pri = LOG_NOTICE;
00123   int ch, logflags = 0;
00124   char *tag, buf[200];
00125   static const char usage[] =
00126   "[-i] [-f file] [-p pri] [-t tag] [ message ... ]";
00127 
00128   tag = NULL;
00129   while ((ch = getopt(argc, argv, "f:ip:t:")) != EOF) {
00130         switch ((char) ch) {
00131             case 'f':           /* file to log */
00132                 if (freopen(optarg, "r", stdin) == NULL) {
00133                         bailout(strerror(errno), optarg);
00134                 }
00135                 break;
00136             case 'i':           /* log process id also */
00137                 logflags |= LOG_PID;
00138                 break;
00139             case 'p':           /* priority */
00140                 pri = pencode(optarg);
00141                 break;
00142             case 't':           /* tag */
00143                 tag = optarg;
00144                 break;
00145             case '?':
00146             default:    bailout(usage, "");     break;
00147         }
00148   }
00149   argc -= optind;
00150   argv += optind;
00151 
00152   /* Setup for logging */
00153   openlog(tag ? tag : getlogin(), logflags, 0);
00154   fclose(stdout);
00155 
00156   if (argc > 0) {               /* Log input line if appropriate */
00157         char *p, *endp;
00158         int len;
00159 
00160         for (p = buf, endp = buf + sizeof(buf) - 1;;) {
00161                 len = strlen(*argv);
00162                 if (p + len < endp && p > buf) {
00163                         *--p = '\0';
00164                         syslog(pri, buf);
00165                         p = buf;
00166                 }
00167                 if (len > sizeof(buf) - 1) {
00168                         syslog(pri, *argv++);
00169                         if (!--argc) break;
00170                 } else {
00171                         memcpy(p, *argv++, len);
00172                         p += len;
00173                         if (!--argc) break;
00174                         *p++ = ' ';
00175                         *--p = '\0';
00176                 }
00177         }
00178         if (p != buf) {
00179                 *p = '\0';
00180                 syslog(pri, buf);
00181         }
00182   } else                        /* Main loop */
00183         while (fgets(buf, sizeof(buf), stdin) != NULL) syslog(pri, buf);
00184 
00185   return EXIT_SUCCESS;
00186 }
00187 

Generated on Fri Apr 14 22:57:13 2006 for minix by  doxygen 1.4.6