1 /* $OpenBSD: touch.c,v 1.17 2007/08/06 19:16:06 sobrado 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 <locale.h> 45 #include <time.h> 46 #include <tzfile.h> 47 #include <unistd.h> 48 49 void stime_arg1(char *, struct timeval *); 50 void stime_arg2(char *, int, struct timeval *); 51 void stime_file(char *, struct timeval *); 52 __dead void usage(void); 53 54 int 55 main(int argc, char *argv[]) 56 { 57 struct stat sb; 58 struct timeval tv[2]; 59 int aflag, cflag, mflag, ch, fd, len, rval, timeset; 60 char *p; 61 62 (void)setlocale(LC_ALL, ""); 63 64 aflag = cflag = mflag = timeset = 0; 65 if (gettimeofday(&tv[0], NULL)) 66 err(1, "gettimeofday"); 67 68 while ((ch = getopt(argc, argv, "acfmr:t:")) != -1) 69 switch (ch) { 70 case 'a': 71 aflag = 1; 72 break; 73 case 'c': 74 cflag = 1; 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, tv); 84 break; 85 case 't': 86 timeset = 1; 87 stime_arg1(optarg, tv); 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, tv); 109 } 110 } 111 112 /* Otherwise use the current time of day. */ 113 if (!timeset) 114 tv[1] = tv[0]; 115 116 if (*argv == NULL) 117 usage(); 118 119 for (rval = 0; *argv; ++argv) { 120 /* See if the file exists. */ 121 if (stat(*argv, &sb)) { 122 if (!cflag) { 123 /* Create the file. */ 124 fd = open(*argv, 125 O_WRONLY | O_CREAT, DEFFILEMODE); 126 if (fd == -1 || fstat(fd, &sb) || close(fd)) { 127 rval = 1; 128 warn("%s", *argv); 129 continue; 130 } 131 132 /* If using the current time, we're done. */ 133 if (!timeset) 134 continue; 135 } else 136 continue; 137 } 138 139 if (!aflag) 140 TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec); 141 if (!mflag) 142 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec); 143 144 /* Try utimes(2). */ 145 if (!utimes(*argv, tv)) 146 continue; 147 148 /* If the user specified a time, nothing else we can do. */ 149 if (timeset) { 150 rval = 1; 151 warn("%s", *argv); 152 } 153 154 /* 155 * System V and POSIX 1003.1 require that a NULL argument 156 * set the access/modification times to the current time. 157 * The permission checks are different, too, in that the 158 * ability to write the file is sufficient. Take a shot. 159 */ 160 if (!utimes(*argv, NULL)) 161 continue; 162 163 rval = 1; 164 warn("%s", *argv); 165 } 166 exit(rval); 167 } 168 169 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 170 171 void 172 stime_arg1(char *arg, struct timeval *tvp) 173 { 174 struct tm *lt; 175 time_t tmptime; 176 int yearset; 177 char *dot, *p; 178 /* Start with the current time. */ 179 tmptime = tvp[0].tv_sec; 180 if ((lt = localtime(&tmptime)) == NULL) 181 err(1, "localtime"); 182 /* [[CC]YY]MMDDhhmm[.SS] */ 183 for (p = arg, dot = NULL; *p != '\0'; p++) { 184 if (*p == '.' && dot == NULL) 185 dot = p; 186 else if (!isdigit((unsigned char)*p)) 187 goto terr; 188 } 189 if (dot == NULL) 190 lt->tm_sec = 0; /* Seconds defaults to 0. */ 191 else { 192 *dot++ = '\0'; 193 if (strlen(dot) != 2) 194 goto terr; 195 lt->tm_sec = ATOI2(dot); 196 if (lt->tm_sec > 61) /* Could be leap second. */ 197 goto terr; 198 } 199 200 yearset = 0; 201 switch (strlen(arg)) { 202 case 12: /* CCYYMMDDhhmm */ 203 lt->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE; 204 yearset = 1; 205 /* FALLTHROUGH */ 206 case 10: /* YYMMDDhhmm */ 207 if (yearset) { 208 yearset = ATOI2(arg); 209 lt->tm_year += yearset; 210 } else { 211 yearset = ATOI2(arg); 212 /* Preserve current century. */ 213 lt->tm_year = ((lt->tm_year / 100) * 100) + yearset; 214 } 215 /* FALLTHROUGH */ 216 case 8: /* MMDDhhmm */ 217 lt->tm_mon = ATOI2(arg); 218 if (lt->tm_mon > 12 || lt->tm_mon == 0) 219 goto terr; 220 --lt->tm_mon; /* Convert from 01-12 to 00-11 */ 221 lt->tm_mday = ATOI2(arg); 222 if (lt->tm_mday > 31 || lt->tm_mday == 0) 223 goto terr; 224 lt->tm_hour = ATOI2(arg); 225 if (lt->tm_hour > 23) 226 goto terr; 227 lt->tm_min = ATOI2(arg); 228 if (lt->tm_min > 59) 229 goto terr; 230 break; 231 default: 232 goto terr; 233 } 234 235 lt->tm_isdst = -1; /* Figure out DST. */ 236 tvp[0].tv_sec = tvp[1].tv_sec = mktime(lt); 237 if (tvp[0].tv_sec == -1) 238 terr: errx(1, 239 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 240 241 tvp[0].tv_usec = tvp[1].tv_usec = 0; 242 } 243 244 void 245 stime_arg2(char *arg, int year, struct timeval *tvp) 246 { 247 struct tm *lt; 248 time_t tmptime; 249 /* Start with the current time. */ 250 tmptime = tvp[0].tv_sec; 251 if ((lt = localtime(&tmptime)) == NULL) 252 err(1, "localtime"); 253 254 lt->tm_mon = ATOI2(arg); /* MMDDhhmm[YY] */ 255 if (lt->tm_mon > 12 || lt->tm_mon == 0) 256 goto terr; 257 --lt->tm_mon; /* Convert from 01-12 to 00-11 */ 258 lt->tm_mday = ATOI2(arg); 259 if (lt->tm_mday > 31 || lt->tm_mday == 0) 260 goto terr; 261 lt->tm_hour = ATOI2(arg); 262 if (lt->tm_hour > 23) 263 goto terr; 264 lt->tm_min = ATOI2(arg); 265 if (lt->tm_min > 59) 266 goto terr; 267 if (year) { 268 year = ATOI2(arg); /* Preserve current century. */ 269 lt->tm_year = ((lt->tm_year / 100) * 100) + year; 270 } 271 lt->tm_sec = 0; 272 273 lt->tm_isdst = -1; /* Figure out DST. */ 274 tvp[0].tv_sec = tvp[1].tv_sec = mktime(lt); 275 if (tvp[0].tv_sec == -1) 276 terr: errx(1, 277 "out of range or illegal time specification: MMDDhhmm[YY]"); 278 279 tvp[0].tv_usec = tvp[1].tv_usec = 0; 280 } 281 282 void 283 stime_file(char *fname, struct timeval *tvp) 284 { 285 struct stat sb; 286 287 if (stat(fname, &sb)) 288 err(1, "%s", fname); 289 TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec); 290 TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec); 291 } 292 293 __dead void 294 usage(void) 295 { 296 extern char *__progname; 297 298 (void)fprintf(stderr, 299 "usage: %s [-acm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n", 300 __progname); 301 exit(1); 302 } 303