Mercurial > hg > toybox
annotate toys/touch.c @ 233:d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
toys/*.c. Move relevant info into comment at the top of each toys/*.c. Also
convert more of Makefile into a thin wrapper around shell scripts that actually
do the work. (Makefile is only still there for the user interface.)
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 19 Jan 2008 17:08:39 -0600 |
parents | bc87305c391f |
children | 163498bf547b |
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 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
9 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
|
10 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
|
11 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
|
12 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
13 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
|
14 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
218
diff
changeset
|
15 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
|
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 -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
|
18 -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
|
19 -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
|
20 -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
|
21 -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
|
22 -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
|
23 */ |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
24 |
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
25 #include "toys.h" |
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
26 |
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
|
27 #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
|
28 #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
|
29 #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
|
30 #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
|
31 #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
|
32 #define OPT_LENGTH 0x20 |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
163
diff
changeset
|
34 void touch_main(void) |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 { |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
36 char *arg; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
37 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
|
38 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
|
39 |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
40 set_a = !!(toys.optflags & OPT_ATIME); |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
41 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
|
42 |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
43 // Use timestamp on a file |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
44 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
|
45 struct stat sb; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
46 |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
47 if (toys.optflags & OPT_TIME) |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
48 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
|
49 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
|
50 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
|
51 curr_a = sb.st_atime; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
52 |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
53 // Use time specified on command line. |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
54 } 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
|
55 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
|
56 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
|
57 char *c; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
58 |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
59 curr = time(NULL); |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
60 if (localtime_r(&curr, &t) |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
61 || !(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
|
62 || *c || -1==(curr_a = curr_m = mktime(&t))) |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
63 { |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
64 error_exit("Unknown time %s", toy.touch.time); |
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 |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
67 // use current time |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
68 } 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
|
69 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
70 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
|
71 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
|
72 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
|
73 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
74 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
|
75 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
|
76 |
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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 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
|
84 } |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
85 } |
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 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
|
88 /* We've been asked to only change one */ |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
89 if (set_a) buf.modtime = sb.st_mtime; |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
90 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
|
91 } |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
92 |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
93 if (toys.optflags & OPT_LENGTH) |
163 | 94 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
|
95 goto error; |
160
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
96 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
|
97 error: |
160
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
98 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
|
99 } |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
100 } |