1 /* 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 36 __FBSDID("$FreeBSD: src/usr.bin/touch/touch.c,v 1.11.2.2 2002/07/28 06:52:15 eric Exp $"); 37 38 #ifndef lint 39 static char copyright[] = 40 "@(#) Copyright (c) 1993\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42 #endif /* not lint */ 43 44 #ifndef lint 45 static char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93"; 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 #include <sys/stat.h> 50 #include <sys/time.h> 51 52 #include <err.h> 53 #include <errno.h> 54 #include <fcntl.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <time.h> 59 #include <unistd.h> 60 61 int rw __P((char *, struct stat *, int)); 62 void stime_arg1 __P((char *, struct timeval *)); 63 void stime_arg2 __P((char *, int, struct timeval *)); 64 void stime_file __P((char *, struct timeval *)); 65 void usage __P((void)); 66 67 int 68 main(argc, argv) 69 int argc; 70 char *argv[]; 71 { 72 struct stat sb; 73 struct timeval tv[2]; 74 int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset; 75 char *p; 76 77 aflag = cflag = fflag = mflag = timeset = 0; 78 if (gettimeofday(&tv[0], NULL)) 79 err(1, "gettimeofday"); 80 81 while ((ch = getopt(argc, argv, "acfmr:t:")) != -1) 82 switch(ch) { 83 case 'a': 84 aflag = 1; 85 break; 86 case 'c': 87 cflag = 1; 88 break; 89 case 'f': 90 fflag = 1; 91 break; 92 case 'm': 93 mflag = 1; 94 break; 95 case 'r': 96 timeset = 1; 97 stime_file(optarg, tv); 98 break; 99 case 't': 100 timeset = 1; 101 stime_arg1(optarg, tv); 102 break; 103 case '?': 104 default: 105 usage(); 106 } 107 argc -= optind; 108 argv += optind; 109 110 /* Default is both -a and -m. */ 111 if (aflag == 0 && mflag == 0) 112 aflag = mflag = 1; 113 114 /* 115 * If no -r or -t flag, at least two operands, the first of which 116 * is an 8 or 10 digit number, use the obsolete time specification. 117 */ 118 if (!timeset && argc > 1) { 119 (void)strtol(argv[0], &p, 10); 120 len = p - argv[0]; 121 if (*p == '\0' && (len == 8 || len == 10)) { 122 timeset = 1; 123 stime_arg2(*argv++, len == 10, tv); 124 } 125 } 126 127 /* Otherwise use the current time of day. */ 128 if (!timeset) 129 tv[1] = tv[0]; 130 131 if (*argv == NULL) 132 usage(); 133 134 for (rval = 0; *argv; ++argv) { 135 /* See if the file exists. */ 136 if (stat(*argv, &sb)) { 137 if (!cflag) { 138 /* Create the file. */ 139 fd = open(*argv, 140 O_WRONLY | O_CREAT, DEFFILEMODE); 141 if (fd == -1 || fstat(fd, &sb) || close(fd)) { 142 rval = 1; 143 warn("%s", *argv); 144 continue; 145 } 146 147 /* If using the current time, we're done. */ 148 if (!timeset) 149 continue; 150 } else 151 continue; 152 } 153 154 if (!aflag) 155 TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec); 156 if (!mflag) 157 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec); 158 159 /* Try utimes(2). */ 160 if (!utimes(*argv, tv)) 161 continue; 162 163 /* If the user specified a time, nothing else we can do. */ 164 if (timeset) { 165 rval = 1; 166 warn("%s", *argv); 167 } 168 169 /* 170 * System V and POSIX 1003.1 require that a NULL argument 171 * set the access/modification times to the current time. 172 * The permission checks are different, too, in that the 173 * ability to write the file is sufficient. Take a shot. 174 */ 175 if (!utimes(*argv, NULL)) 176 continue; 177 178 /* Try reading/writing. */ 179 if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode) && 180 rw(*argv, &sb, fflag)) 181 rval = 1; 182 else 183 warn("%s", *argv); 184 } 185 exit(rval); 186 } 187 188 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 189 190 void 191 stime_arg1(arg, tvp) 192 char *arg; 193 struct timeval *tvp; 194 { 195 time_t now; 196 struct tm *t; 197 int yearset; 198 char *p; 199 /* Start with the current time. */ 200 now = tvp[0].tv_sec; 201 if ((t = localtime(&now)) == NULL) 202 err(1, "localtime"); 203 /* [[CC]YY]MMDDhhmm[.SS] */ 204 if ((p = strchr(arg, '.')) == NULL) 205 t->tm_sec = 0; /* Seconds defaults to 0. */ 206 else { 207 if (strlen(p + 1) != 2) 208 goto terr; 209 *p++ = '\0'; 210 t->tm_sec = ATOI2(p); 211 } 212 213 yearset = 0; 214 switch(strlen(arg)) { 215 case 12: /* CCYYMMDDhhmm */ 216 t->tm_year = ATOI2(arg); 217 t->tm_year *= 100; 218 yearset = 1; 219 /* FALLTHOUGH */ 220 case 10: /* YYMMDDhhmm */ 221 if (yearset) { 222 yearset = ATOI2(arg); 223 t->tm_year += yearset; 224 } else { 225 yearset = ATOI2(arg); 226 if (yearset < 69) 227 t->tm_year = yearset + 2000; 228 else 229 t->tm_year = yearset + 1900; 230 } 231 t->tm_year -= 1900; /* Convert to UNIX time. */ 232 /* FALLTHROUGH */ 233 case 8: /* MMDDhhmm */ 234 t->tm_mon = ATOI2(arg); 235 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 236 t->tm_mday = ATOI2(arg); 237 t->tm_hour = ATOI2(arg); 238 t->tm_min = ATOI2(arg); 239 break; 240 default: 241 goto terr; 242 } 243 244 t->tm_isdst = -1; /* Figure out DST. */ 245 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 246 if (tvp[0].tv_sec == -1) 247 terr: errx(1, 248 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 249 250 tvp[0].tv_usec = tvp[1].tv_usec = 0; 251 } 252 253 void 254 stime_arg2(arg, year, tvp) 255 char *arg; 256 int year; 257 struct timeval *tvp; 258 { 259 time_t now; 260 struct tm *t; 261 /* Start with the current time. */ 262 now = tvp[0].tv_sec; 263 if ((t = localtime(&now)) == NULL) 264 err(1, "localtime"); 265 266 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */ 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); 271 if (year) { 272 t->tm_year = ATOI2(arg); 273 if (t->tm_year < 39) /* support 2000-2038 not 1902-1969 */ 274 t->tm_year += 100; 275 } 276 277 t->tm_isdst = -1; /* Figure out DST. */ 278 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 279 if (tvp[0].tv_sec == -1) 280 errx(1, 281 "out of range or illegal time specification: MMDDhhmm[yy]"); 282 283 tvp[0].tv_usec = tvp[1].tv_usec = 0; 284 } 285 286 void 287 stime_file(fname, tvp) 288 char *fname; 289 struct timeval *tvp; 290 { 291 struct stat sb; 292 293 if (stat(fname, &sb)) 294 err(1, "%s", fname); 295 TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec); 296 TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec); 297 } 298 299 int 300 rw(fname, sbp, force) 301 char *fname; 302 struct stat *sbp; 303 int force; 304 { 305 int fd, needed_chmod, rval; 306 u_char byte; 307 308 /* Try regular files. */ 309 if (!S_ISREG(sbp->st_mode)) { 310 warnx("%s: %s", fname, strerror(EFTYPE)); 311 return (1); 312 } 313 314 needed_chmod = rval = 0; 315 if ((fd = open(fname, O_RDWR, 0)) == -1) { 316 if (!force || chmod(fname, DEFFILEMODE)) 317 goto err; 318 if ((fd = open(fname, O_RDWR, 0)) == -1) 319 goto err; 320 needed_chmod = 1; 321 } 322 323 if (sbp->st_size != 0) { 324 if (read(fd, &byte, sizeof(byte)) != sizeof(byte)) 325 goto err; 326 if (lseek(fd, (off_t)0, SEEK_SET) == -1) 327 goto err; 328 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) 329 goto err; 330 } else { 331 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) { 332 err: rval = 1; 333 warn("%s", fname); 334 } else if (ftruncate(fd, (off_t)0)) { 335 rval = 1; 336 warn("%s: file modified", fname); 337 } 338 } 339 340 if (close(fd) && rval != 1) { 341 rval = 1; 342 warn("%s", fname); 343 } 344 if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) { 345 rval = 1; 346 warn("%s: permissions modified", fname); 347 } 348 return (rval); 349 } 350 351 void 352 usage() 353 { 354 (void)fprintf(stderr, "usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n"); 355 exit(1); 356 } 357