]>
Commit | Line | Data |
---|---|---|
44a7a5ab A |
1 | /* |
2 | * Copyright (c) 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #include <sys/cdefs.h> | |
6c780a1f A |
35 | |
36 | __RCSID("$FreeBSD: src/usr.bin/touch/touch.c,v 1.20 2002/09/04 23:29:07 dwmalone Exp $"); | |
37 | ||
44a7a5ab | 38 | #ifndef lint |
6c780a1f A |
39 | static const char copyright[] = |
40 | "@(#) Copyright (c) 1993\n\ | |
41 | The Regents of the University of California. All rights reserved.\n"; | |
42 | #endif | |
44a7a5ab A |
43 | |
44 | #ifndef lint | |
6c780a1f | 45 | static const char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93"; |
44a7a5ab | 46 | #endif |
44a7a5ab A |
47 | |
48 | #include <sys/types.h> | |
49 | #include <sys/stat.h> | |
50 | #include <sys/time.h> | |
51 | ||
52 | #include <err.h> | |
53 | #include <errno.h> | |
54 | #include <fcntl.h> | |
55 | #include <stdio.h> | |
56 | #include <stdlib.h> | |
57 | #include <string.h> | |
44a7a5ab | 58 | #include <time.h> |
44a7a5ab A |
59 | #include <unistd.h> |
60 | ||
6c780a1f A |
61 | int rw(char *, struct stat *, int); |
62 | void stime_arg1(char *, struct timeval *); | |
63 | void stime_arg2(char *, int, struct timeval *); | |
64 | void stime_file(char *, struct timeval *); | |
65 | void usage(void); | |
44a7a5ab A |
66 | |
67 | int | |
6c780a1f | 68 | main(int argc, char *argv[]) |
44a7a5ab A |
69 | { |
70 | struct stat sb; | |
71 | struct timeval tv[2]; | |
6c780a1f A |
72 | int (*stat_f)(const char *, struct stat *); |
73 | int (*utimes_f)(const char *, const struct timeval *); | |
74 | int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset; | |
44a7a5ab | 75 | char *p; |
44a7a5ab | 76 | |
6c780a1f A |
77 | aflag = cflag = fflag = mflag = timeset = 0; |
78 | stat_f = stat; | |
79 | utimes_f = utimes; | |
44a7a5ab A |
80 | if (gettimeofday(&tv[0], NULL)) |
81 | err(1, "gettimeofday"); | |
82 | ||
f383e97b | 83 | #ifndef __APPLE__ |
44a7a5ab | 84 | while ((ch = getopt(argc, argv, "acfhmr:t:")) != -1) |
f383e97b A |
85 | #else |
86 | while ((ch = getopt(argc, argv, "acfmr:t:")) != -1) | |
87 | #endif | |
44a7a5ab A |
88 | switch(ch) { |
89 | case 'a': | |
90 | aflag = 1; | |
91 | break; | |
92 | case 'c': | |
93 | cflag = 1; | |
94 | break; | |
95 | case 'f': | |
96 | fflag = 1; | |
97 | break; | |
98 | #ifndef __APPLE__ | |
99 | case 'h': | |
6c780a1f A |
100 | cflag = 1; |
101 | stat_f = lstat; | |
102 | utimes_f = lutimes; | |
44a7a5ab | 103 | break; |
6c780a1f | 104 | #endif /* __APPLE__ */ |
44a7a5ab A |
105 | case 'm': |
106 | mflag = 1; | |
107 | break; | |
108 | case 'r': | |
109 | timeset = 1; | |
110 | stime_file(optarg, tv); | |
111 | break; | |
112 | case 't': | |
113 | timeset = 1; | |
114 | stime_arg1(optarg, tv); | |
115 | break; | |
116 | case '?': | |
117 | default: | |
118 | usage(); | |
119 | } | |
120 | argc -= optind; | |
121 | argv += optind; | |
122 | ||
123 | /* Default is both -a and -m. */ | |
124 | if (aflag == 0 && mflag == 0) | |
125 | aflag = mflag = 1; | |
126 | ||
44a7a5ab A |
127 | /* |
128 | * If no -r or -t flag, at least two operands, the first of which | |
129 | * is an 8 or 10 digit number, use the obsolete time specification. | |
130 | */ | |
131 | if (!timeset && argc > 1) { | |
132 | (void)strtol(argv[0], &p, 10); | |
133 | len = p - argv[0]; | |
134 | if (*p == '\0' && (len == 8 || len == 10)) { | |
135 | timeset = 1; | |
136 | stime_arg2(*argv++, len == 10, tv); | |
137 | } | |
138 | } | |
139 | ||
140 | /* Otherwise use the current time of day. */ | |
141 | if (!timeset) | |
142 | tv[1] = tv[0]; | |
143 | ||
144 | if (*argv == NULL) | |
145 | usage(); | |
146 | ||
147 | for (rval = 0; *argv; ++argv) { | |
148 | /* See if the file exists. */ | |
6c780a1f | 149 | if (stat_f(*argv, &sb) != 0) { |
44a7a5ab A |
150 | if (!cflag) { |
151 | /* Create the file. */ | |
152 | fd = open(*argv, | |
153 | O_WRONLY | O_CREAT, DEFFILEMODE); | |
154 | if (fd == -1 || fstat(fd, &sb) || close(fd)) { | |
155 | rval = 1; | |
156 | warn("%s", *argv); | |
157 | continue; | |
158 | } | |
159 | ||
160 | /* If using the current time, we're done. */ | |
161 | if (!timeset) | |
162 | continue; | |
163 | } else | |
164 | continue; | |
165 | } | |
6c780a1f | 166 | |
44a7a5ab A |
167 | if (!aflag) |
168 | TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec); | |
169 | if (!mflag) | |
170 | TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec); | |
171 | ||
172 | /* Try utimes(2). */ | |
6c780a1f | 173 | if (!utimes_f(*argv, tv)) |
44a7a5ab A |
174 | continue; |
175 | ||
176 | /* If the user specified a time, nothing else we can do. */ | |
177 | if (timeset) { | |
178 | rval = 1; | |
179 | warn("%s", *argv); | |
180 | } | |
181 | ||
182 | /* | |
183 | * System V and POSIX 1003.1 require that a NULL argument | |
184 | * set the access/modification times to the current time. | |
185 | * The permission checks are different, too, in that the | |
186 | * ability to write the file is sufficient. Take a shot. | |
187 | */ | |
6c780a1f | 188 | if (!utimes_f(*argv, NULL)) |
44a7a5ab A |
189 | continue; |
190 | ||
191 | /* Try reading/writing. */ | |
440bd198 A |
192 | if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode) && |
193 | rw(*argv, &sb, fflag)) | |
44a7a5ab | 194 | rval = 1; |
440bd198 A |
195 | else |
196 | warn("%s", *argv); | |
44a7a5ab A |
197 | } |
198 | exit(rval); | |
199 | } | |
200 | ||
6c780a1f | 201 | #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; |
44a7a5ab A |
202 | |
203 | void | |
6c780a1f | 204 | stime_arg1(char *arg, struct timeval *tvp) |
44a7a5ab | 205 | { |
6c780a1f | 206 | time_t now; |
44a7a5ab | 207 | struct tm *t; |
44a7a5ab A |
208 | int yearset; |
209 | char *p; | |
210 | /* Start with the current time. */ | |
6c780a1f A |
211 | now = tvp[0].tv_sec; |
212 | if ((t = localtime(&now)) == NULL) | |
44a7a5ab A |
213 | err(1, "localtime"); |
214 | /* [[CC]YY]MMDDhhmm[.SS] */ | |
215 | if ((p = strchr(arg, '.')) == NULL) | |
216 | t->tm_sec = 0; /* Seconds defaults to 0. */ | |
217 | else { | |
218 | if (strlen(p + 1) != 2) | |
219 | goto terr; | |
220 | *p++ = '\0'; | |
221 | t->tm_sec = ATOI2(p); | |
222 | } | |
6c780a1f | 223 | |
44a7a5ab | 224 | yearset = 0; |
6c780a1f | 225 | switch(strlen(arg)) { |
44a7a5ab | 226 | case 12: /* CCYYMMDDhhmm */ |
6c780a1f A |
227 | t->tm_year = ATOI2(arg); |
228 | t->tm_year *= 100; | |
44a7a5ab | 229 | yearset = 1; |
6c780a1f | 230 | /* FALLTHROUGH */ |
44a7a5ab A |
231 | case 10: /* YYMMDDhhmm */ |
232 | if (yearset) { | |
6c780a1f A |
233 | yearset = ATOI2(arg); |
234 | t->tm_year += yearset; | |
44a7a5ab A |
235 | } else { |
236 | yearset = ATOI2(arg); | |
237 | if (yearset < 69) | |
6c780a1f | 238 | t->tm_year = yearset + 2000; |
44a7a5ab | 239 | else |
6c780a1f | 240 | t->tm_year = yearset + 1900; |
44a7a5ab | 241 | } |
6c780a1f | 242 | t->tm_year -= 1900; /* Convert to UNIX time. */ |
44a7a5ab A |
243 | /* FALLTHROUGH */ |
244 | case 8: /* MMDDhhmm */ | |
245 | t->tm_mon = ATOI2(arg); | |
246 | --t->tm_mon; /* Convert from 01-12 to 00-11 */ | |
44a7a5ab | 247 | t->tm_mday = ATOI2(arg); |
44a7a5ab | 248 | t->tm_hour = ATOI2(arg); |
44a7a5ab A |
249 | t->tm_min = ATOI2(arg); |
250 | break; | |
251 | default: | |
252 | goto terr; | |
253 | } | |
254 | ||
255 | t->tm_isdst = -1; /* Figure out DST. */ | |
256 | tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); | |
257 | if (tvp[0].tv_sec == -1) | |
258 | terr: errx(1, | |
259 | "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); | |
260 | ||
261 | tvp[0].tv_usec = tvp[1].tv_usec = 0; | |
262 | } | |
263 | ||
264 | void | |
6c780a1f | 265 | stime_arg2(char *arg, int year, struct timeval *tvp) |
44a7a5ab | 266 | { |
6c780a1f | 267 | time_t now; |
44a7a5ab | 268 | struct tm *t; |
44a7a5ab | 269 | /* Start with the current time. */ |
6c780a1f A |
270 | now = tvp[0].tv_sec; |
271 | if ((t = localtime(&now)) == NULL) | |
44a7a5ab A |
272 | err(1, "localtime"); |
273 | ||
274 | t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */ | |
275 | --t->tm_mon; /* Convert from 01-12 to 00-11 */ | |
276 | t->tm_mday = ATOI2(arg); | |
277 | t->tm_hour = ATOI2(arg); | |
278 | t->tm_min = ATOI2(arg); | |
279 | if (year) { | |
6c780a1f A |
280 | t->tm_year = ATOI2(arg); |
281 | if (t->tm_year < 39) /* support 2000-2038 not 1902-1969 */ | |
282 | t->tm_year += 100; | |
44a7a5ab A |
283 | } |
284 | ||
285 | t->tm_isdst = -1; /* Figure out DST. */ | |
286 | tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); | |
287 | if (tvp[0].tv_sec == -1) | |
288 | errx(1, | |
289 | "out of range or illegal time specification: MMDDhhmm[yy]"); | |
290 | ||
291 | tvp[0].tv_usec = tvp[1].tv_usec = 0; | |
292 | } | |
293 | ||
294 | void | |
6c780a1f | 295 | stime_file(char *fname, struct timeval *tvp) |
44a7a5ab A |
296 | { |
297 | struct stat sb; | |
298 | ||
299 | if (stat(fname, &sb)) | |
300 | err(1, "%s", fname); | |
6c780a1f A |
301 | TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec); |
302 | TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec); | |
44a7a5ab A |
303 | } |
304 | ||
305 | int | |
6c780a1f | 306 | rw(char *fname, struct stat *sbp, int force) |
44a7a5ab A |
307 | { |
308 | int fd, needed_chmod, rval; | |
309 | u_char byte; | |
310 | ||
440bd198 A |
311 | /* Try regular files. */ |
312 | if (!S_ISREG(sbp->st_mode)) { | |
44a7a5ab A |
313 | warnx("%s: %s", fname, strerror(EFTYPE)); |
314 | return (1); | |
315 | } | |
316 | ||
317 | needed_chmod = rval = 0; | |
318 | if ((fd = open(fname, O_RDWR, 0)) == -1) { | |
319 | if (!force || chmod(fname, DEFFILEMODE)) | |
320 | goto err; | |
321 | if ((fd = open(fname, O_RDWR, 0)) == -1) | |
322 | goto err; | |
323 | needed_chmod = 1; | |
324 | } | |
325 | ||
326 | if (sbp->st_size != 0) { | |
327 | if (read(fd, &byte, sizeof(byte)) != sizeof(byte)) | |
328 | goto err; | |
329 | if (lseek(fd, (off_t)0, SEEK_SET) == -1) | |
330 | goto err; | |
331 | if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) | |
332 | goto err; | |
333 | } else { | |
334 | if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) { | |
335 | err: rval = 1; | |
336 | warn("%s", fname); | |
337 | } else if (ftruncate(fd, (off_t)0)) { | |
338 | rval = 1; | |
339 | warn("%s: file modified", fname); | |
340 | } | |
341 | } | |
342 | ||
343 | if (close(fd) && rval != 1) { | |
344 | rval = 1; | |
345 | warn("%s", fname); | |
346 | } | |
347 | if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) { | |
348 | rval = 1; | |
349 | warn("%s: permissions modified", fname); | |
350 | } | |
351 | return (rval); | |
352 | } | |
353 | ||
6c780a1f A |
354 | void |
355 | usage(void) | |
44a7a5ab | 356 | { |
f383e97b | 357 | #ifndef __APPLE__ |
6c780a1f | 358 | (void)fprintf(stderr, "usage: touch [-acfhm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n"); |
f383e97b A |
359 | #else |
360 | (void)fprintf(stderr, "usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n"); | |
361 | #endif | |
44a7a5ab A |
362 | exit(1); |
363 | } |