]> granicus.if.org Git - sudo/commitdiff
Use strftime() instead of ctime() if it is available.
authorTodd C. Miller <Todd.Miller@courtesan.com>
Wed, 15 Sep 1999 09:15:33 +0000 (09:15 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Wed, 15 Sep 1999 09:15:33 +0000 (09:15 +0000)
TODO
config.h.in
configure
configure.in
logging.c

diff --git a/TODO b/TODO
index d41146eee8dc8565e34dcd3dcdd249ad98890515..1af881482f0a548b8f7b28cc1586e0f23c52b7f4 100644 (file)
--- a/TODO
+++ b/TODO
@@ -67,8 +67,10 @@ TODO list (most will be addressed in sudo 2.0)
 23) Make 'sudo -l user' if run as root do a "sudo -l" output for the specified
     user.
 
-24) Use strtol() and strtoul(), not atoi()?
+24) Use strtol() and strtoul(), not atoi()
 
 25) In parse.yacc get rid on unneeded '{ ; }'
 
 26) Look into %e, %p, %k in parse.lex
+
+27) Document Defaults stuff in sudoers.pod
index c52de6822b24fcb24b00e8d0e21c4356e700013a..5e113aa2952cc7e50df27e87219a6d0e041ffa65 100644 (file)
 /* Define if you have ftruncate(2).  */
 #undef HAVE_FTRUNCATE
 
+/* Define if you have strftime(2).  */
+#undef HAVE_STRFTIME
+
 /* Define if you have snprintf(3).  */
 #undef HAVE_SNPRINTF
 
index a43ac206710af30d76512ebb9a0a6862b4bc7cd2..7887fc8721abd53e13a5db880d761eb809eec55a 100755 (executable)
--- a/configure
+++ b/configure
@@ -5456,7 +5456,7 @@ EOF
 
 ;;
 esac
-for ac_func in strchr strrchr memchr memcpy memset sysconf sigaction tzset seteuid ftruncate
+for ac_func in strchr strrchr memchr memcpy memset sysconf sigaction tzset seteuid ftruncate strftime
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
 echo "configure:5463: checking for $ac_func" >&5
index bdb87c9abc97d18e83134a8073781d36464a5eb4..5e991585e01d8142a17098f718ce9026167934af 100644 (file)
@@ -1395,7 +1395,7 @@ esac
 dnl
 dnl Function checks
 dnl
-AC_CHECK_FUNCS(strchr strrchr memchr memcpy memset sysconf sigaction tzset seteuid ftruncate)
+AC_CHECK_FUNCS(strchr strrchr memchr memcpy memset sysconf sigaction tzset seteuid ftruncate strftime)
 if test -n "$SECUREWARE"; then
     AC_CHECK_FUNCS(bigcrypt)
     AC_CHECK_FUNCS(set_auth_parameters)
index 3e6fb3b25364e4c7d8375bd36db27da852a6c381..b086f858f9a00615e4ddeba95f9f15b760d4e5da 100644 (file)
--- a/logging.c
+++ b/logging.c
@@ -66,6 +66,7 @@ static void do_syslog         __P((int, char *));
 static void do_logfile         __P((char *));
 static void send_mail          __P((char *));
 static void mail_auth          __P((int, char *));
+static char *get_timestr       __P((void));
 
 #ifdef BROKEN_SYSLOG
 # define MAXSYSLOGTRIES        16      /* num of retries for broken syslogs */
@@ -149,15 +150,12 @@ static void
 do_logfile(msg)
     char *msg;
 {
-    char *full_line, *timestr;
+    char *full_line;
     char *beg, *oldend, *end;
     FILE *fp;
     mode_t oldmask;
-    time_t now;
     int maxlen = sudo_inttable[I_LOGLEN];
 
-    now = time((time_t) 0);
-
     oldmask = umask(077);
     fp = fopen(sudo_strtable[I_LOGFILE], "a");
     (void) umask(oldmask);
@@ -172,25 +170,21 @@ do_logfile(msg)
        send_mail(full_line);
        free(full_line);
     } else {
-       timestr = ctime(&now) + 4;              /* skip day of the week */
-       if (sudo_flag_set(FL_LOG_YEAR))
-           timestr[20] = '\0';                 /* avoid the newline */
-       else
-           timestr[15] = '\0';                 /* don't care about year */
-
        if (sudo_inttable[I_LOGLEN] == 0) {
            /* Don't pretty-print long log file lines (hard to grep) */
            if (sudo_flag_set(FL_LOG_HOST))
-               (void) fprintf(fp, "%s : %s : HOST=%s : %s\n", timestr,
+               (void) fprintf(fp, "%s : %s : HOST=%s : %s\n", get_timestr(),
                    user_name, user_shost, msg);
            else
-               (void) fprintf(fp, "%s : %s : %s\n", timestr, user_name, msg);
+               (void) fprintf(fp, "%s : %s : %s\n", get_timestr(),
+                   user_name, msg);
        } else {
            if (sudo_flag_set(FL_LOG_HOST))
-               easprintf(&full_line, "%s : %s : HOST=%s : %s", timestr,
+               easprintf(&full_line, "%s : %s : HOST=%s : %s", get_timestr(),
                    user_name, user_shost, msg);
            else
-               easprintf(&full_line, "%s : %s : %s", timestr, user_name, msg);
+               easprintf(&full_line, "%s : %s : %s", get_timestr(),
+                   user_name, msg);
 
            /*
             * Print out full_line with word wrap
@@ -405,7 +399,6 @@ send_mail(line)
     FILE *mail;
     char *p;
     int pfd[2], pid;
-    time_t now;
 
     /* Just return if mailer is disabled. */
     if (!sudo_strtable[I_MAILERPATH])
@@ -467,14 +460,8 @@ send_mail(line)
            } else
                (void) fputc(*p, mail);
        }
-       now = time((time_t) 0);
-       p = ctime(&now) + 4;
-       if (sudo_flag_set(FL_LOG_YEAR))
-           p[20] = '\0';                       /* avoid the newline */
-       else
-           p[15] = '\0';                       /* don't care about year */
-       (void) fprintf(mail, "\n\n%s : %s : %s : %s\n\n", user_host, p,
-           user_name, line);
+       (void) fprintf(mail, "\n\n%s : %s : %s : %s\n\n", user_host,
+           get_timestr(), user_name, line);
        fclose(mail);
        reapchild(0);
        _exit(0);
@@ -536,3 +523,39 @@ reapchild(sig)
 #endif /* POSIX_SIGNALS */
     errno = serrno;
 }
+
+/*
+ * Return an ascii string with the current date + time
+ * Uses strftime() if available, else falls back to ctime().
+ */
+static char *
+get_timestr()
+{
+    char *s;
+    time_t now = time((time_t) 0);
+#ifdef HAVE_STRFTIME
+    static char buf[128];
+    struct tm *timeptr;
+
+    timeptr = localtime(&now);
+    if (sudo_flag_set(FL_LOG_YEAR))
+       s = "%h %e %T %Y";
+    else
+       s = "%h %e %T";
+
+    /* strftime() does not guarantee to NUL-terminate so we must check. */
+    buf[sizeof(buf) - 1] = '\0';
+    if (strftime(buf, sizeof(buf), s, timeptr) && !buf[sizeof(buf) - 1])
+       return(buf);
+
+#else
+
+    s = ctime(&now) + 4;               /* skip day of the week */
+    if (sudo_flag_set(FL_LOG_YEAR))
+       s[20] = '\0';                   /* avoid the newline */
+    else
+       s[15] = '\0';                   /* don't care about year */
+
+    return(s);
+#endif /* HAVE_STRFTIME */
+}