diff options
author | Ken Sumrall <ksumrall@android.com> | 2013-06-28 02:46:09 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2013-06-28 02:46:09 +0000 |
commit | 3b2b2b51376b1cf9da21155dacd8d9df106bf92d (patch) | |
tree | dc75638492940731b83cb4ffd647b92e9fe93555 /toolbox | |
parent | eecf40fce8d502a9b35f79d73bba708bc35c9632 (diff) | |
parent | 13495a1cbb6c6b7ec617488f272bc02f2107a63c (diff) | |
download | system_core-3b2b2b51376b1cf9da21155dacd8d9df106bf92d.zip system_core-3b2b2b51376b1cf9da21155dacd8d9df106bf92d.tar.gz system_core-3b2b2b51376b1cf9da21155dacd8d9df106bf92d.tar.bz2 |
Merge "Update the touch command to take a human readable timestamp"
Diffstat (limited to 'toolbox')
-rw-r--r-- | toolbox/touch.c | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/toolbox/touch.c b/toolbox/touch.c index b8ab310..52ddf2a 100644 --- a/toolbox/touch.c +++ b/toolbox/touch.c @@ -5,13 +5,40 @@ #include <sys/stat.h> #include <stdlib.h> #include <fcntl.h> +#include <time.h> static void usage(void) { - fprintf(stderr, "touch: usage: touch [-alm] [-t time_t] <file>\n"); + fprintf(stderr, "touch: usage: touch [-alm] [-t YYYYMMDD[.hhmmss]] <file>\n"); exit(1); } +static time_t parse_time(char *s) +{ + struct tm tm; + int day = atoi(s); + int hour = 0; + + while (*s && *s != '.') { + s++; + } + + if (*s) { + s++; + hour = atoi(s); + } + + tm.tm_year = day / 10000 - 1900; + tm.tm_mon = (day % 10000) / 100 - 1; + tm.tm_mday = day % 100; + tm.tm_hour = hour / 10000; + tm.tm_min = (hour % 10000) / 100; + tm.tm_sec = hour % 100; + tm.tm_isdst = -1; + + return mktime(&tm); +} + int touch_main(int argc, char *argv[]) { int i, fd, aflag = 0, mflag = 0, debug = 0, flags = 0; @@ -31,9 +58,9 @@ int touch_main(int argc, char *argv[]) case 't': if ((i+1) >= argc) usage(); - specified_time.tv_sec = atol(argv[++i]); - if (specified_time.tv_sec == 0) { - fprintf(stderr, "touch: invalid time_t\n"); + specified_time.tv_sec = parse_time(argv[++i]); + if (specified_time.tv_sec == -1) { + fprintf(stderr, "touch: invalid timestamp specified\n"); exit(1); } specified_time.tv_nsec = 0; |