comparison 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
comparison
equal deleted inserted replaced
187:c983a0af6d4e 188:6a12feedd893
11 #include <sys/stat.h> 11 #include <sys/stat.h>
12 #include <utime.h> 12 #include <utime.h>
13 #include <time.h> 13 #include <time.h>
14 #include "toys.h" 14 #include "toys.h"
15 15
16 #define MTIME 0x01 16 #define OPT_MTIME 0x01
17 #define NO_CREATE 0x02 17 #define OPT_NOCREATE 0x02
18 #define ATIME 0x04 18 #define OPT_ATIME 0x04
19 #define REFERENCE 0x08 19 #define OPT_REFERENCE 0x08
20 #define TIME 0x10 20 #define OPT_TIME 0x10
21 #define LENGTH 0x20 21 #define OPT_LENGTH 0x20
22 22
23 void touch_main(void) 23 void touch_main(void)
24 { 24 {
25 char *arg; 25 char *arg;
26 int i, set_a, set_m, create; 26 int i, set_a, set_m;
27 time_t curr_a, curr_m; 27 time_t curr_a, curr_m;
28 28
29 set_a = !!(toys.optflags & ATIME); 29 set_a = !!(toys.optflags & OPT_ATIME);
30 set_m = !!(toys.optflags & MTIME); 30 set_m = !!(toys.optflags & OPT_MTIME);
31 create = !(toys.optflags & NO_CREATE);
32 31
33 if (toys.optflags & REFERENCE) { 32 // Use timestamp on a file
33 if (toys.optflags & OPT_REFERENCE) {
34 struct stat sb; 34 struct stat sb;
35 if (toys.optflags & TIME) 35
36 error_exit("Cannot specify times from more than one source"); 36 if (toys.optflags & OPT_TIME)
37 error_exit("Redundant time source");
37 xstat(toy.touch.ref_file, &sb); 38 xstat(toy.touch.ref_file, &sb);
38 curr_m = sb.st_mtime; 39 curr_m = sb.st_mtime;
39 curr_a = sb.st_atime; 40 curr_a = sb.st_atime;
40 } else if (toys.optflags & TIME) { 41
42 // Use time specified on command line.
43 } else if (toys.optflags & OPT_TIME) {
41 struct tm t; 44 struct tm t;
42 time_t curr; 45 time_t curr;
43 char *c; 46 char *c;
47
44 curr = time(NULL); 48 curr = time(NULL);
45 if (!localtime_r(&curr, &t)) 49 if (localtime_r(&curr, &t)
46 goto time_error; 50 || !(c = strptime(toy.touch.time, "%m%d%H%M", &t))
47 c = strptime(toy.touch.time, "%m%d%H%M", &t); 51 || *c || -1==(curr_a = curr_m = mktime(&t)))
48 if (!c || *c) 52 {
49 goto time_error; 53 error_exit("Unknown time %s", toy.touch.time);
50 curr_a = curr_m = mktime(&t); 54 }
51 if (curr_a == -1) 55
52 time_error: 56 // use current time
53 error_exit("Error converting time %s to internal format", 57 } else curr_m = curr_a = time(NULL);
54 toy.touch.time);
55 } else {
56 curr_m = curr_a = time(NULL);
57 }
58 58
59 for (i = 0; (arg = toys.optargs[i]); i++) { 59 for (i = 0; (arg = toys.optargs[i]); i++) {
60 struct utimbuf buf; 60 struct utimbuf buf;
61 struct stat sb; 61 struct stat sb;
62 62
63 buf.modtime = curr_m; 63 buf.modtime = curr_m;
64 buf.actime = curr_a; 64 buf.actime = curr_a;
65 65
66 if (stat(arg, &sb) == -1) { 66 if (stat(arg, &sb) == -1) {
67 if (create && errno == ENOENT) { 67 if (!(toys.optflags & OPT_NOCREATE) && errno == ENOENT) {
68 if (creat(arg, 0644)) 68 if (creat(arg, 0644))
69 goto error; 69 goto error;
70 if (stat(arg, &sb)) 70 if (stat(arg, &sb))
71 goto error; 71 goto error;
72 } 72 }
73 } 73 }
74 74
75 if ((set_a+set_m) == 1) { 75 if ((set_a+set_m) == 1) {
76 /* We've been asked to only change one */ 76 /* We've been asked to only change one */
77 if (set_a) 77 if (set_a) buf.modtime = sb.st_mtime;
78 buf.modtime = sb.st_mtime; 78 else if (set_m) buf.actime = sb.st_atime;
79 else if (set_m)
80 buf.actime = sb.st_atime;
81 } 79 }
82 80
83 if (toys.optflags & LENGTH) 81 if (toys.optflags & OPT_LENGTH)
84 if (truncate(arg, toy.touch.length)) 82 if (truncate(arg, toy.touch.length))
85 goto error; 83 goto error;
86 if (utime(arg, &buf)) 84 if (utime(arg, &buf))
87 error: 85 error:
88 perror_exit(arg); 86 perror_exit(arg);