1 /* $OpenBSD: touch.c,v 1.27 2022/01/29 00:06:26 cheloha Exp $ */ 2 /* $NetBSD: touch.c,v 1.11 1995/08/31 22:10:06 jtc Exp $ */ 3 4 /* 5 * Copyright (c) 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <sys/time.h> 36 37 #include <ctype.h> 38 #include <err.h> 39 #include <errno.h> 40 #include <fcntl.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <time.h> 45 #include <unistd.h> 46 47 void stime_arg1(char *, struct timespec *); 48 void stime_arg2(char *, int, struct timespec *); 49 void stime_argd(char *, struct timespec *); 50 void stime_file(char *, struct timespec *); 51 static void __dead usage(void); 52 53 int 54 main(int argc, char *argv[]) 55 { 56 struct timespec ts[2]; 57 int aflag, cflag, mflag, ch, fd, len, rval, timeset; 58 char *p; 59 60 if (pledge("stdio rpath wpath cpath fattr", NULL) == -1) 61 err(1, "pledge"); 62 63 aflag = cflag = mflag = timeset = 0; 64 while ((ch = getopt(argc, argv, "acd:fmr:t:")) != -1) 65 switch (ch) { 66 case 'a': 67 aflag = 1; 68 break; 69 case 'c': 70 cflag = 1; 71 break; 72 case 'd': 73 timeset = 1; 74 stime_argd(optarg, ts); 75 break; 76 case 'f': 77 break; 78 case 'm': 79 mflag = 1; 80 break; 81 case 'r': 82 timeset = 1; 83 stime_file(optarg, ts); 84 break; 85 case 't': 86 timeset = 1; 87 stime_arg1(optarg, ts); 88 break; 89 default: 90 usage(); 91 } 92 argc -= optind; 93 argv += optind; 94 95 /* Default is both -a and -m. */ 96 if (aflag == 0 && mflag == 0) 97 aflag = mflag = 1; 98 99 /* 100 * If no -r or -t flag, at least two operands, the first of which 101 * is an 8 or 10 digit number, use the obsolete time specification. 102 */ 103 if (!timeset && argc > 1) { 104 (void)strtol(argv[0], &p, 10); 105 len = p - argv[0]; 106 if (*p == '\0' && (len == 8 || len == 10)) { 107 timeset = 1; 108 stime_arg2(*argv++, len == 10, ts); 109 } 110 } 111 112 /* Otherwise use the current time of day. */ 113 if (!timeset) 114 ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW; 115 116 if (!aflag) 117 ts[0].tv_nsec = UTIME_OMIT; 118 if (!mflag) 119 ts[1].tv_nsec = UTIME_OMIT; 120 121 if (*argv == NULL) 122 usage(); 123 124 for (rval = 0; *argv; ++argv) { 125 /* Update the file's timestamp if it exists. */ 126 if (! utimensat(AT_FDCWD, *argv, ts, 0)) 127 continue; 128 if (errno != ENOENT) { 129 rval = 1; 130 warn("%s", *argv); 131 continue; 132 } 133 134 /* Didn't exist; should we create it? */ 135 if (cflag) 136 continue; 137 138 /* Create the file. */ 139 fd = open(*argv, O_WRONLY | O_CREAT, DEFFILEMODE); 140 if (fd == -1) { 141 rval = 1; 142 warn("%s", *argv); 143 continue; 144 } 145 if (futimens(fd, ts) == -1) { 146 warn("%s", *argv); 147 rval = 1; 148 } 149 if (close(fd) == -1) { 150 warn("%s", *argv); 151 rval = 1; 152 } 153 } 154 return rval; 155 } 156 157 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 158 159 void 160 stime_arg1(char *arg, struct timespec *tsp) 161 { 162 struct tm *lt; 163 time_t tmptime; 164 int yearset; 165 char *dot, *p; 166 /* Start with the current time. */ 167 tmptime = time(NULL); 168 if ((lt = localtime(&tmptime)) == NULL) 169 err(1, "localtime"); 170 /* [[CC]YY]MMDDhhmm[.SS] */ 171 for (p = arg, dot = NULL; *p != '\0'; p++) { 172 if (*p == '.' && dot == NULL) 173 dot = p; 174 else if (!isdigit((unsigned char)*p)) 175 goto terr; 176 } 177 if (dot == NULL) 178 lt->tm_sec = 0; /* Seconds defaults to 0. */ 179 else { 180 *dot++ = '\0'; 181 if (strlen(dot) != 2) 182 goto terr; 183 lt->tm_sec = ATOI2(dot); 184 if (lt->tm_sec > 61) /* Could be leap second. */ 185 goto terr; 186 } 187 188 yearset = 0; 189 switch (strlen(arg)) { 190 case 12: /* CCYYMMDDhhmm */ 191 lt->tm_year = (ATOI2(arg) * 100) - 1900; 192 yearset = 1; 193 /* FALLTHROUGH */ 194 case 10: /* YYMMDDhhmm */ 195 if (yearset) { 196 yearset = ATOI2(arg); 197 lt->tm_year += yearset; 198 } else { 199 yearset = ATOI2(arg); 200 /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */ 201 lt->tm_year = yearset; 202 if (yearset < 69) 203 lt->tm_year += 100; 204 } 205 /* FALLTHROUGH */ 206 case 8: /* MMDDhhmm */ 207 lt->tm_mon = ATOI2(arg); 208 if (lt->tm_mon > 12 || lt->tm_mon == 0) 209 goto terr; 210 --lt->tm_mon; /* Convert from 01-12 to 00-11 */ 211 lt->tm_mday = ATOI2(arg); 212 if (lt->tm_mday > 31 || lt->tm_mday == 0) 213 goto terr; 214 lt->tm_hour = ATOI2(arg); 215 if (lt->tm_hour > 23) 216 goto terr; 217 lt->tm_min = ATOI2(arg); 218 if (lt->tm_min > 59) 219 goto terr; 220 break; 221 default: 222 goto terr; 223 } 224 225 lt->tm_isdst = -1; /* Figure out DST. */ 226 tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt); 227 if (tsp[0].tv_sec == -1) 228 terr: errx(1, 229 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 230 231 tsp[0].tv_nsec = tsp[1].tv_nsec = 0; 232 } 233 234 void 235 stime_arg2(char *arg, int year, struct timespec *tsp) 236 { 237 struct tm *lt; 238 time_t tmptime; 239 /* Start with the current time. */ 240 tmptime = time(NULL); 241 if ((lt = localtime(&tmptime)) == NULL) 242 err(1, "localtime"); 243 244 lt->tm_mon = ATOI2(arg); /* MMDDhhmm[YY] */ 245 if (lt->tm_mon > 12 || lt->tm_mon == 0) 246 goto terr; 247 --lt->tm_mon; /* Convert from 01-12 to 00-11 */ 248 lt->tm_mday = ATOI2(arg); 249 if (lt->tm_mday > 31 || lt->tm_mday == 0) 250 goto terr; 251 lt->tm_hour = ATOI2(arg); 252 if (lt->tm_hour > 23) 253 goto terr; 254 lt->tm_min = ATOI2(arg); 255 if (lt->tm_min > 59) 256 goto terr; 257 if (year) { 258 year = ATOI2(arg); 259 /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */ 260 lt->tm_year = year; 261 if (year < 69) 262 lt->tm_year += 100; 263 } 264 lt->tm_sec = 0; 265 266 lt->tm_isdst = -1; /* Figure out DST. */ 267 tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt); 268 if (tsp[0].tv_sec == -1) 269 terr: errx(1, 270 "out of range or illegal time specification: MMDDhhmm[YY]"); 271 272 tsp[0].tv_nsec = tsp[1].tv_nsec = 0; 273 } 274 275 void 276 stime_file(char *fname, struct timespec *tsp) 277 { 278 struct stat sb; 279 280 if (stat(fname, &sb)) 281 err(1, "%s", fname); 282 tsp[0] = sb.st_atim; 283 tsp[1] = sb.st_mtim; 284 } 285 286 void 287 stime_argd(char *arg, struct timespec *tsp) 288 { 289 struct tm tm; 290 char *frac, *p; 291 int utc = 0; 292 293 /* accept YYYY-MM-DD(T| )hh:mm:ss[(.|,)frac][Z] */ 294 memset(&tm, 0, sizeof(tm)); 295 p = strptime(arg, "%F", &tm); 296 if (p == NULL || (*p != 'T' && *p != ' ')) 297 goto terr; 298 p = strptime(p + 1, "%T", &tm); 299 if (p == NULL) 300 goto terr; 301 tsp[0].tv_nsec = 0; 302 if (*p == '.' || *p == ',') { 303 frac = ++p; 304 while (isdigit((unsigned char)*p)) { 305 if (p - frac < 9) { 306 tsp[0].tv_nsec = tsp[0].tv_nsec * 10 + 307 *p - '0'; 308 } 309 p++; 310 } 311 if (p == frac) 312 goto terr; 313 314 /* fill in the trailing zeros */ 315 while (p - frac-- < 9) 316 tsp[0].tv_nsec *= 10; 317 } 318 if (*p == 'Z') { 319 utc = 1; 320 p++; 321 } 322 if (*p != '\0') 323 goto terr; 324 325 tm.tm_isdst = -1; 326 tsp[0].tv_sec = utc ? timegm(&tm) : mktime(&tm); 327 if (tsp[0].tv_sec == -1) 328 terr: errx(1, 329 "out of range or illegal time specification: YYYY-MM-DDThh:mm:ss[.frac][Z]"); 330 tsp[1] = tsp[0]; 331 } 332 333 static void __dead 334 usage(void) 335 { 336 (void)fprintf(stderr, 337 "usage: touch [-acm] [-d ccyy-mm-ddTHH:MM:SS[.frac][Z]] [-r file]\n" 338 " [-t [[cc]yy]mmddHHMM[.SS]] file ...\n"); 339 exit(1); 340 } 341