386BSD 0.1 development
[unix-history] / usr / src / usr.bin / logger / logger.c
index afe2a31..457a720 100644 (file)
@@ -2,12 +2,33 @@
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -17,7 +38,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)logger.c   6.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)logger.c   6.15 (Berkeley) 3/1/91";
 #endif /* not lint */
 
 #include <stdio.h>
 #endif /* not lint */
 
 #include <stdio.h>
@@ -36,18 +57,18 @@ main(argc, argv)
        char **argv;
 {
        extern char *optarg;
        char **argv;
 {
        extern char *optarg;
-       extern int optind;
+       extern int errno, optind;
        int pri = LOG_NOTICE;
        int ch, logflags = 0;
        int pri = LOG_NOTICE;
        int ch, logflags = 0;
-       char *tag, buf[200], *getlogin();
+       char *tag, buf[1024], *getlogin(), *strerror();
 
        tag = NULL;
 
        tag = NULL;
-       while ((ch = getopt(argc, argv, "f:ip:t:")) != EOF)
+       while ((ch = getopt(argc, argv, "f:ip:st:")) != EOF)
                switch((char)ch) {
                case 'f':               /* file to log */
                        if (freopen(optarg, "r", stdin) == NULL) {
                switch((char)ch) {
                case 'f':               /* file to log */
                        if (freopen(optarg, "r", stdin) == NULL) {
-                               fprintf("logger: ");
-                               perror(optarg);
+                               (void)fprintf(stderr, "logger: %s: %s.\n",
+                                   optarg, strerror(errno));
                                exit(1);
                        }
                        break;
                                exit(1);
                        }
                        break;
@@ -57,6 +78,9 @@ main(argc, argv)
                case 'p':               /* priority */
                        pri = pencode(optarg);
                        break;
                case 'p':               /* priority */
                        pri = pencode(optarg);
                        break;
+               case 's':               /* log to standard error */
+                       logflags |= LOG_PERROR;
+                       break;
                case 't':               /* tag */
                        tag = optarg;
                        break;
                case 't':               /* tag */
                        tag = optarg;
                        break;
@@ -76,88 +100,39 @@ main(argc, argv)
                register char *p, *endp;
                int len;
 
                register char *p, *endp;
                int len;
 
-               for (p = buf, endp = buf + sizeof(buf) - 1;;) {
+               for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
                        len = strlen(*argv);
                        len = strlen(*argv);
-                       if (p + len < endp && p > buf) {
-                               *--p = '\0';
-                               syslog(pri, buf);
+                       if (p + len > endp && p > buf) {
+                               syslog(pri, "%s", buf);
                                p = buf;
                        }
                                p = buf;
                        }
-                       if (len > sizeof(buf) - 1) {
-                               syslog(pri, *argv++);
-                               if (!--argc)
-                                       break;
-                       } else {
+                       if (len > sizeof(buf) - 1)
+                               syslog(pri, "%s", *argv++);
+                       else {
+                               if (p != buf)
+                                       *p++ = ' ';
                                bcopy(*argv++, p, len);
                                bcopy(*argv++, p, len);
-                               p += len;
-                               if (!--argc)
-                                       break;
-                               *p++ = ' ';
-                               *--p = '\0';
+                               *(p += len) = '\0';
                        }
                }
                        }
                }
-               if (p != buf) {
-                       *p = '\0';
-                       syslog(pri, buf);
-               }
+               if (p != buf)
+                       syslog(pri, "%s", buf);
                exit(0);
        }
 
        /* main loop */
        while (fgets(buf, sizeof(buf), stdin) != NULL)
                exit(0);
        }
 
        /* main loop */
        while (fgets(buf, sizeof(buf), stdin) != NULL)
-               syslog(pri, buf);
+               syslog(pri, "%s", buf);
 
        exit(0);
 }
 
 
        exit(0);
 }
 
-
-struct code {
-       char    *c_name;
-       int     c_val;
-};
-
-struct code    PriNames[] = {
-       "panic",        LOG_EMERG,
-       "emerg",        LOG_EMERG,
-       "alert",        LOG_ALERT,
-       "crit",         LOG_CRIT,
-       "err",          LOG_ERR,
-       "error",        LOG_ERR,
-       "warn",         LOG_WARNING,
-       "warning",      LOG_WARNING,
-       "notice",       LOG_NOTICE,
-       "info",         LOG_INFO,
-       "debug",        LOG_DEBUG,
-       NULL,           -1
-};
-
-struct code    FacNames[] = {
-       "kern",         LOG_KERN,
-       "user",         LOG_USER,
-       "mail",         LOG_MAIL,
-       "daemon",       LOG_DAEMON,
-       "auth",         LOG_AUTH,
-       "security",     LOG_AUTH,
-       "syslog",       LOG_SYSLOG,
-       "lpr",          LOG_LPR,
-       "news",         LOG_NEWS,
-       "uucp",         LOG_UUCP,
-       "local0",       LOG_LOCAL0,
-       "local1",       LOG_LOCAL1,
-       "local2",       LOG_LOCAL2,
-       "local3",       LOG_LOCAL3,
-       "local4",       LOG_LOCAL4,
-       "local5",       LOG_LOCAL5,
-       "local6",       LOG_LOCAL6,
-       "local7",       LOG_LOCAL7,
-       NULL,           -1
-};
-
+#define        SYSLOG_NAMES
+#include <syslog.h>
 
 /*
  *  Decode a symbolic name to a numeric value
  */
 
 /*
  *  Decode a symbolic name to a numeric value
  */
-
 pencode(s)
        register char *s;
 {
 pencode(s)
        register char *s;
 {
@@ -167,27 +142,32 @@ pencode(s)
        for (save = s; *s && *s != '.'; ++s);
        if (*s) {
                *s = '\0';
        for (save = s; *s && *s != '.'; ++s);
        if (*s) {
                *s = '\0';
-               fac = decode(save, FacNames);
-               if (fac < 0)
-                       bailout("unknown facility name: ", save);
+               fac = decode(save, facilitynames);
+               if (fac < 0) {
+                       (void)fprintf(stderr,
+                           "logger: unknown facility name: %s.\n", save);
+                       exit(1);
+               }
                *s++ = '.';
        }
        else {
                fac = 0;
                s = save;
        }
                *s++ = '.';
        }
        else {
                fac = 0;
                s = save;
        }
-       lev = decode(s, PriNames);
-       if (lev < 0)
-               bailout("unknown priority name: ", save);
+       lev = decode(s, prioritynames);
+       if (lev < 0) {
+               (void)fprintf(stderr,
+                   "logger: unknown priority name: %s.\n", save);
+               exit(1);
+       }
        return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
 }
 
        return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
 }
 
-
 decode(name, codetab)
        char *name;
 decode(name, codetab)
        char *name;
-       struct code *codetab;
+       CODE *codetab;
 {
 {
-       register struct code *c;
+       register CODE *c;
 
        if (isdigit(*name))
                return (atoi(name));
 
        if (isdigit(*name))
                return (atoi(name));
@@ -199,16 +179,9 @@ decode(name, codetab)
        return (-1);
 }
 
        return (-1);
 }
 
-bailout(msg, arg)
-       char *msg, *arg;
-{
-       fprintf(stderr, "logger: %s%s\n", msg, arg);
-       exit(1);
-}
-
 usage()
 {
 usage()
 {
-       fputs("logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n",
-           stderr);
+       (void)fprintf(stderr,
+           "logger: [-i] [-f file] [-p pri] [-t tag] [ message ... ]\n");
        exit(1);
 }
        exit(1);
 }