Mercurial > hg > toybox
annotate toys/touch.c @ 234:163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 19 Jan 2008 17:43:27 -0600 |
parents | d4176f3f3835 |
children | 7cb15eae1664 |
rev | line source |
---|---|
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
1 /* vi: set sw=4 ts=4: |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
2 * |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
3 * touch.c - Modify a file's timestamps. |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
4 * |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
5 * Copyright (C) 2007 Charlie Shepherd <masterdriverz@gentoo.org> |
194
30a6db5a95c2
Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents:
188
diff
changeset
|
6 * |
198
3227c5316260
Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents:
194
diff
changeset
|
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/touch.html |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
8 |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
9 USE_TOUCH(NEWTOY(touch, "l#t:r:mca", TOYFLAG_BIN)) |
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
10 |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
11 config TOUCH |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
12 bool "touch" |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
13 default y |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
14 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
15 usage: touch [-acm] [-r FILE] [-t MMDDhhmm] [-l bytes] FILE... |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
16 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
17 Change file timestamps, ensure file existance and change file length. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
18 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
19 -a Only change the access time. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
20 -c Do not create the file if it doesn't exist. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
21 -l Length to truncate (or sparsely extend) file to. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
22 -m Only change the modification time. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
23 -r Reference file to take timestamps from. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
24 -t Time to change {a,m}time to. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
25 */ |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 |
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
27 #include "toys.h" |
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
28 |
218
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
29 #define OPT_MTIME 0x01 |
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
30 #define OPT_NOCREATE 0x02 |
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
31 #define OPT_ATIME 0x04 |
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
32 #define OPT_REFERENCE 0x08 |
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
33 #define OPT_TIME 0x10 |
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
34 #define OPT_LENGTH 0x20 |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
163
diff
changeset
|
36 void touch_main(void) |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
37 { |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
38 char *arg; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
39 int i, set_a, set_m; |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
40 time_t curr_a, curr_m; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
41 |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
42 set_a = !!(toys.optflags & OPT_ATIME); |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
43 set_m = !!(toys.optflags & OPT_MTIME); |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
44 |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
45 // Use timestamp on a file |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
46 if (toys.optflags & OPT_REFERENCE) { |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
47 struct stat sb; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
48 |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
49 if (toys.optflags & OPT_TIME) |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
50 error_exit("Redundant time source"); |
160
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
51 xstat(toy.touch.ref_file, &sb); |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
52 curr_m = sb.st_mtime; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
53 curr_a = sb.st_atime; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
54 |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
55 // Use time specified on command line. |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
56 } else if (toys.optflags & OPT_TIME) { |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
57 struct tm t; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
58 time_t curr; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
59 char *c; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
60 |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
61 curr = time(NULL); |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
62 if (localtime_r(&curr, &t) |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
63 || !(c = strptime(toy.touch.time, "%m%d%H%M", &t)) |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
64 || *c || -1==(curr_a = curr_m = mktime(&t))) |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
65 { |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
66 error_exit("Unknown time %s", toy.touch.time); |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
67 } |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
68 |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
69 // use current time |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
70 } else curr_m = curr_a = time(NULL); |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
71 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
72 for (i = 0; (arg = toys.optargs[i]); i++) { |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
73 struct utimbuf buf; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
74 struct stat sb; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
75 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
76 buf.modtime = curr_m; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
77 buf.actime = curr_a; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
78 |
218
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
79 if (stat(arg, &sb)) { |
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
80 if (!(toys.optflags & OPT_NOCREATE)) { |
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
81 int temp = umask(0); |
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
82 xcreate(arg, O_CREAT, 0644); |
bc87305c391f
Make touch work reliably when file doesn't exist and clean up headers a bit.
Rob Landley <rob@landley.net>
parents:
198
diff
changeset
|
83 if (CFG_TOYBOX_FREE) umask(temp); |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
84 if (stat(arg, &sb)) |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
85 goto error; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
86 } |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
87 } |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
88 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
89 if ((set_a+set_m) == 1) { |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
90 /* We've been asked to only change one */ |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
91 if (set_a) buf.modtime = sb.st_mtime; |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
92 else if (set_m) buf.actime = sb.st_atime; |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
93 } |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
94 |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
95 if (toys.optflags & OPT_LENGTH) |
163 | 96 if (truncate(arg, toy.touch.length)) |
162
0864aec90026
Add an option to let touch extend or truncate a file and rename the err label to time_error to reduce confusion.
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
161
diff
changeset
|
97 goto error; |
160
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
98 if (utime(arg, &buf)) |
162
0864aec90026
Add an option to let touch extend or truncate a file and rename the err label to time_error to reduce confusion.
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
161
diff
changeset
|
99 error: |
160
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
100 perror_exit(arg); |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
101 } |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
102 } |