1 /* touch.c : change timestamp of a file
3 * Copyright 2012 Choubey Ji <warior.linux@gmail.com>
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html
7 USE_TOUCH(NEWTOY(touch, "<1acd:mr:t:h[!dtr]", TOYFLAG_BIN))
13 usage: touch [-amch] [-d DATE] [-t TIME] [-r FILE] FILE...
15 Update the access and modification times of each FILE to the current time.
18 -m change modification time
21 -d set time to DATE (in YYYY-MM-DDThh:mm:SS[.frac][tz] format)
22 -t set time to TIME (in [[CC]YY]MMDDhhmm[.ss][frac] format)
23 -r set time same as reference FILE
37 struct timespec ts[2];
41 // use current time if no -t or -d
42 ts[0].tv_nsec = UTIME_NOW;
43 if (toys.optflags & (FLAG_t|FLAG_d)) {
44 char *s, *date, **format;
48 // Initialize default values for time fields
52 // List of search types
53 if (toys.optflags & FLAG_d) {
54 format = (char *[]){"%Y-%m-%dT%T", "%Y-%m-%d %T", 0};
57 format = (char *[]){"%m%d%H%M", "%y%m%d%H%M", "%C%y%m%d%H%M", 0};
61 // Trailing Z means UTC timezone, don't expect libc to know this.
63 if (i && toupper(date[i-1])=='Z') {
65 setenv("TZ", "UTC0", 1);
69 if (toys.optflags&FLAG_t) {
70 s = strchr(date, '.');
71 if ((s ? s-date : strlen(date)) != strlen(*format)) {
76 localtime_r(&(ts->tv_sec), &tm);
77 // Adjusting for daylight savings time gives the wrong answer.
80 s = strptime(date, *format++, &tm);
83 if (s && *s=='.' && isdigit(s[1])) {
85 if (toys.optflags&FLAG_t)
86 if (1 == sscanf(s, "%2u%n", &(tm.tm_sec), &len)) s += len;
87 if (1 == sscanf(s, "%lu%n", &ts->tv_nsec, &len)) {
89 if (ts->tv_nsec > 999999999) s = 0;
90 else while (len++ < 9) ts->tv_nsec *= 10;
97 ts->tv_sec = mktime(&tm);
98 if (!s || *s || ts->tv_sec == -1) perror_exit("bad '%s'", date);
112 // Which time(s) should we actually change?
113 i = toys.optflags & (FLAG_a|FLAG_m);
114 if (i && i!=(FLAG_a|FLAG_m)) ts[i==FLAG_m].tv_nsec = UTIME_OMIT;
116 // Loop through files on command line
117 for (ss = toys.optargs; *ss;) {
120 if (!strcmp(s, "-")) {
121 if (!futimens(1, ts)) continue;
123 // cheat: FLAG_h is rightmost flag, so its value is 1
124 if (!utimensat(AT_FDCWD, s, ts,
125 (toys.optflags & FLAG_h)*AT_SYMLINK_NOFOLLOW)) continue;
126 if (toys.optflags & FLAG_c) continue;
127 if (access(s, F_OK) && (-1!=(fd = open(s, O_CREAT, 0666)))) {
129 if (toys.optflags) ss--;
133 perror_msg("'%s'", s);