annotate toys/posix/touch.c @ 1776:7bf68329eb3b draft default tip

Repository switched to git at https://github.com/landley/toybox
author Rob Landley <rob@landley.net>
date Thu, 09 Apr 2015 02:28:32 -0500
parents 542b55c0f723
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
1 /* touch.c : change timestamp of a file
699
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
700
ad6bff0c9169 Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents: 699
diff changeset
3 * Copyright 2012 Choubey Ji <warior.linux@gmail.com>
699
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
6
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
7 USE_TOUCH(NEWTOY(touch, "acd:mr:t:h[!dtr]", TOYFLAG_BIN))
699
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 config TOUCH
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
10 bool "touch"
699
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 default y
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 help
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
13 usage: touch [-amch] [-d DATE] [-t TIME] [-r FILE] FILE...
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
14
699
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 Update the access and modification times of each FILE to the current time.
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
16
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
17 -a change access time
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
18 -m change modification time
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
19 -c don't create file
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
20 -h change symlink
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
21 -d set time to DATE (in YYYY-MM-DDThh:mm:SS[.frac][tz] format)
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
22 -t set time to TIME (in [[CC]YY]MMDDhhmm[.ss][frac] format)
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
23 -r set time same as reference FILE
699
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 */
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
25
702
9fb5fe6eb13f More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents: 701
diff changeset
26 #define FOR_touch
699
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 #include "toys.h"
26ea643e1de1 Add touch from Choubey Ji.
Rob Landley <rob@landley.net>
parents:
diff changeset
28
702
9fb5fe6eb13f More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents: 701
diff changeset
29 GLOBALS(
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
30 char *time;
702
9fb5fe6eb13f More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents: 701
diff changeset
31 char *file;
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
32 char *date;
702
9fb5fe6eb13f More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents: 701
diff changeset
33 )
700
ad6bff0c9169 Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents: 699
diff changeset
34
ad6bff0c9169 Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents: 699
diff changeset
35 void touch_main(void)
ad6bff0c9169 Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents: 699
diff changeset
36 {
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
37 struct timespec ts[2];
1478
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
38 char **ss;
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
39 int fd, i;
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
40
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
41 // use current time if no -t or -d
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
42 ts[0].tv_nsec = UTIME_NOW;
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
43 if (toys.optflags & (FLAG_t|FLAG_d)) {
1478
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
44 char *s, *date;
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
45 struct tm tm;
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
46 int len = 0;
1478
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
47
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
48 localtime_r(&(ts->tv_sec), &tm);
1478
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
49
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
50 // Set time from -d?
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
51
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
52 if (toys.optflags & FLAG_d) {
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
53 date = TT.date;
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
54 i = strlen(date);
1478
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
55 if (i) {
1511
df792340e3f7 Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents: 1478
diff changeset
56 // Trailing Z means UTC timezone, don't expect libc to know this.
df792340e3f7 Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents: 1478
diff changeset
57 if (toupper(date[i-1])=='Z') {
df792340e3f7 Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents: 1478
diff changeset
58 date[i-1] = 0;
df792340e3f7 Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents: 1478
diff changeset
59 setenv("TZ", "UTC0", 1);
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
60 localtime_r(&(ts->tv_sec), &tm);
1511
df792340e3f7 Bugfix from Ashwini Sharma: Z timezone required by posix for touch but not for libc, so we have to implement it here.
Rob Landley <rob@landley.net>
parents: 1478
diff changeset
61 }
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
62 s = strptime(date, "%Y-%m-%dT%T", &tm);
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
63 if (s && *s=='.' && isdigit(s[1]))
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
64 sscanf(s, ".%lu%n", &ts->tv_nsec, &len);
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
65 else len = 0;
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
66 } else s = 0;
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
67
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
68 // Set time from -t?
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
69
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
70 } else {
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
71 strcpy(toybuf, "%Y%m%d%H%M");
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
72 date = TT.time;
1657
910a958980dc Make touch -d miliseconds work. (Still only 6 digits precision instead of 9, but it's a start.)
Rob Landley <rob@landley.net>
parents: 1511
diff changeset
73 i = ((s = strchr(date, '.'))) ? s-date : strlen(date);
910a958980dc Make touch -d miliseconds work. (Still only 6 digits precision instead of 9, but it's a start.)
Rob Landley <rob@landley.net>
parents: 1511
diff changeset
74 if (i < 8 || i%2) error_exit("bad '%s'", date);
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
75 for (i=0;i<3;i++) {
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
76 s = strptime(date, toybuf+(i&2), &tm);
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
77 if (s) break;
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
78 toybuf[1]='y';
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
79 }
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
80 if (s && *s=='.' && sscanf(s, ".%2u%n", &(tm.tm_sec), &len) == 1) {
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
81 sscanf(s += len, "%lu%n", &ts->tv_nsec, &len);
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
82 len++;
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
83 } else len = 0;
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
84 }
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
85 if (len) {
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
86 s += len;
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
87 if (ts->tv_nsec > 999999999) s = 0;
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
88 else while (len++ < 10) ts->tv_nsec *= 10;
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
89 }
700
ad6bff0c9169 Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents: 699
diff changeset
90
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
91 errno = 0;
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
92 ts->tv_sec = mktime(&tm);
1478
0d19abb90c52 Minor cleanup pass on touch.
Rob Landley <rob@landley.net>
parents: 1063
diff changeset
93 if (!s || *s || errno == EOVERFLOW) perror_exit("bad '%s'", date);
700
ad6bff0c9169 Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents: 699
diff changeset
94 }
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
95 ts[1]=ts[0];
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
96
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
97 // Set time from -r?
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
98
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
99 if (TT.file) {
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
100 struct stat st;
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
101
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
102 xstat(TT.file, &st);
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
103 ts[0] = st.st_atim;
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
104 ts[1] = st.st_mtim;
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
105 }
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
106
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
107 // Which time(s) should we actually change?
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
108 i = toys.optflags & (FLAG_a|FLAG_m);
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
109 if (i && i!=(FLAG_a|FLAG_m)) ts[i==FLAG_m].tv_nsec = UTIME_OMIT;
1063
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
110
51f0dda223b8 Minor cleanups.
Rob Landley <rob@landley.net>
parents: 1062
diff changeset
111 // Loop through files on command line
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
112 for (ss = toys.optargs; *ss;) {
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
113
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
114 // cheat: FLAG_h is rightmost flag, so its value is 1
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
115 if (!utimensat(AT_FDCWD, *ss, ts,
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
116 (toys.optflags & FLAG_h)*AT_SYMLINK_NOFOLLOW)) ss++;
711
dcc6136e6659 Update touch: add remaining posix arguments, make existing ones work.
Rob Landley <rob@landley.net>
parents: 702
diff changeset
117 else if (toys.optflags & FLAG_c) ss++;
1685
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
118 else if (access(*ss, F_OK) && (-1!=(fd = open(*ss, O_CREAT, 0666))))
542b55c0f723 Rewrite of a patch from Elliott Hughes to implement touch -h and switch API to utimensat() which offers nanosecond granularity and UTIME_OMIT/UTIME_NOW.
Rob Landley <rob@landley.net>
parents: 1657
diff changeset
119 close(fd);
780
6cc69be43c42 Have error_msg() and friends set TT.exitval to 1 if it's still 0, clean out other places that were setting it that no longer need to.
Rob Landley <rob@landley.net>
parents: 711
diff changeset
120 else perror_msg("'%s'", *ss++);
700
ad6bff0c9169 Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents: 699
diff changeset
121 }
ad6bff0c9169 Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents: 699
diff changeset
122 }