Skip to content

Commit

Permalink
Update the touch command to take a human readable timestamp
Browse files Browse the repository at this point in the history
Now the -t option to the toolbox touch command takes a timestamp
in the form of YYYYMMDD.hhmmss

Change-Id: I3812700edaa1a06590a07c15b050721b49e9b7e0
  • Loading branch information
Ken Sumrall committed Jun 27, 2013
1 parent 945ff92 commit 13495a1
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions toolbox/touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 13495a1

Please sign in to comment.