]>
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 | 35 | |
686e1a44 | 36 | __FBSDID("$FreeBSD: src/usr.bin/touch/touch.c,v 1.25 2010/03/28 13:16:08 ed Exp $"); |
6c780a1f | 37 | |
44a7a5ab | 38 | #ifndef lint |
00337e45 | 39 | __used static const char copyright[] = |
6c780a1f A |
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 | |
00337e45 | 45 | __used 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> | |
867b3d41 | 51 | #include <sys/attr.h> |
44a7a5ab A |
52 | |
53 | #include <err.h> | |
54 | #include <errno.h> | |
55 | #include <fcntl.h> | |
686e1a44 | 56 | #include <libgen.h> |
44a7a5ab A |
57 | #include <stdio.h> |
58 | #include <stdlib.h> | |
59 | #include <string.h> | |
44a7a5ab | 60 | #include <time.h> |
44a7a5ab A |
61 | #include <unistd.h> |
62 | ||
867b3d41 A |
63 | typedef struct { |
64 | struct timespec mtime; | |
65 | struct timespec atime; | |
66 | } set_ts; | |
67 | ||
6c780a1f | 68 | int rw(char *, struct stat *, int); |
867b3d41 A |
69 | void stime_arg1(char *, set_ts *); |
70 | void stime_arg2(char *, int, set_ts *); | |
71 | void stime_file(char *, set_ts *); | |
686e1a44 A |
72 | int timeoffset(char *); |
73 | void usage(char *); | |
44a7a5ab A |
74 | |
75 | int | |
6c780a1f | 76 | main(int argc, char *argv[]) |
44a7a5ab A |
77 | { |
78 | struct stat sb; | |
6c780a1f A |
79 | int (*stat_f)(const char *, struct stat *); |
80 | int (*utimes_f)(const char *, const struct timeval *); | |
686e1a44 | 81 | int Aflag, aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset; |
44a7a5ab | 82 | char *p; |
686e1a44 | 83 | char *myname; |
867b3d41 A |
84 | struct attrlist ts_req = { |
85 | .bitmapcount = ATTR_BIT_MAP_COUNT, | |
86 | .commonattr = ATTR_CMN_MODTIME | ATTR_CMN_ACCTIME, | |
87 | }; | |
88 | set_ts ts_struct = {}; | |
44a7a5ab | 89 | |
686e1a44 A |
90 | myname = basename(argv[0]); |
91 | Aflag = aflag = cflag = fflag = mflag = timeset = 0; | |
6c780a1f A |
92 | stat_f = stat; |
93 | utimes_f = utimes; | |
867b3d41 A |
94 | if (clock_gettime(CLOCK_REALTIME, &ts_struct.mtime)) |
95 | err(1, "clock_gettime"); | |
96 | ts_struct.atime = ts_struct.mtime; | |
44a7a5ab | 97 | |
686e1a44 | 98 | while ((ch = getopt(argc, argv, "A:acfhmr:t:")) != -1) |
44a7a5ab | 99 | switch(ch) { |
686e1a44 A |
100 | case 'A': |
101 | Aflag = timeoffset(optarg); | |
102 | break; | |
44a7a5ab A |
103 | case 'a': |
104 | aflag = 1; | |
105 | break; | |
106 | case 'c': | |
107 | cflag = 1; | |
108 | break; | |
109 | case 'f': | |
110 | fflag = 1; | |
111 | break; | |
44a7a5ab | 112 | case 'h': |
6c780a1f A |
113 | cflag = 1; |
114 | stat_f = lstat; | |
115 | utimes_f = lutimes; | |
44a7a5ab | 116 | break; |
44a7a5ab A |
117 | case 'm': |
118 | mflag = 1; | |
119 | break; | |
120 | case 'r': | |
121 | timeset = 1; | |
867b3d41 | 122 | stime_file(optarg, &ts_struct); |
44a7a5ab A |
123 | break; |
124 | case 't': | |
125 | timeset = 1; | |
867b3d41 | 126 | stime_arg1(optarg, &ts_struct); |
44a7a5ab A |
127 | break; |
128 | case '?': | |
129 | default: | |
686e1a44 | 130 | usage(myname); |
44a7a5ab A |
131 | } |
132 | argc -= optind; | |
133 | argv += optind; | |
134 | ||
44a7a5ab A |
135 | if (aflag == 0 && mflag == 0) |
136 | aflag = mflag = 1; | |
137 | ||
686e1a44 A |
138 | if (timeset) { |
139 | if (Aflag) { | |
140 | /* | |
141 | * We're setting the time to an offset from a specified | |
142 | * time. God knows why, but it means that we can set | |
143 | * that time once and for all here. | |
144 | */ | |
145 | if (aflag) | |
867b3d41 | 146 | ts_struct.atime.tv_sec += Aflag; |
686e1a44 | 147 | if (mflag) |
867b3d41 | 148 | ts_struct.mtime.tv_sec += Aflag; |
686e1a44 | 149 | Aflag = 0; /* done our job */ |
44a7a5ab | 150 | } |
686e1a44 A |
151 | } else { |
152 | /* | |
153 | * If no -r or -t flag, at least two operands, the first of | |
154 | * which is an 8 or 10 digit number, use the obsolete time | |
155 | * specification, otherwise use the current time. | |
156 | */ | |
157 | if (argc > 1) { | |
158 | strtol(argv[0], &p, 10); | |
159 | len = p - argv[0]; | |
160 | if (*p == '\0' && (len == 8 || len == 10)) { | |
161 | timeset = 1; | |
867b3d41 | 162 | stime_arg2(*argv++, len == 10, &ts_struct); |
686e1a44 A |
163 | } |
164 | } | |
686e1a44 | 165 | } |
44a7a5ab A |
166 | |
167 | if (*argv == NULL) | |
686e1a44 A |
168 | usage(myname); |
169 | ||
170 | if (Aflag) | |
171 | cflag = 1; | |
44a7a5ab A |
172 | |
173 | for (rval = 0; *argv; ++argv) { | |
174 | /* See if the file exists. */ | |
6c780a1f | 175 | if (stat_f(*argv, &sb) != 0) { |
686e1a44 A |
176 | if (errno != ENOENT) { |
177 | rval = 1; | |
178 | warn("%s", *argv); | |
179 | continue; | |
180 | } | |
44a7a5ab A |
181 | if (!cflag) { |
182 | /* Create the file. */ | |
183 | fd = open(*argv, | |
184 | O_WRONLY | O_CREAT, DEFFILEMODE); | |
185 | if (fd == -1 || fstat(fd, &sb) || close(fd)) { | |
186 | rval = 1; | |
187 | warn("%s", *argv); | |
188 | continue; | |
189 | } | |
190 | ||
191 | /* If using the current time, we're done. */ | |
192 | if (!timeset) | |
193 | continue; | |
194 | } else | |
195 | continue; | |
196 | } | |
6c780a1f | 197 | |
44a7a5ab | 198 | if (!aflag) |
867b3d41 | 199 | ts_struct.atime = sb.st_atimespec; |
44a7a5ab | 200 | if (!mflag) |
867b3d41 | 201 | ts_struct.mtime = sb.st_mtimespec; |
44a7a5ab | 202 | |
686e1a44 A |
203 | /* |
204 | * We're adjusting the times based on the file times, not a | |
205 | * specified time (that gets handled above). | |
206 | */ | |
207 | if (Aflag) { | |
208 | if (aflag) { | |
867b3d41 A |
209 | ts_struct.atime = sb.st_atimespec; |
210 | ts_struct.atime.tv_sec += Aflag; | |
686e1a44 A |
211 | } |
212 | if (mflag) { | |
867b3d41 A |
213 | ts_struct.mtime = sb.st_mtimespec; |
214 | ts_struct.mtime.tv_sec += Aflag; | |
686e1a44 A |
215 | } |
216 | } | |
217 | ||
867b3d41 A |
218 | /* Try setattrlist(2). */ |
219 | if (!setattrlist(*argv, &ts_req, &ts_struct, sizeof(ts_struct), 0)) | |
44a7a5ab A |
220 | continue; |
221 | ||
222 | /* If the user specified a time, nothing else we can do. */ | |
686e1a44 | 223 | if (timeset || Aflag) { |
44a7a5ab A |
224 | rval = 1; |
225 | warn("%s", *argv); | |
40bf83fe | 226 | continue; |
44a7a5ab A |
227 | } |
228 | ||
229 | /* | |
230 | * System V and POSIX 1003.1 require that a NULL argument | |
231 | * set the access/modification times to the current time. | |
232 | * The permission checks are different, too, in that the | |
233 | * ability to write the file is sufficient. Take a shot. | |
234 | */ | |
6c780a1f | 235 | if (!utimes_f(*argv, NULL)) |
44a7a5ab A |
236 | continue; |
237 | ||
238 | /* Try reading/writing. */ | |
686e1a44 A |
239 | if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode)) { |
240 | if (rw(*argv, &sb, fflag)) | |
241 | rval = 1; | |
242 | } else { | |
243 | rval = 1; | |
244 | warn("%s", *argv); | |
245 | } | |
44a7a5ab A |
246 | } |
247 | exit(rval); | |
248 | } | |
249 | ||
6c780a1f | 250 | #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; |
44a7a5ab A |
251 | |
252 | void | |
867b3d41 | 253 | stime_arg1(char *arg, set_ts *tsp) |
44a7a5ab | 254 | { |
6c780a1f | 255 | time_t now; |
44a7a5ab | 256 | struct tm *t; |
44a7a5ab A |
257 | int yearset; |
258 | char *p; | |
259 | /* Start with the current time. */ | |
867b3d41 | 260 | now = tsp->atime.tv_sec; |
6c780a1f | 261 | if ((t = localtime(&now)) == NULL) |
44a7a5ab A |
262 | err(1, "localtime"); |
263 | /* [[CC]YY]MMDDhhmm[.SS] */ | |
264 | if ((p = strchr(arg, '.')) == NULL) | |
265 | t->tm_sec = 0; /* Seconds defaults to 0. */ | |
266 | else { | |
267 | if (strlen(p + 1) != 2) | |
268 | goto terr; | |
269 | *p++ = '\0'; | |
270 | t->tm_sec = ATOI2(p); | |
271 | } | |
6c780a1f | 272 | |
44a7a5ab | 273 | yearset = 0; |
6c780a1f | 274 | switch(strlen(arg)) { |
44a7a5ab | 275 | case 12: /* CCYYMMDDhhmm */ |
6c780a1f A |
276 | t->tm_year = ATOI2(arg); |
277 | t->tm_year *= 100; | |
44a7a5ab | 278 | yearset = 1; |
6c780a1f | 279 | /* FALLTHROUGH */ |
44a7a5ab A |
280 | case 10: /* YYMMDDhhmm */ |
281 | if (yearset) { | |
6c780a1f A |
282 | yearset = ATOI2(arg); |
283 | t->tm_year += yearset; | |
44a7a5ab A |
284 | } else { |
285 | yearset = ATOI2(arg); | |
286 | if (yearset < 69) | |
6c780a1f | 287 | t->tm_year = yearset + 2000; |
44a7a5ab | 288 | else |
6c780a1f | 289 | t->tm_year = yearset + 1900; |
44a7a5ab | 290 | } |
6c780a1f | 291 | t->tm_year -= 1900; /* Convert to UNIX time. */ |
44a7a5ab A |
292 | /* FALLTHROUGH */ |
293 | case 8: /* MMDDhhmm */ | |
294 | t->tm_mon = ATOI2(arg); | |
295 | --t->tm_mon; /* Convert from 01-12 to 00-11 */ | |
44a7a5ab | 296 | t->tm_mday = ATOI2(arg); |
44a7a5ab | 297 | t->tm_hour = ATOI2(arg); |
44a7a5ab A |
298 | t->tm_min = ATOI2(arg); |
299 | break; | |
300 | default: | |
301 | goto terr; | |
302 | } | |
303 | ||
304 | t->tm_isdst = -1; /* Figure out DST. */ | |
867b3d41 A |
305 | tsp->atime.tv_sec = tsp->mtime.tv_sec = mktime(t); |
306 | if (tsp->atime.tv_sec == -1) | |
44a7a5ab A |
307 | terr: errx(1, |
308 | "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); | |
309 | ||
867b3d41 | 310 | tsp->atime.tv_nsec = tsp->mtime.tv_nsec = 0; |
44a7a5ab A |
311 | } |
312 | ||
313 | void | |
867b3d41 | 314 | stime_arg2(char *arg, int year, set_ts *tsp) |
44a7a5ab | 315 | { |
6c780a1f | 316 | time_t now; |
44a7a5ab | 317 | struct tm *t; |
44a7a5ab | 318 | /* Start with the current time. */ |
867b3d41 | 319 | now = tsp->atime.tv_sec; |
6c780a1f | 320 | if ((t = localtime(&now)) == NULL) |
44a7a5ab A |
321 | err(1, "localtime"); |
322 | ||
323 | t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */ | |
324 | --t->tm_mon; /* Convert from 01-12 to 00-11 */ | |
325 | t->tm_mday = ATOI2(arg); | |
326 | t->tm_hour = ATOI2(arg); | |
327 | t->tm_min = ATOI2(arg); | |
328 | if (year) { | |
6c780a1f A |
329 | t->tm_year = ATOI2(arg); |
330 | if (t->tm_year < 39) /* support 2000-2038 not 1902-1969 */ | |
331 | t->tm_year += 100; | |
44a7a5ab A |
332 | } |
333 | ||
334 | t->tm_isdst = -1; /* Figure out DST. */ | |
867b3d41 A |
335 | tsp->atime.tv_sec = tsp->mtime.tv_sec = mktime(t); |
336 | if (tsp->atime.tv_sec == -1) | |
44a7a5ab A |
337 | errx(1, |
338 | "out of range or illegal time specification: MMDDhhmm[yy]"); | |
339 | ||
867b3d41 | 340 | tsp->atime.tv_nsec = tsp->mtime.tv_nsec = 0; |
44a7a5ab A |
341 | } |
342 | ||
686e1a44 A |
343 | /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */ |
344 | int | |
345 | timeoffset(char *arg) | |
346 | { | |
347 | int offset; | |
348 | int isneg; | |
349 | ||
350 | offset = 0; | |
351 | isneg = *arg == '-'; | |
352 | if (isneg) | |
353 | arg++; | |
354 | switch (strlen(arg)) { | |
355 | default: /* invalid */ | |
356 | errx(1, "Invalid offset spec, must be [-][[HH]MM]SS"); | |
357 | ||
358 | case 6: /* HHMMSS */ | |
359 | offset = ATOI2(arg); | |
360 | /* FALLTHROUGH */ | |
361 | case 4: /* MMSS */ | |
362 | offset = offset * 60 + ATOI2(arg); | |
363 | /* FALLTHROUGH */ | |
364 | case 2: /* SS */ | |
365 | offset = offset * 60 + ATOI2(arg); | |
366 | } | |
367 | if (isneg) | |
368 | return (-offset); | |
369 | else | |
370 | return (offset); | |
371 | } | |
372 | ||
44a7a5ab | 373 | void |
867b3d41 | 374 | stime_file(char *fname, set_ts *ts_struct) |
44a7a5ab A |
375 | { |
376 | struct stat sb; | |
377 | ||
378 | if (stat(fname, &sb)) | |
379 | err(1, "%s", fname); | |
867b3d41 A |
380 | ts_struct->atime = sb.st_atimespec; |
381 | ts_struct->mtime = sb.st_mtimespec; | |
44a7a5ab A |
382 | } |
383 | ||
384 | int | |
6c780a1f | 385 | rw(char *fname, struct stat *sbp, int force) |
44a7a5ab A |
386 | { |
387 | int fd, needed_chmod, rval; | |
388 | u_char byte; | |
389 | ||
440bd198 A |
390 | /* Try regular files. */ |
391 | if (!S_ISREG(sbp->st_mode)) { | |
44a7a5ab A |
392 | warnx("%s: %s", fname, strerror(EFTYPE)); |
393 | return (1); | |
394 | } | |
395 | ||
396 | needed_chmod = rval = 0; | |
397 | if ((fd = open(fname, O_RDWR, 0)) == -1) { | |
398 | if (!force || chmod(fname, DEFFILEMODE)) | |
399 | goto err; | |
400 | if ((fd = open(fname, O_RDWR, 0)) == -1) | |
401 | goto err; | |
402 | needed_chmod = 1; | |
403 | } | |
404 | ||
405 | if (sbp->st_size != 0) { | |
406 | if (read(fd, &byte, sizeof(byte)) != sizeof(byte)) | |
407 | goto err; | |
408 | if (lseek(fd, (off_t)0, SEEK_SET) == -1) | |
409 | goto err; | |
410 | if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) | |
411 | goto err; | |
412 | } else { | |
413 | if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) { | |
414 | err: rval = 1; | |
415 | warn("%s", fname); | |
416 | } else if (ftruncate(fd, (off_t)0)) { | |
417 | rval = 1; | |
418 | warn("%s: file modified", fname); | |
419 | } | |
420 | } | |
421 | ||
422 | if (close(fd) && rval != 1) { | |
423 | rval = 1; | |
424 | warn("%s", fname); | |
425 | } | |
426 | if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) { | |
427 | rval = 1; | |
428 | warn("%s: permissions modified", fname); | |
429 | } | |
430 | return (rval); | |
431 | } | |
432 | ||
6c780a1f | 433 | void |
686e1a44 | 434 | usage(char *myname) |
44a7a5ab | 435 | { |
686e1a44 A |
436 | fprintf(stderr, "usage:\n" "%s [-A [-][[hh]mm]SS] [-acfhm] [-r file] " |
437 | "[-t [[CC]YY]MMDDhhmm[.SS]] file ...\n", myname); | |
44a7a5ab A |
438 | exit(1); |
439 | } |