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 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved. 34 * @(#)touch.c 8.1 (Berkeley) 6/6/93 35 * $FreeBSD: src/usr.bin/touch/touch.c,v 1.11.2.2 2002/07/28 06:52:15 eric Exp $ 36 * $DragonFly: src/usr.bin/touch/touch.c,v 1.3 2003/10/04 20:36:53 hmp Exp $ 37 */ 38 39 #include <sys/types.h> 40 #include <sys/stat.h> 41 #include <sys/time.h> 42 43 #include <err.h> 44 #include <errno.h> 45 #include <fcntl.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <time.h> 50 #include <unistd.h> 51 52 int rw(char *, struct stat *, int); 53 void stime_arg1(char *, struct timeval *); 54 void stime_arg2(char *, int, struct timeval *); 55 void stime_file(char *, struct timeval *); 56 void usage(void); 57 58 int 59 main(int argc, char **argv) 60 { 61 struct stat sb; 62 struct timeval tv[2]; 63 int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset; 64 char *p; 65 66 aflag = cflag = fflag = mflag = timeset = 0; 67 if (gettimeofday(&tv[0], NULL)) 68 err(1, "gettimeofday"); 69 70 while ((ch = getopt(argc, argv, "acfmr:t:")) != -1) 71 switch(ch) { 72 case 'a': 73 aflag = 1; 74 break; 75 case 'c': 76 cflag = 1; 77 break; 78 case 'f': 79 fflag = 1; 80 break; 81 case 'm': 82 mflag = 1; 83 break; 84 case 'r': 85 timeset = 1; 86 stime_file(optarg, tv); 87 break; 88 case 't': 89 timeset = 1; 90 stime_arg1(optarg, tv); 91 break; 92 case '?': 93 default: 94 usage(); 95 } 96 argc -= optind; 97 argv += optind; 98 99 /* Default is both -a and -m. */ 100 if (aflag == 0 && mflag == 0) 101 aflag = mflag = 1; 102 103 /* 104 * If no -r or -t flag, at least two operands, the first of which 105 * is an 8 or 10 digit number, use the obsolete time specification. 106 */ 107 if (!timeset && argc > 1) { 108 (void)strtol(argv[0], &p, 10); 109 len = p - argv[0]; 110 if (*p == '\0' && (len == 8 || len == 10)) { 111 timeset = 1; 112 stime_arg2(*argv++, len == 10, tv); 113 } 114 } 115 116 /* Otherwise use the current time of day. */ 117 if (!timeset) 118 tv[1] = tv[0]; 119 120 if (*argv == NULL) 121 usage(); 122 123 for (rval = 0; *argv; ++argv) { 124 /* See if the file exists. */ 125 if (stat(*argv, &sb)) { 126 if (!cflag) { 127 /* Create the file. */ 128 fd = open(*argv, 129 O_WRONLY | O_CREAT, DEFFILEMODE); 130 if (fd == -1 || fstat(fd, &sb) || close(fd)) { 131 rval = 1; 132 warn("%s", *argv); 133 continue; 134 } 135 136 /* If using the current time, we're done. */ 137 if (!timeset) 138 continue; 139 } else 140 continue; 141 } 142 143 if (!aflag) 144 TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec); 145 if (!mflag) 146 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec); 147 148 /* Try utimes(2). */ 149 if (!utimes(*argv, tv)) 150 continue; 151 152 /* If the user specified a time, nothing else we can do. */ 153 if (timeset) { 154 rval = 1; 155 warn("%s", *argv); 156 } 157 158 /* 159 * System V and POSIX 1003.1 require that a NULL argument 160 * set the access/modification times to the current time. 161 * The permission checks are different, too, in that the 162 * ability to write the file is sufficient. Take a shot. 163 */ 164 if (!utimes(*argv, NULL)) 165 continue; 166 167 /* Try reading/writing. */ 168 if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode) && 169 rw(*argv, &sb, fflag)) 170 rval = 1; 171 else 172 warn("%s", *argv); 173 } 174 exit(rval); 175 } 176 177 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 178 179 void 180 stime_arg1(char *arg, struct timeval *tvp) 181 { 182 time_t now; 183 struct tm *t; 184 int yearset; 185 char *p; 186 /* Start with the current time. */ 187 now = tvp[0].tv_sec; 188 if ((t = localtime(&now)) == NULL) 189 err(1, "localtime"); 190 /* [[CC]YY]MMDDhhmm[.SS] */ 191 if ((p = strchr(arg, '.')) == NULL) 192 t->tm_sec = 0; /* Seconds defaults to 0. */ 193 else { 194 if (strlen(p + 1) != 2) 195 goto terr; 196 *p++ = '\0'; 197 t->tm_sec = ATOI2(p); 198 } 199 200 yearset = 0; 201 switch(strlen(arg)) { 202 case 12: /* CCYYMMDDhhmm */ 203 t->tm_year = ATOI2(arg); 204 t->tm_year *= 100; 205 yearset = 1; 206 /* FALLTHOUGH */ 207 case 10: /* YYMMDDhhmm */ 208 if (yearset) { 209 yearset = ATOI2(arg); 210 t->tm_year += yearset; 211 } else { 212 yearset = ATOI2(arg); 213 if (yearset < 69) 214 t->tm_year = yearset + 2000; 215 else 216 t->tm_year = yearset + 1900; 217 } 218 t->tm_year -= 1900; /* Convert to UNIX time. */ 219 /* FALLTHROUGH */ 220 case 8: /* MMDDhhmm */ 221 t->tm_mon = ATOI2(arg); 222 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 223 t->tm_mday = ATOI2(arg); 224 t->tm_hour = ATOI2(arg); 225 t->tm_min = ATOI2(arg); 226 break; 227 default: 228 goto terr; 229 } 230 231 t->tm_isdst = -1; /* Figure out DST. */ 232 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 233 if (tvp[0].tv_sec == -1) 234 terr: errx(1, 235 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 236 237 tvp[0].tv_usec = tvp[1].tv_usec = 0; 238 } 239 240 void 241 stime_arg2(char *arg, int year, struct timeval *tvp) 242 { 243 time_t now; 244 struct tm *t; 245 /* Start with the current time. */ 246 now = tvp[0].tv_sec; 247 if ((t = localtime(&now)) == NULL) 248 err(1, "localtime"); 249 250 t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */ 251 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 252 t->tm_mday = ATOI2(arg); 253 t->tm_hour = ATOI2(arg); 254 t->tm_min = ATOI2(arg); 255 if (year) { 256 t->tm_year = ATOI2(arg); 257 if (t->tm_year < 39) /* support 2000-2038 not 1902-1969 */ 258 t->tm_year += 100; 259 } 260 261 t->tm_isdst = -1; /* Figure out DST. */ 262 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 263 if (tvp[0].tv_sec == -1) 264 errx(1, 265 "out of range or illegal time specification: MMDDhhmm[yy]"); 266 267 tvp[0].tv_usec = tvp[1].tv_usec = 0; 268 } 269 270 void 271 stime_file(char *fname, struct timeval *tvp) 272 { 273 struct stat sb; 274 275 if (stat(fname, &sb)) 276 err(1, "%s", fname); 277 TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec); 278 TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec); 279 } 280 281 int 282 rw(char *fname, struct stat *sbp, int force) 283 { 284 int fd, needed_chmod, rval; 285 u_char byte; 286 287 /* Try regular files. */ 288 if (!S_ISREG(sbp->st_mode)) { 289 warnx("%s: %s", fname, strerror(EFTYPE)); 290 return (1); 291 } 292 293 needed_chmod = rval = 0; 294 if ((fd = open(fname, O_RDWR, 0)) == -1) { 295 if (!force || chmod(fname, DEFFILEMODE)) 296 goto err; 297 if ((fd = open(fname, O_RDWR, 0)) == -1) 298 goto err; 299 needed_chmod = 1; 300 } 301 302 if (sbp->st_size != 0) { 303 if (read(fd, &byte, sizeof(byte)) != sizeof(byte)) 304 goto err; 305 if (lseek(fd, (off_t)0, SEEK_SET) == -1) 306 goto err; 307 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) 308 goto err; 309 } else { 310 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) { 311 err: rval = 1; 312 warn("%s", fname); 313 } else if (ftruncate(fd, (off_t)0)) { 314 rval = 1; 315 warn("%s: file modified", fname); 316 } 317 } 318 319 if (close(fd) && rval != 1) { 320 rval = 1; 321 warn("%s", fname); 322 } 323 if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) { 324 rval = 1; 325 warn("%s: permissions modified", fname); 326 } 327 return (rval); 328 } 329 330 void 331 usage(void) 332 { 333 (void)fprintf(stderr, "usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n"); 334 exit(1); 335 } 336