home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / daemons / syslog_f.z / syslog_f / logger / logger.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  4.6 KB  |  187 lines

  1. /*
  2.  * Copyright (c) 1983 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)logger.c    6.15 (Berkeley) 3/1/91";
  42. #endif /* not lint */
  43.  
  44. #include <stdio.h>
  45. #define    SYSLOG_NAMES
  46. #include <syslog.h>
  47. #include <ctype.h>
  48.  
  49. /*
  50. **  LOGGER -- read and log utility
  51. **
  52. **    This routine reads from an input and arranges to write the
  53. **    result on the system log, along with a useful tag.
  54. */
  55.  
  56. main(argc, argv)
  57.     int argc;
  58.     char **argv;
  59. {
  60.     extern char *optarg;
  61.     extern int errno, optind;
  62.     int pri = LOG_NOTICE;
  63.     int ch, logflags = 0;
  64.     char *tag, buf[1024], *getlogin(), *strerror();
  65.  
  66.     tag = NULL;
  67.     while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
  68.         switch((char)ch) {
  69.         case 'f':        /* file to log */
  70.             if (freopen(optarg, "r", stdin) == NULL) {
  71.                 (void)fprintf(stderr, "logger: %s: %s.\n",
  72.                     optarg, strerror(errno));
  73.                 exit(1);
  74.             }
  75.             break;
  76.         case 'i':        /* log process id also */
  77.             logflags |= LOG_PID;
  78.             break;
  79.         case 'p':        /* priority */
  80.             pri = pencode(optarg);
  81.             break;
  82.         case 's':        /* log to standard error */
  83.             logflags |= LOG_PERROR;
  84.             break;
  85.         case 't':        /* tag */
  86.             tag = optarg;
  87.             break;
  88.         case '?':
  89.         default:
  90.             usage();
  91.         }
  92.     argc -= optind;
  93.     argv += optind;
  94.  
  95.     /* setup for logging */
  96.     openlog(tag ? tag : getlogin(), logflags, 0);
  97.     (void) fclose(stdout);
  98.  
  99.     /* log input line if appropriate */
  100.     if (argc > 0) {
  101.         register char *p, *endp;
  102.         int len;
  103.  
  104.         for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
  105.             len = strlen(*argv);
  106.             if (p + len > endp && p > buf) {
  107.                 syslog(pri, "%s", buf);
  108.                 p = buf;
  109.             }
  110.             if (len > sizeof(buf) - 1)
  111.                 syslog(pri, "%s", *argv++);
  112.             else {
  113.                 if (p != buf)
  114.                     *p++ = ' ';
  115.                 bcopy(*argv++, p, len);
  116.                 *(p += len) = '\0';
  117.             }
  118.         }
  119.         if (p != buf)
  120.             syslog(pri, "%s", buf);
  121.         exit(0);
  122.     }
  123.  
  124.     /* main loop */
  125.     while (fgets(buf, sizeof(buf), stdin) != NULL)
  126.         syslog(pri, "%s", buf);
  127.  
  128.     exit(0);
  129. }
  130.  
  131.  
  132. /*
  133.  *  Decode a symbolic name to a numeric value
  134.  */
  135. pencode(s)
  136.     register char *s;
  137. {
  138.     char *save;
  139.     int fac, lev;
  140.  
  141.     for (save = s; *s && *s != '.'; ++s);
  142.     if (*s) {
  143.         *s = '\0';
  144.         fac = decode(save, facilitynames);
  145.         if (fac < 0) {
  146.             (void)fprintf(stderr,
  147.                 "logger: unknown facility name: %s.\n", save);
  148.             exit(1);
  149.         }
  150.         *s++ = '.';
  151.     }
  152.     else {
  153.         fac = 0;
  154.         s = save;
  155.     }
  156.     lev = decode(s, prioritynames);
  157.     if (lev < 0) {
  158.         (void)fprintf(stderr,
  159.             "logger: unknown priority name: %s.\n", save);
  160.         exit(1);
  161.     }
  162.     return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
  163. }
  164.  
  165. decode(name, codetab)
  166.     char *name;
  167.     CODE *codetab;
  168. {
  169.     register CODE *c;
  170.  
  171.     if (isdigit(*name))
  172.         return (atoi(name));
  173.  
  174.     for (c = codetab; c->c_name; c++)
  175.         if (!strcasecmp(name, c->c_name))
  176.             return (c->c_val);
  177.  
  178.     return (-1);
  179. }
  180.  
  181. usage()
  182. {
  183.     (void)fprintf(stderr,
  184.         "logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n");
  185.     exit(1);
  186. }
  187.