home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / logger / logger.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-17  |  4.6 KB  |  188 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. #include <syslog.h>
  46. #include <ctype.h>
  47.  
  48. /*
  49. **  LOGGER -- read and log utility
  50. **
  51. **    This routine reads from an input and arranges to write the
  52. **    result on the system log, along with a useful tag.
  53. */
  54.  
  55. main(argc, argv)
  56.     int argc;
  57.     char **argv;
  58. {
  59.     extern char *optarg;
  60.     extern int errno, optind;
  61.     int pri = LOG_NOTICE;
  62.     int ch, logflags = 0;
  63.     char *tag, buf[1024], *getlogin(), *strerror();
  64.  
  65.     tag = NULL;
  66.     while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
  67.         switch((char)ch) {
  68.         case 'f':        /* file to log */
  69.             if (freopen(optarg, "r", stdin) == NULL) {
  70.                 (void)fprintf(stderr, "logger: %s: %s.\n",
  71.                     optarg, strerror(errno));
  72.                 exit(1);
  73.             }
  74.             break;
  75.         case 'i':        /* log process id also */
  76.             logflags |= LOG_PID;
  77.             break;
  78.         case 'p':        /* priority */
  79.             pri = pencode(optarg);
  80.             break;
  81.         case 's':        /* log to standard error */
  82.             logflags |= LOG_PERROR;
  83.             break;
  84.         case 't':        /* tag */
  85.             tag = optarg;
  86.             break;
  87.         case '?':
  88.         default:
  89.             usage();
  90.         }
  91.     argc -= optind;
  92.     argv += optind;
  93.  
  94.     /* setup for logging */
  95.     openlog(tag ? tag : getlogin(), logflags, 0);
  96.     (void) fclose(stdout);
  97.  
  98.     /* log input line if appropriate */
  99.     if (argc > 0) {
  100.         register char *p, *endp;
  101.         int len;
  102.  
  103.         for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
  104.             len = strlen(*argv);
  105.             if (p + len > endp && p > buf) {
  106.                 syslog(pri, "%s", buf);
  107.                 p = buf;
  108.             }
  109.             if (len > sizeof(buf) - 1)
  110.                 syslog(pri, "%s", *argv++);
  111.             else {
  112.                 if (p != buf)
  113.                     *p++ = ' ';
  114.                 bcopy(*argv++, p, len);
  115.                 *(p += len) = '\0';
  116.             }
  117.         }
  118.         if (p != buf)
  119.             syslog(pri, "%s", buf);
  120.         exit(0);
  121.     }
  122.  
  123.     /* main loop */
  124.     while (fgets(buf, sizeof(buf), stdin) != NULL)
  125.         syslog(pri, "%s", buf);
  126.  
  127.     exit(0);
  128. }
  129.  
  130. #define    SYSLOG_NAMES
  131. #include <syslog.h>
  132.  
  133. /*
  134.  *  Decode a symbolic name to a numeric value
  135.  */
  136. pencode(s)
  137.     register char *s;
  138. {
  139.     char *save;
  140.     int fac, lev;
  141.  
  142.     for (save = s; *s && *s != '.'; ++s);
  143.     if (*s) {
  144.         *s = '\0';
  145.         fac = decode(save, facilitynames);
  146.         if (fac < 0) {
  147.             (void)fprintf(stderr,
  148.                 "logger: unknown facility name: %s.\n", save);
  149.             exit(1);
  150.         }
  151.         *s++ = '.';
  152.     }
  153.     else {
  154.         fac = 0;
  155.         s = save;
  156.     }
  157.     lev = decode(s, prioritynames);
  158.     if (lev < 0) {
  159.         (void)fprintf(stderr,
  160.             "logger: unknown priority name: %s.\n", save);
  161.         exit(1);
  162.     }
  163.     return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
  164. }
  165.  
  166. decode(name, codetab)
  167.     char *name;
  168.     CODE *codetab;
  169. {
  170.     register CODE *c;
  171.  
  172.     if (isdigit(*name))
  173.         return (atoi(name));
  174.  
  175.     for (c = codetab; c->c_name; c++)
  176.         if (!strcasecmp(name, c->c_name))
  177.             return (c->c_val);
  178.  
  179.     return (-1);
  180. }
  181.  
  182. usage()
  183. {
  184.     (void)fprintf(stderr,
  185.         "logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n");
  186.     exit(1);
  187. }
  188.