home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / inetutils-1.2-src.tgz / tar.out / fsf / inetutils / syslog / syslog.c < prev   
C/C++ Source or Header  |  1996-09-28  |  5KB  |  197 lines

  1. /*
  2.  * Copyright (c) 1983, 1993
  3.  *    The Regents of the University of California.  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. static char copyright[] =
  36. "@(#) Copyright (c) 1983, 1993\n\
  37.     The Regents of the University of California.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)logger.c    8.1 (Berkeley) 6/6/93";
  42. #endif /* not lint */
  43.  
  44. #ifdef HAVE_CONFIG_H
  45. #include <config.h>
  46. #endif
  47.  
  48. #include <errno.h>
  49. #include <unistd.h>
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <ctype.h>
  53. #include <string.h>
  54.  
  55. #define    SYSLOG_NAMES
  56. #include <syslog.h>
  57.  
  58. int    decode __P((char *, CODE *));
  59. int    pencode __P((char *));
  60. void    usage __P((void));
  61.  
  62. /*
  63.  * syslog -- read and log utility
  64.  *
  65.  *    Reads from an input and arranges to write the result on the system
  66.  *    log.
  67.  */
  68. int
  69. main(argc, argv)
  70.     int argc;
  71.     char *argv[];
  72. {
  73.     int ch, logflags, pri;
  74.     char *tag, buf[1024];
  75.  
  76.     tag = NULL;
  77.     pri = LOG_NOTICE;
  78.     logflags = 0;
  79.     while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
  80.         switch((char)ch) {
  81.         case 'f':        /* file to log */
  82.             if (freopen(optarg, "r", stdin) == NULL) {
  83.                 (void)fprintf(stderr, "syslog: %s: %s.\n",
  84.                     optarg, strerror(errno));
  85.                 exit(1);
  86.             }
  87.             break;
  88.         case 'i':        /* log process id also */
  89.             logflags |= LOG_PID;
  90.             break;
  91.         case 'p':        /* priority */
  92.             pri = pencode(optarg);
  93.             break;
  94.         case 's':        /* log to standard error */
  95.             logflags |= LOG_PERROR;
  96.             break;
  97.         case 't':        /* tag */
  98.             tag = optarg;
  99.             break;
  100.         case '?':
  101.         default:
  102.             usage();
  103.         }
  104.     argc -= optind;
  105.     argv += optind;
  106.  
  107.     /* setup for logging */
  108.     openlog(tag ? tag : getlogin(), logflags, 0);
  109.     (void) fclose(stdout);
  110.  
  111.     /* log input line if appropriate */
  112.     if (argc > 0) {
  113.         register char *p, *endp;
  114.         int len;
  115.  
  116.         for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
  117.             len = strlen(*argv);
  118.             if (p + len > endp && p > buf) {
  119.                 syslog(pri, "%s", buf);
  120.                 p = buf;
  121.             }
  122.             if (len > sizeof(buf) - 1)
  123.                 syslog(pri, "%s", *argv++);
  124.             else {
  125.                 if (p != buf)
  126.                     *p++ = ' ';
  127.                 bcopy(*argv++, p, len);
  128.                 *(p += len) = '\0';
  129.             }
  130.         }
  131.         if (p != buf)
  132.             syslog(pri, "%s", buf);
  133.     } else
  134.         while (fgets(buf, sizeof(buf), stdin) != NULL)
  135.             syslog(pri, "%s", buf);
  136.     exit(0);
  137. }
  138.  
  139. /*
  140.  *  Decode a symbolic name to a numeric value
  141.  */
  142. int
  143. pencode(s)
  144.     register char *s;
  145. {
  146.     char *save;
  147.     int fac, lev;
  148.  
  149.     for (save = s; *s && *s != '.'; ++s);
  150.     if (*s) {
  151.         *s = '\0';
  152.         fac = decode(save, facilitynames);
  153.         if (fac < 0) {
  154.             (void)fprintf(stderr,
  155.                 "syslog: unknown facility name: %s.\n", save);
  156.             exit(1);
  157.         }
  158.         *s++ = '.';
  159.     }
  160.     else {
  161.         fac = 0;
  162.         s = save;
  163.     }
  164.     lev = decode(s, prioritynames);
  165.     if (lev < 0) {
  166.         (void)fprintf(stderr,
  167.             "syslog: unknown priority name: %s.\n", save);
  168.         exit(1);
  169.     }
  170.     return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
  171. }
  172.  
  173. int
  174. decode(name, codetab)
  175.     char *name;
  176.     CODE *codetab;
  177. {
  178.     register CODE *c;
  179.  
  180.     if (isdigit(*name))
  181.         return (atoi(name));
  182.  
  183.     for (c = codetab; c->c_name; c++)
  184.         if (!strcasecmp(name, c->c_name))
  185.             return (c->c_val);
  186.  
  187.     return (-1);
  188. }
  189.  
  190. void
  191. usage()
  192. {
  193.     (void)fprintf(stderr,
  194.         "syslog: [-is] [-f file] [-p pri] [-t tag] [ message ... ]\n");
  195.     exit(1);
  196. }
  197.