annotate toys/touch.c @ 188:6a12feedd893

Minor cleanups/tweaks to touch.
author Rob Landley <rob@landley.net>
date Fri, 30 Nov 2007 04:37:24 -0600
parents 25447caf1b4b
children 30a6db5a95c2
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>
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 */
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
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
8 #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
9 #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
10 #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
11 #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
12 #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
13 #include <time.h>
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 #include "toys.h"
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
16 #define OPT_MTIME 0x01
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
17 #define OPT_NOCREATE 0x02
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
18 #define OPT_ATIME 0x04
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
19 #define OPT_REFERENCE 0x08
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
20 #define OPT_TIME 0x10
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
21 #define OPT_LENGTH 0x20
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
22
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 163
diff changeset
23 void touch_main(void)
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 {
147
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
25 char *arg;
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
26 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
27 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
28
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
29 set_a = !!(toys.optflags & OPT_ATIME);
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
30 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
31
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
32 // Use timestamp on a file
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
33 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
34 struct stat sb;
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
35
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
36 if (toys.optflags & OPT_TIME)
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
37 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
38 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
39 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
40 curr_a = sb.st_atime;
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
41
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
42 // Use time specified on command line.
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
43 } 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
44 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
45 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
46 char *c;
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
47
147
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
48 curr = time(NULL);
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
49 if (localtime_r(&curr, &t)
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
50 || !(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
51 || *c || -1==(curr_a = curr_m = mktime(&t)))
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 error_exit("Unknown time %s", toy.touch.time);
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
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
56 // use current time
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
57 } 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
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 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
60 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
61 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
62
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
63 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
64 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
65
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
66 if (stat(arg, &sb) == -1) {
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
67 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
68 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
69 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
70 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
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 }
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
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
75 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
76 /* We've been asked to only change one */
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
77 if (set_a) buf.modtime = sb.st_mtime;
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
78 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
79 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
80
188
6a12feedd893 Minor cleanups/tweaks to touch.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
81 if (toys.optflags & OPT_LENGTH)
163
a913f894b7d8 Simplify touch -l slightly.
Rob Landley <rob@landley.net>
parents: 162
diff changeset
82 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
83 goto error;
160
2eb41e7bf180 Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents: 147
diff changeset
84 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
85 error:
160
2eb41e7bf180 Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents: 147
diff changeset
86 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
87 }
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 }