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