Mercurial > hg > toybox
annotate toys/touch.c @ 226:6aac63925eff
Update web pages.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 05 Jan 2008 18:09:49 -0600 |
parents | bc87305c391f |
children | d4176f3f3835 |
rev | line source |
---|---|
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* vi: set sw=4 ts=4: */ |
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
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 |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
8 */ |
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 |
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 #include "toys.h" |
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 |
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
|
12 #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
|
13 #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
|
14 #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
|
15 #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
|
16 #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
|
17 #define OPT_LENGTH 0x20 |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
18 |
186
25447caf1b4b
Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents:
163
diff
changeset
|
19 void touch_main(void) |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 { |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
21 char *arg; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
22 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
|
23 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
|
24 |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
25 set_a = !!(toys.optflags & OPT_ATIME); |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
26 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
|
27 |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
28 // Use timestamp on a file |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
29 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
|
30 struct stat sb; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
31 |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
32 if (toys.optflags & OPT_TIME) |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 curr_a = sb.st_atime; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
37 |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
38 // Use time specified on command line. |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
39 } 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
|
40 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
|
41 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
|
42 char *c; |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
43 |
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 curr = time(NULL); |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
45 if (localtime_r(&curr, &t) |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
46 || !(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
|
47 || *c || -1==(curr_a = curr_m = mktime(&t))) |
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 error_exit("Unknown time %s", toy.touch.time); |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
50 } |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
51 |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
52 // use current time |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
53 } 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
|
54 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
55 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
|
56 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
|
57 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
|
58 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
59 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
|
60 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
|
61 |
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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
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 } |
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 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
|
73 /* We've been asked to only change one */ |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
74 if (set_a) buf.modtime = sb.st_mtime; |
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
75 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
|
76 } |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
77 |
188
6a12feedd893
Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
78 if (toys.optflags & OPT_LENGTH) |
163 | 79 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
|
80 goto error; |
160
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
81 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
|
82 error: |
160
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
83 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
|
84 } |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
85 } |