3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static char copyright[] =
36 "@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/types.h>
57 int rw __P((char *, struct stat *, int));
58 void stime_arg1 __P((char *, struct timeval *));
59 void stime_arg2 __P((char *, int, struct timeval *));
60 void stime_file __P((char *, struct timeval *));
61 void usage __P((void));
70 int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
73 aflag = cflag = fflag = mflag = timeset = 0;
74 if (gettimeofday(&tv[0], NULL))
75 err(1, "gettimeofday");
77 while ((ch = getopt(argc, argv, "acfmr:t:")) != -1)
93 stime_file(optarg, tv);
97 stime_arg1(optarg, tv);
106 /* Default is both -a and -m. */
107 if (aflag == 0 && mflag == 0)
111 * If no -r or -t flag, at least two operands, the first of which
112 * is an 8 or 10 digit number, use the obsolete time specification.
114 if (!timeset && argc > 1) {
115 (void)strtol(argv[0], &p, 10);
117 if (*p == '\0' && (len == 8 || len == 10)) {
119 stime_arg2(*argv++, len == 10, tv);
123 /* Otherwise use the current time of day. */
130 for (rval = 0; *argv; ++argv) {
131 /* See if the file exists. */
132 if (stat(*argv, &sb)) {
134 /* Create the file. */
136 O_WRONLY | O_CREAT, DEFFILEMODE);
137 if (fd == -1 || fstat(fd, &sb) || close(fd)) {
143 /* If using the current time, we're done. */
151 TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
153 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
156 if (!utimes(*argv, tv))
159 /* If the user specified a time, nothing else we can do. */
166 * System V and POSIX 1003.1 require that a NULL argument
167 * set the access/modification times to the current time.
168 * The permission checks are different, too, in that the
169 * ability to write the file is sufficient. Take a shot.
171 if (!utimes(*argv, NULL))
174 /* Try reading/writing. */
175 if (rw(*argv, &sb, fflag))
181 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
192 /* Start with the current time. */
194 if ((t = localtime(&now)) == NULL)
196 /* [[CC]YY]MMDDhhmm[.SS] */
197 if ((p = strchr(arg, '.')) == NULL)
198 t->tm_sec = 0; /* Seconds defaults to 0. */
200 if (strlen(p + 1) != 2)
203 t->tm_sec = ATOI2(p);
207 switch(strlen(arg)) {
208 case 12: /* CCYYMMDDhhmm */
209 t->tm_year = ATOI2(arg);
213 case 10: /* YYMMDDhhmm */
215 yearset = ATOI2(arg);
216 t->tm_year += yearset;
218 yearset = ATOI2(arg);
220 t->tm_year = yearset + 2000;
222 t->tm_year = yearset + 1900;
224 t->tm_year -= 1900; /* Convert to UNIX time. */
226 case 8: /* MMDDhhmm */
227 t->tm_mon = ATOI2(arg);
228 --t->tm_mon; /* Convert from 01-12 to 00-11 */
229 t->tm_mday = ATOI2(arg);
230 t->tm_hour = ATOI2(arg);
231 t->tm_min = ATOI2(arg);
237 t->tm_isdst = -1; /* Figure out DST. */
238 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
239 if (tvp[0].tv_sec == -1)
241 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
243 tvp[0].tv_usec = tvp[1].tv_usec = 0;
247 stime_arg2(arg, year, tvp)
254 /* Start with the current time. */
256 if ((t = localtime(&now)) == NULL)
259 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
260 --t->tm_mon; /* Convert from 01-12 to 00-11 */
261 t->tm_mday = ATOI2(arg);
262 t->tm_hour = ATOI2(arg);
263 t->tm_min = ATOI2(arg);
265 t->tm_year = ATOI2(arg);
266 if (t->tm_year < 39) /* support 2000-2038 not 1902-1969 */
270 t->tm_isdst = -1; /* Figure out DST. */
271 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
272 if (tvp[0].tv_sec == -1)
274 "out of range or illegal time specification: MMDDhhmm[yy]");
276 tvp[0].tv_usec = tvp[1].tv_usec = 0;
280 stime_file(fname, tvp)
286 if (stat(fname, &sb))
288 TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
289 TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
293 rw(fname, sbp, force)
298 int fd, needed_chmod, rval;
301 /* Try regular files and directories. */
302 if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
303 warnx("%s: %s", fname, strerror(EFTYPE));
307 needed_chmod = rval = 0;
308 if ((fd = open(fname, O_RDWR, 0)) == -1) {
309 if (!force || chmod(fname, DEFFILEMODE))
311 if ((fd = open(fname, O_RDWR, 0)) == -1)
316 if (sbp->st_size != 0) {
317 if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
319 if (lseek(fd, (off_t)0, SEEK_SET) == -1)
321 if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
324 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
327 } else if (ftruncate(fd, (off_t)0)) {
329 warn("%s: file modified", fname);
333 if (close(fd) && rval != 1) {
337 if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
339 warn("%s: permissions modified", fname);
347 (void)fprintf(stderr, "usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n");