1 /* $OpenBSD: touch.c,v 1.26 2019/03/10 15:11:52 schwarze 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 || futimens(fd, ts) || close(fd)) { 141 rval = 1; 142 warn("%s", *argv); 143 } 144 } 145 return rval; 146 } 147 148 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 149 150 void 151 stime_arg1(char *arg, struct timespec *tsp) 152 { 153 struct tm *lt; 154 time_t tmptime; 155 int yearset; 156 char *dot, *p; 157 /* Start with the current time. */ 158 tmptime = time(NULL); 159 if ((lt = localtime(&tmptime)) == NULL) 160 err(1, "localtime"); 161 /* [[CC]YY]MMDDhhmm[.SS] */ 162 for (p = arg, dot = NULL; *p != '\0'; p++) { 163 if (*p == '.' && dot == NULL) 164 dot = p; 165 else if (!isdigit((unsigned char)*p)) 166 goto terr; 167 } 168 if (dot == NULL) 169 lt->tm_sec = 0; /* Seconds defaults to 0. */ 170 else { 171 *dot++ = '\0'; 172 if (strlen(dot) != 2) 173 goto terr; 174 lt->tm_sec = ATOI2(dot); 175 if (lt->tm_sec > 61) /* Could be leap second. */ 176 goto terr; 177 } 178 179 yearset = 0; 180 switch (strlen(arg)) { 181 case 12: /* CCYYMMDDhhmm */ 182 lt->tm_year = (ATOI2(arg) * 100) - 1900; 183 yearset = 1; 184 /* FALLTHROUGH */ 185 case 10: /* YYMMDDhhmm */ 186 if (yearset) { 187 yearset = ATOI2(arg); 188 lt->tm_year += yearset; 189 } else { 190 yearset = ATOI2(arg); 191 /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */ 192 lt->tm_year = yearset; 193 if (yearset < 69) 194 lt->tm_year += 100; 195 } 196 /* FALLTHROUGH */ 197 case 8: /* MMDDhhmm */ 198 lt->tm_mon = ATOI2(arg); 199 if (lt->tm_mon > 12 || lt->tm_mon == 0) 200 goto terr; 201 --lt->tm_mon; /* Convert from 01-12 to 00-11 */ 202 lt->tm_mday = ATOI2(arg); 203 if (lt->tm_mday > 31 || lt->tm_mday == 0) 204 goto terr; 205 lt->tm_hour = ATOI2(arg); 206 if (lt->tm_hour > 23) 207 goto terr; 208 lt->tm_min = ATOI2(arg); 209 if (lt->tm_min > 59) 210 goto terr; 211 break; 212 default: 213 goto terr; 214 } 215 216 lt->tm_isdst = -1; /* Figure out DST. */ 217 tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt); 218 if (tsp[0].tv_sec == -1) 219 terr: errx(1, 220 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 221 222 tsp[0].tv_nsec = tsp[1].tv_nsec = 0; 223 } 224 225 void 226 stime_arg2(char *arg, int year, struct timespec *tsp) 227 { 228 struct tm *lt; 229 time_t tmptime; 230 /* Start with the current time. */ 231 tmptime = time(NULL); 232 if ((lt = localtime(&tmptime)) == NULL) 233 err(1, "localtime"); 234 235 lt->tm_mon = ATOI2(arg); /* MMDDhhmm[YY] */ 236 if (lt->tm_mon > 12 || lt->tm_mon == 0) 237 goto terr; 238 --lt->tm_mon; /* Convert from 01-12 to 00-11 */ 239 lt->tm_mday = ATOI2(arg); 240 if (lt->tm_mday > 31 || lt->tm_mday == 0) 241 goto terr; 242 lt->tm_hour = ATOI2(arg); 243 if (lt->tm_hour > 23) 244 goto terr; 245 lt->tm_min = ATOI2(arg); 246 if (lt->tm_min > 59) 247 goto terr; 248 if (year) { 249 year = ATOI2(arg); 250 /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */ 251 lt->tm_year = year; 252 if (year < 69) 253 lt->tm_year += 100; 254 } 255 lt->tm_sec = 0; 256 257 lt->tm_isdst = -1; /* Figure out DST. */ 258 tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt); 259 if (tsp[0].tv_sec == -1) 260 terr: errx(1, 261 "out of range or illegal time specification: MMDDhhmm[YY]"); 262 263 tsp[0].tv_nsec = tsp[1].tv_nsec = 0; 264 } 265 266 void 267 stime_file(char *fname, struct timespec *tsp) 268 { 269 struct stat sb; 270 271 if (stat(fname, &sb)) 272 err(1, "%s", fname); 273 tsp[0] = sb.st_atim; 274 tsp[1] = sb.st_mtim; 275 } 276 277 void 278 stime_argd(char *arg, struct timespec *tsp) 279 { 280 struct tm tm; 281 char *frac, *p; 282 int utc = 0; 283 284 /* accept YYYY-MM-DD(T| )hh:mm:ss[(.|,)frac][Z] */ 285 memset(&tm, 0, sizeof(tm)); 286 p = strptime(arg, "%F", &tm); 287 if (p == NULL || (*p != 'T' && *p != ' ')) 288 goto terr; 289 p = strptime(p + 1, "%T", &tm); 290 if (p == NULL) 291 goto terr; 292 tsp[0].tv_nsec = 0; 293 if (*p == '.' || *p == ',') { 294 frac = ++p; 295 while (isdigit((unsigned char)*p)) { 296 if (p - frac < 9) { 297 tsp[0].tv_nsec = tsp[0].tv_nsec * 10 + 298 *p - '0'; 299 } 300 p++; 301 } 302 if (p == frac) 303 goto terr; 304 305 /* fill in the trailing zeros */ 306 while (p - frac-- < 9) 307 tsp[0].tv_nsec *= 10; 308 } 309 if (*p == 'Z') { 310 utc = 1; 311 p++; 312 } 313 if (*p != '\0') 314 goto terr; 315 316 tm.tm_isdst = -1; 317 tsp[0].tv_sec = utc ? timegm(&tm) : mktime(&tm); 318 if (tsp[0].tv_sec == -1) 319 terr: errx(1, 320 "out of range or illegal time specification: YYYY-MM-DDThh:mm:ss[.frac][Z]"); 321 tsp[1] = tsp[0]; 322 } 323 324 static void __dead 325 usage(void) 326 { 327 (void)fprintf(stderr, 328 "usage: touch [-acm] [-d ccyy-mm-ddTHH:MM:SS[.frac][Z]] [-r file]\n" 329 " [-t [[cc]yy]mmddHHMM[.SS]] file ...\n"); 330 exit(1); 331 } 332