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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char copyright[] =
36 "@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/types.h>
59 static void stime_arg1(const char *, struct timespec *);
60 static void stime_arg2(const char *, int, struct timespec *);
61 static void stime_darg(const char *, struct timespec *);
62 static void stime_file(const char *, struct timespec *);
63 static int timeoffset(const char *);
64 static void usage(const char *);
67 main(int argc, char *argv[])
70 struct timespec ts[2];
72 int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
76 myname = basename(argv[0]);
77 Aflag = aflag = cflag = mflag = timeset = 0;
79 ts[0].tv_sec = ts[1].tv_sec = 0;
80 ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
82 while ((ch = getopt(argc, argv, "A:acd:fhmr:t:")) != -1)
85 Aflag = timeoffset(optarg);
95 stime_darg(optarg, ts);
98 /* No-op for compatibility. */
102 atflag = AT_SYMLINK_NOFOLLOW;
109 stime_file(optarg, ts);
113 stime_arg1(optarg, ts);
121 if (aflag == 0 && mflag == 0)
127 * We're setting the time to an offset from a specified
128 * time. God knows why, but it means that we can set
129 * that time once and for all here.
132 ts[0].tv_sec += Aflag;
134 ts[1].tv_sec += Aflag;
135 Aflag = 0; /* done our job */
139 * If no -r or -t flag, at least two operands, the first of
140 * which is an 8 or 10 digit number, use the obsolete time
141 * specification, otherwise use the current time.
144 strtol(argv[0], &p, 10);
146 if (*p == '\0' && (len == 8 || len == 10)) {
148 stime_arg2(*argv++, len == 10, ts);
151 /* Both times default to the same. */
156 ts[0].tv_nsec = UTIME_OMIT;
158 ts[1].tv_nsec = UTIME_OMIT;
166 for (rval = 0; *argv; ++argv) {
167 /* See if the file exists. */
168 if (fstatat(AT_FDCWD, *argv, &sb, atflag) != 0) {
169 if (errno != ENOENT) {
175 /* Create the file. */
177 O_WRONLY | O_CREAT, DEFFILEMODE);
178 if (fd == -1 || fstat(fd, &sb) || close(fd)) {
184 /* If using the current time, we're done. */
192 * We're adjusting the times based on the file times, not a
193 * specified time (that gets handled above).
198 ts[0].tv_sec += Aflag;
202 ts[1].tv_sec += Aflag;
206 if (!utimensat(AT_FDCWD, *argv, ts, atflag))
215 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
218 stime_arg1(const char *arg, struct timespec *tvp)
226 if ((t = localtime(&now)) == NULL)
228 /* [[CC]YY]MMDDhhmm[.SS] */
229 if ((p = strchr(arg, '.')) == NULL)
230 t->tm_sec = 0; /* Seconds defaults to 0. */
232 if (strlen(p + 1) != 2)
235 t->tm_sec = ATOI2(p);
239 switch(strlen(arg)) {
240 case 12: /* CCYYMMDDhhmm */
241 t->tm_year = ATOI2(arg);
245 case 10: /* YYMMDDhhmm */
247 yearset = ATOI2(arg);
248 t->tm_year += yearset;
250 yearset = ATOI2(arg);
252 t->tm_year = yearset + 2000;
254 t->tm_year = yearset + 1900;
256 t->tm_year -= 1900; /* Convert to UNIX time. */
258 case 8: /* MMDDhhmm */
259 t->tm_mon = ATOI2(arg);
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);
269 t->tm_isdst = -1; /* Figure out DST. */
270 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
271 if (tvp[0].tv_sec == -1)
274 tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
278 errx(1, "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
282 stime_arg2(const char *arg, int year, struct timespec *tvp)
288 if ((t = localtime(&now)) == NULL)
291 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */
292 --t->tm_mon; /* Convert from 01-12 to 00-11 */
293 t->tm_mday = ATOI2(arg);
294 t->tm_hour = ATOI2(arg);
295 t->tm_min = ATOI2(arg);
297 t->tm_year = ATOI2(arg);
298 if (t->tm_year < 39) /* support 2000-2038 not 1902-1969 */
302 t->tm_isdst = -1; /* Figure out DST. */
303 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
304 if (tvp[0].tv_sec == -1)
306 "out of range or illegal time specification: MMDDhhmm[yy]");
308 tvp[0].tv_nsec = tvp[1].tv_nsec = 0;
312 stime_darg(const char *arg, struct timespec *tvp)
314 struct tm t = { .tm_sec = 0 };
315 const char *fmt, *colon;
321 colon = strchr(arg, ':');
322 if (colon == NULL || strchr(colon + 1, ':') == NULL)
324 fmt = strchr(arg, 'T') != NULL ? "%Y-%m-%dT%H:%M:%S" :
326 p = strptime(arg, fmt, &t);
329 /* POSIX: must have at least one digit after dot */
330 if ((*p == '.' || *p == ',') && isdigit((unsigned char)p[1])) {
333 while (isdigit((unsigned char)*p)) {
334 tvp[0].tv_nsec += val * (*p - '0');
346 tvp[0].tv_sec = isutc ? timegm(&t) : mktime(&t);
352 errx(1, "out of range or illegal time specification: YYYY-MM-DDThh:mm:SS[.frac][tz]");
355 /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
357 timeoffset(const char *arg)
366 switch (strlen(arg)) {
367 default: /* invalid */
368 errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
374 offset = offset * 60 + ATOI2(arg);
377 offset = offset * 60 + ATOI2(arg);
386 stime_file(const char *fname, struct timespec *tsp)
390 if (stat(fname, &sb))
397 usage(const char *myname)
399 fprintf(stderr, "usage: %s [-A [-][[hh]mm]SS] [-achm] [-r file] "
400 "[-t [[CC]YY]MMDDhhmm[.SS]]\n"
401 " [-d YYYY-MM-DDThh:mm:SS[.frac][tz]] "
402 "file ...\n", myname);