annotate toys/touch.c @ 198:3227c5316260

Update links and add some more spec comments.
author Rob Landley <rob@landley.net>
date Tue, 11 Dec 2007 15:41:31 -0600
parents 30a6db5a95c2
children bc87305c391f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
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
10 #define _XOPEN_SOURCE 600
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
11 #include <unistd.h>
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
12 #include <sys/types.h>
147
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
13 #include <sys/stat.h>
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
14 #include <utime.h>
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
15 #include <time.h>
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 #include "toys.h"
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
18 #define OPT_MTIME 0x01
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
19 #define OPT_NOCREATE 0x02
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
20 #define OPT_ATIME 0x04
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
21 #define OPT_REFERENCE 0x08
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
22 #define OPT_TIME 0x10
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
23 #define OPT_LENGTH 0x20
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 163
diff changeset
25 void touch_main(void)
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 {
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 char *arg;
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
28 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
29 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
30
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
31 set_a = !!(toys.optflags & OPT_ATIME);
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
32 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
33
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
34 // Use timestamp on a file
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
35 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
36 struct stat sb;
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 if (toys.optflags & OPT_TIME)
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
39 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
40 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
41 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
42 curr_a = sb.st_atime;
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
43
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
44 // Use time specified on command line.
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
45 } 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
46 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
47 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
48 char *c;
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
49
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 = time(NULL);
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
51 if (localtime_r(&curr, &t)
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
52 || !(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
53 || *c || -1==(curr_a = curr_m = mktime(&t)))
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 error_exit("Unknown time %s", toy.touch.time);
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
56 }
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
57
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
58 // use current time
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
59 } 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
60
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
61 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
62 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
63 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
64
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
65 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
66 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
67
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
68 if (stat(arg, &sb) == -1) {
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
69 if (!(toys.optflags & OPT_NOCREATE) && errno == ENOENT) {
161
b4c79ab09f9e Don't error on stat success, and create files with a sensible mode instead of 000
Charlie Shepherd <masterdriverz@gentoo.org>
parents: 160
diff changeset
70 if (creat(arg, 0644))
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 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
72 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
73 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
74 }
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
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
77 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
78 /* We've been asked to only change one */
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
79 if (set_a) buf.modtime = sb.st_mtime;
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
80 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
81 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
82
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
83 if (toys.optflags & OPT_LENGTH)
163
a913f894b7d8 Simplify touch -l slightly.
Rob Landley <rob@landley.net>
parents: 162
diff changeset
84 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
85 goto error;
160
2eb41e7bf180 Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents: 147
diff changeset
86 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
87 error:
160
2eb41e7bf180 Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents: 147
diff changeset
88 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
89 }
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 }