BSD 4_3_Reno release
[unix-history] / usr / src / usr.bin / logger / logger.c
index afe2a31..413636a 100644 (file)
@@ -3,11 +3,18 @@
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
  * 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.
+ * provided that: (1) source distributions retain this entire copyright
+ * notice and comment, and (2) distributions including binaries display
+ * the following acknowledgement:  ``This product includes software
+ * developed by the University of California, Berkeley and its contributors''
+ * in the documentation or other materials provided with the distribution
+ * and in all advertising materials mentioning features or use of this
+ * software. 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 ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -17,7 +24,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.14 (Berkeley) 6/1/90";
 #endif /* not lint */
 
 #include <stdio.h>
 #endif /* not lint */
 
 #include <stdio.h>
@@ -36,18 +43,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 +64,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 +86,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,7 +128,7 @@ 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);
+               fac = decode(save, facilitynames);
                if (fac < 0)
                        bailout("unknown facility name: ", save);
                *s++ = '.';
                if (fac < 0)
                        bailout("unknown facility name: ", save);
                *s++ = '.';
@@ -176,7 +137,7 @@ pencode(s)
                fac = 0;
                s = save;
        }
                fac = 0;
                s = save;
        }
-       lev = decode(s, PriNames);
+       lev = decode(s, prioritynames);
        if (lev < 0)
                bailout("unknown priority name: ", save);
        return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
        if (lev < 0)
                bailout("unknown priority name: ", save);
        return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
@@ -185,9 +146,9 @@ pencode(s)
 
 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));