Mercurial > hg > toybox
annotate toys/touch.c @ 163:a913f894b7d8
Simplify touch -l slightly.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 15 Nov 2007 20:43:35 -0600 |
parents | 0864aec90026 |
children | 25447caf1b4b |
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 |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
16 #define MTIME 0x01 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
17 #define NO_CREATE 0x02 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
18 #define ATIME 0x04 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
19 #define REFERENCE 0x08 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
20 #define TIME 0x10 |
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
|
21 #define LENGTH 0x20 |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
22 |
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 int touch_main(void) |
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; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
26 int i, set_a, set_m, create; |
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 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
29 set_a = !!(toys.optflags & ATIME); |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
30 set_m = !!(toys.optflags & MTIME); |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
31 create = !(toys.optflags & NO_CREATE); |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
32 |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
33 if (toys.optflags & REFERENCE) { |
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; |
160
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
35 if (toys.optflags & TIME) |
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
36 error_exit("Cannot specify times from more than one source"); |
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
37 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
|
38 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
|
39 curr_a = sb.st_atime; |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
40 } else if (toys.optflags & TIME) { |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
41 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
|
42 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
|
43 char *c; |
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); |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
45 if (!localtime_r(&curr, &t)) |
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
|
46 goto time_error; |
147
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
47 c = strptime(toy.touch.time, "%m%d%H%M", &t); |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
48 if (!c || *c) |
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
|
49 goto time_error; |
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_a = curr_m = mktime(&t); |
160
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
51 if (curr_a == -1) |
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
|
52 time_error: |
160
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
53 error_exit("Error converting time %s to internal format", |
2eb41e7bf180
Use builtin functions to simplify some code in touch
Charlie Shepherd <masterdriverz@gentoo.org>
parents:
147
diff
changeset
|
54 toy.touch.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 } else { |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
56 curr_m = curr_a = time(NULL); |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
57 } |
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) { |
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 (create && 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 */ |
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) |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
78 buf.modtime = 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
|
79 else if (set_m) |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
80 buf.actime = sb.st_atime; |
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 |
163 | 83 if (toys.optflags & LENGTH) |
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 } |
ec6e13b2495d
Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents:
130
diff
changeset
|
90 |
130
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 return 0; |
de3b6d914468
Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
92 } |