libbb: introduce and use strftime_[YYYYMMDD]HHMMSS()
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 29 Mar 2013 11:30:33 +0000 (12:30 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 29 Mar 2013 11:30:33 +0000 (12:30 +0100)
function                                             old     new   delta
strftime_fmt                                           -      53     +53
strftime_YYYYMMDDHHMMSS                                -      12     +12
strftime_HHMMSS                                        -      12     +12
human_time                                            44      43      -1
fmtstr_t                                               9       -      -9
step_time                                            361     345     -16
watch_main                                           261     232     -29
------------------------------------------------------------------------------
(add/remove: 3/1 grow/shrink: 0/3 up/down: 77/-55)             Total: 22 bytes
   text    data     bss     dec     hex filename
 919203     932   17692  937827   e4f63 busybox_old
 919209     932   17692  937833   e4f69 busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
coreutils/stat.c
include/libbb.h
libbb/login.c
libbb/time.c
networking/ntpd.c
networking/ntpd_simple.c
procps/watch.c

index c8677ebaa93b71705b510bbff8dd04296c7c4b9d..dc9d81c357cea78c01129cf44f1dd486d2c9316f 100644 (file)
@@ -127,7 +127,7 @@ static const char *human_time(time_t t)
        /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
 #define buf bb_common_bufsiz1
 
-       strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S.000000000", localtime(&t));
+       strcpy(strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &t), ".000000000");
        return buf;
 #undef buf
 }
index 79a37a75983b075b091c22593b0844015ef0c4aa..6dd4d7caed768c5139e69e5324a037dd4e334674 100644 (file)
@@ -523,7 +523,8 @@ struct BUG_too_small {
 
 void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC;
 time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC;
-
+char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
+char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
 
 int xsocket(int domain, int type, int protocol) FAST_FUNC;
 void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) FAST_FUNC;
index 8a82c6add71041d8df959439053a0a6a621d6e31..8f080b77549c19dbde6bd6257ac31edcc1bb5952 100644 (file)
@@ -16,7 +16,6 @@
 #define LOGIN " login: "
 
 static const char fmtstr_d[] ALIGN1 = "%A, %d %B %Y";
-static const char fmtstr_t[] ALIGN1 = "%H:%M:%S";
 
 void FAST_FUNC print_login_issue(const char *issue_file, const char *tty)
 {
@@ -73,7 +72,7 @@ void FAST_FUNC print_login_issue(const char *issue_file, const char *tty)
                                strftime(buf, sizeof(buf), fmtstr_d, localtime(&t));
                                break;
                        case 't':
-                               strftime(buf, sizeof(buf), fmtstr_t, localtime(&t));
+                               strftime_HHMMSS(buf, sizeof(buf), &t);
                                break;
                        case 'l':
                                outbuf = tty;
index e2b938471357a264e34ebe08dbf750a5d5080d1f..57e14b66c6d5be590ba2218e49a8a4b29c5a685f 100644 (file)
@@ -187,6 +187,27 @@ time_t FAST_FUNC validate_tm_time(const char *date_str, struct tm *ptm)
        return t;
 }
 
+static char* strftime_fmt(char *buf, unsigned len, time_t *tp, const char *fmt)
+{
+       time_t t;
+       if (!tp) {
+               tp = &t;
+               time(tp);
+       }
+       /* Returns pointer to NUL */
+       return buf + strftime(buf, len, fmt, localtime(tp));
+}
+
+char* FAST_FUNC strftime_HHMMSS(char *buf, unsigned len, time_t *tp)
+{
+       return strftime_fmt(buf, len, tp, "%H:%M:%S");
+}
+
+char* FAST_FUNC strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp)
+{
+       return strftime_fmt(buf, len, tp, "%Y-%m-%d %H:%M:%S");
+}
+
 #if ENABLE_MONOTONIC_SYSCALL
 
 #include <sys/syscall.h>
index 79b7c379361f615c1ab49d106cdec283b527385e..0f4319ef206611d287e629ff80fb848bcf1ab459 100644 (file)
@@ -887,11 +887,11 @@ step_time(double offset)
 
        VERB2 {
                tval = tvc.tv_sec;
-               strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval));
+               strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval);
                bb_error_msg("current time is %s.%06u", buf, (unsigned)tvc.tv_usec);
        }
        tval = tvn.tv_sec;
-       strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&tval));
+       strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval);
        bb_error_msg("setting time to %s.%06u (offset %+fs)", buf, (unsigned)tvn.tv_usec, offset);
 
        /* Correct various fields which contain time-relative values: */
index 55bded8ffe38b05335cb572fb245eae16e364bb3..3e7fc47199667b73cc4199531ee761d8f5dc8bfa 100644 (file)
@@ -378,7 +378,7 @@ step_time_once(double offset)
                bb_perror_msg_and_die("settimeofday");
 
        tval = tv.tv_sec;
-       strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval));
+       strftime_YYYYMMDDHHMMSS(buf, sizeof(buf), &tval);
 
        bb_error_msg("setting clock to %s (offset %fs)", buf, offset);
 
index 36af1cca7ccdb878f1f8eefcbe71b78a227fe3fe..0397f21bf2b01a77509920d609188ff85f4e8efb 100644 (file)
@@ -69,7 +69,6 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
                printf("\033[H""\033[J");
                if (!(opt & 0x2)) { // no -t
                        const unsigned time_len = sizeof("1234-67-90 23:56:89");
-                       time_t t;
 
                        // STDERR_FILENO is procps3 compat:
                        // "watch ls 2>/dev/null" does not detect tty size
@@ -79,10 +78,13 @@ int watch_main(int argc UNUSED_PARAM, char **argv)
                                free(header);
                                header = xasprintf("Every %us: %-*s", period, (int)width, cmd);
                        }
-                       time(&t);
-                       if (time_len < width)
-                               strftime(header + width - time_len, time_len,
-                                       "%Y-%m-%d %H:%M:%S", localtime(&t));
+                       if (time_len < width) {
+                               strftime_YYYYMMDDHHMMSS(
+                                       header + width - time_len,
+                                       time_len,
+                                       /*time_t*:*/ NULL
+                               );
+                       }
 
                        // compat: empty line between header and cmd output
                        printf("%s\n\n", header);