touch.c revision 1.10
1/* $OpenBSD: touch.c,v 1.10 2003/06/10 22:20:53 deraadt 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#ifndef lint 34static char copyright[] = 35"@(#) Copyright (c) 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37#endif /* not lint */ 38 39#ifndef lint 40#if 0 41static char sccsid[] = "@(#)touch.c 8.2 (Berkeley) 4/28/95"; 42#endif 43static char rcsid[] = "$OpenBSD: touch.c,v 1.10 2003/06/10 22:20:53 deraadt Exp $"; 44#endif /* not lint */ 45 46#include <sys/types.h> 47#include <sys/stat.h> 48#include <sys/time.h> 49 50#include <err.h> 51#include <errno.h> 52#include <fcntl.h> 53#include <stdio.h> 54#include <stdlib.h> 55#include <string.h> 56#include <locale.h> 57#include <time.h> 58#include <tzfile.h> 59#include <unistd.h> 60 61int rw(char *, struct stat *, int); 62void stime_arg1(char *, struct timeval *); 63void stime_arg2(char *, int, struct timeval *); 64void stime_file(char *, struct timeval *); 65void usage(void); 66 67int 68main(int argc, char *argv[]) 69{ 70 struct stat sb; 71 struct timeval tv[2]; 72 int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset; 73 char *p; 74 75 setlocale(LC_ALL, ""); 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 (rw(*argv, &sb, fflag)) 180 rval = 1; 181 } 182 exit(rval); 183} 184 185#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 186 187void 188stime_arg1(char *arg, struct timeval *tvp) 189{ 190 struct tm *t; 191 time_t tmptime; 192 int yearset; 193 char *p; 194 /* Start with the current time. */ 195 tmptime = tvp[0].tv_sec; 196 if ((t = localtime(&tmptime)) == NULL) 197 err(1, "localtime"); 198 /* [[CC]YY]MMDDhhmm[.SS] */ 199 if ((p = strchr(arg, '.')) == NULL) 200 t->tm_sec = 0; /* Seconds defaults to 0. */ 201 else { 202 if (strlen(p + 1) != 2) 203 goto terr; 204 *p++ = '\0'; 205 t->tm_sec = ATOI2(p); 206 } 207 208 yearset = 0; 209 switch(strlen(arg)) { 210 case 12: /* CCYYMMDDhhmm */ 211 t->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE; 212 yearset = 1; 213 /* FALLTHROUGH */ 214 case 10: /* YYMMDDhhmm */ 215 if (yearset) { 216 yearset = ATOI2(arg); 217 t->tm_year += yearset; 218 } else { 219 yearset = ATOI2(arg); 220 if (yearset < 69) 221 t->tm_year = yearset + 2000 - TM_YEAR_BASE; 222 else 223 t->tm_year = yearset + 1900 - TM_YEAR_BASE; 224 } 225 /* FALLTHROUGH */ 226 case 8: /* MMDDhhmm */ 227 t->tm_mon = ATOI2(arg); 228 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 229 t->tm_mday = ATOI2(arg); 230 t->tm_hour = ATOI2(arg); 231 t->tm_min = ATOI2(arg); 232 break; 233 default: 234 goto terr; 235 } 236 237 t->tm_isdst = -1; /* Figure out DST. */ 238 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 239 if (tvp[0].tv_sec == -1) 240terr: errx(1, 241 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 242 243 tvp[0].tv_usec = tvp[1].tv_usec = 0; 244} 245 246void 247stime_arg2(char *arg, int year, struct timeval *tvp) 248{ 249 struct tm *t; 250 time_t tmptime; 251 /* Start with the current time. */ 252 tmptime = tvp[0].tv_sec; 253 if ((t = localtime(&tmptime)) == NULL) 254 err(1, "localtime"); 255 256 t->tm_mon = ATOI2(arg); /* MMDDhhmm[YY] */ 257 --t->tm_mon; /* Convert from 01-12 to 00-11 */ 258 t->tm_mday = ATOI2(arg); 259 t->tm_hour = ATOI2(arg); 260 t->tm_min = ATOI2(arg); 261 if (year) { 262 year = ATOI2(arg); 263 if (year < 69) 264 t->tm_year = year + 2000 - TM_YEAR_BASE; 265 else 266 t->tm_year = year + 1900 - TM_YEAR_BASE; 267 } 268 t->tm_sec = 0; 269 270 t->tm_isdst = -1; /* Figure out DST. */ 271 tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 272 if (tvp[0].tv_sec == -1) 273 errx(1, 274 "out of range or illegal time specification: MMDDhhmm[YY]"); 275 276 tvp[0].tv_usec = tvp[1].tv_usec = 0; 277} 278 279void 280stime_file(char *fname, struct timeval *tvp) 281{ 282 struct stat sb; 283 284 if (stat(fname, &sb)) 285 err(1, "%s", fname); 286 TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec); 287 TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec); 288} 289 290int 291rw(char *fname, struct stat *sbp, int force) 292{ 293 int fd, needed_chmod, rval; 294 u_char byte; 295 296 /* Try regular files and directories. */ 297 if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) { 298 warnx("%s: %s", fname, strerror(EFTYPE)); 299 return (1); 300 } 301 302 needed_chmod = rval = 0; 303 if ((fd = open(fname, O_RDWR, 0)) == -1) { 304 if (!force || chmod(fname, DEFFILEMODE)) 305 goto err; 306 if ((fd = open(fname, O_RDWR, 0)) == -1) 307 goto err; 308 needed_chmod = 1; 309 } 310 311 if (sbp->st_size != 0) { 312 if (read(fd, &byte, sizeof(byte)) != sizeof(byte)) 313 goto err; 314 if (lseek(fd, (off_t)0, SEEK_SET) == -1) 315 goto err; 316 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) 317 goto err; 318 } else { 319 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) { 320err: rval = 1; 321 warn("%s", fname); 322 } else if (ftruncate(fd, (off_t)0)) { 323 rval = 1; 324 warn("%s: file modified", fname); 325 } 326 } 327 328 if (close(fd) && rval != 1) { 329 rval = 1; 330 warn("%s", fname); 331 } 332 if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) { 333 rval = 1; 334 warn("%s: permissions modified", fname); 335 } 336 return (rval); 337} 338 339__dead void 340usage(void) 341{ 342 (void)fprintf(stderr, 343 "usage: touch [-acfm] [-r file] [-t time] file ...\n"); 344 exit(1); 345} 346