2 * SPDX-License-Identifier: BSD-3-Clause
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static const char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/types.h>
58 static void stime_arg1(const char *, struct timespec *);
59 static void stime_arg2(const char *, int, struct timespec *);
60 static void stime_darg(const char *, struct timespec *);
61 static void stime_file(const char *, struct timespec *);
62 static int timeoffset(const char *);
63 static void usage(const char *);
66 main(int argc, char *argv[])
69 struct timespec ts[2];
71 int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
75 myname = basename(argv[0]);
76 Aflag = aflag = cflag = mflag = timeset = 0;
78 ts[0].tv_sec = ts[1].tv_sec = 0;
79 ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
81 while ((ch = getopt(argc, argv, "A:acd:fhmr:t:")) != -1)
84 Aflag = timeoffset(optarg);
94 stime_darg(optarg, ts);
97 /* No-op for compatibility. */
101 atflag = AT_SYMLINK_NOFOLLOW;
108 stime_file(optarg, ts);
112 stime_arg1(optarg, ts);
120 if (aflag == 0 && mflag == 0)
126 * We're setting the time to an offset from a specified
127 * time. God knows why, but it means that we can set
128 * that time once and for all here.
131 ts[0].tv_sec += Aflag;
133 ts[1].tv_sec += Aflag;
134 Aflag = 0; /* done our job */
138 * If no -r or -t flag, at least two operands, the first of
139 * which is an 8 or 10 digit number, use the obsolete time
140 * specification, otherwise use the current time.
143 strtol(argv[0], &p, 10);
145 if (*p == '\0' && (len == 8 || len == 10)) {
147 stime_arg2(*argv++, len == 10, ts);
150 /* Both times default to the same. */
155 ts[0].tv_nsec = UTIME_OMIT;
157 ts[1].tv_nsec = UTIME_OMIT;
165 for (rval = 0; *argv; ++argv) {
166 /* See if the file exists. */
167 if (fstatat(AT_FDCWD, *argv, &sb, atflag) != 0) {
168 if (errno != ENOENT) {
174 /* Create the file. */
176 O_WRONLY | O_CREAT, DEFFILEMODE);
182 if (fstat(fd, &sb) < 0) {
191 /* If using the current time, we're done. */
199 * We're adjusting the times based on the file times, not a
200 * specified time (that gets handled above).
205 ts[0].tv_sec += Aflag;
209 ts[1].tv_sec += Aflag;
213 if (!utimensat(AT_FDCWD, *argv, ts, atflag))
222 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
225 stime_arg1(const char *arg, struct timespec *tvp)
233 if ((t = localtime(&now)) == NULL)
235 /* [[CC]YY]MMDDhhmm[.SS] */
236 if ((p = strchr(arg, '.')) == NULL)
237 t->tm_sec = 0; /* Seconds defaults to 0. */
239 if (strlen(p + 1) != 2)
242 t->tm_sec = ATOI2(p);
246 switch(strlen(arg)) {
247 case 12: /* CCYYMMDDhhmm */
248 t->tm_year = ATOI2(arg);
252 case 10: /* YYMMDDhhmm */
254 yearset = ATOI2(arg);
255 t->tm_year += yearset;
257 yearset = ATOI2(arg);
259 t->tm_year = yearset + 2000;
261 t->tm_year = yearset + 1900;
263 t->tm_year -= 1900; /* Convert to UNIX time. */
265 case 8: /* MMDDhhmm */
266 t->tm_mon = ATOI2(arg);
267 --t->tm_mon; /* Convert from 01-12 to 00-11 */
268 t->tm_mday = ATOI2(arg);
269 t->tm_hour = ATOI2(arg);
270 t->tm_min = ATOI2(arg);
276 t->tm_isdst = -1; /* Figure out DST. */
277 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
278 if (tvp[0].tv_sec == -1)
281 tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
285 errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
289 stime_arg2(const char *arg, int year, struct timespec *tvp)
295 if ((t = localtime(&now)) == NULL)
298 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
299 --t->tm_mon; /* Convert from 01-12 to 00-11 */
300 t->tm_mday = ATOI2(arg);
301 t->tm_hour = ATOI2(arg);
302 t->tm_min = ATOI2(arg);
304 t->tm_year = ATOI2(arg);
305 if (t->tm_year < 39) /* support 2000-2038 not 1902-1969 */
309 t->tm_isdst = -1; /* Figure out DST. */
310 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
311 if (tvp[0].tv_sec == -1)
313 "out of range or illegal time specification: MMDDhhmm[yy]");
315 tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
319 stime_darg(const char *arg, struct timespec *tvp)
321 struct tm t = { .tm_sec = 0 };
322 const char *fmt, *colon;
328 colon = strchr(arg, ':');
329 if (colon == NULL || strchr(colon + 1, ':') == NULL)
331 fmt = strchr(arg, 'T') != NULL ? "%Y-%m-%dT%H:%M:%S" :
333 p = strptime(arg, fmt, &t);
336 /* POSIX: must have at least one digit after dot */
337 if ((*p == '.' || *p == ',') && isdigit((unsigned char)p[1])) {
340 while (isdigit((unsigned char)*p)) {
341 tvp[0].tv_nsec += val * (*p - '0');
353 tvp[0].tv_sec = isutc ? timegm(&t) : mktime(&t);
359 errx(1, "out of range or illegal time specification: YYYY-MM-DDThh:mm:SS[.frac][tz]");
362 /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
364 timeoffset(const char *arg)
373 switch (strlen(arg)) {
374 default: /* invalid */
375 errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
381 offset = offset * 60 + ATOI2(arg);
384 offset = offset * 60 + ATOI2(arg);
393 stime_file(const char *fname, struct timespec *tsp)
397 if (stat(fname, &sb))
404 usage(const char *myname)
406 fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "
407 "[-t [[CC]YY]MMDDhhmm[.SS]]\n"
408 " [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] "
409 "file ...\n", myname);