From: Todd C. Miller Date: Wed, 15 Sep 1999 09:15:33 +0000 (+0000) Subject: Use strftime() instead of ctime() if it is available. X-Git-Tag: SUDO_1_6_0~52 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f7e23611be8f589e35dd74a28386378237d3eddd;p=sudo Use strftime() instead of ctime() if it is available. --- diff --git a/TODO b/TODO index d41146eee..1af881482 100644 --- 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 diff --git a/config.h.in b/config.h.in index c52de6822..5e113aa29 100644 --- a/config.h.in +++ b/config.h.in @@ -308,6 +308,9 @@ /* 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 diff --git a/configure b/configure index a43ac2067..7887fc872 100755 --- 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 diff --git a/configure.in b/configure.in index bdb87c9ab..5e991585e 100644 --- a/configure.in +++ b/configure.in @@ -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) diff --git a/logging.c b/logging.c index 3e6fb3b25..b086f858f 100644 --- 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 */ +}