19b50d902SRodney W. Grimes /* 29b50d902SRodney W. Grimes * Copyright (c) 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 359b50d902SRodney W. Grimes static char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 419b50d902SRodney W. Grimes static char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93"; 429b50d902SRodney W. Grimes #endif /* not lint */ 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #include <sys/types.h> 459b50d902SRodney W. Grimes #include <sys/stat.h> 469b50d902SRodney W. Grimes #include <sys/time.h> 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include <err.h> 499b50d902SRodney W. Grimes #include <errno.h> 509b50d902SRodney W. Grimes #include <fcntl.h> 519b50d902SRodney W. Grimes #include <stdio.h> 529b50d902SRodney W. Grimes #include <stdlib.h> 539b50d902SRodney W. Grimes #include <string.h> 549b50d902SRodney W. Grimes #include <time.h> 559b50d902SRodney W. Grimes #include <unistd.h> 569b50d902SRodney W. Grimes 579b50d902SRodney W. Grimes int rw __P((char *, struct stat *, int)); 589b50d902SRodney W. Grimes void stime_arg1 __P((char *, struct timeval *)); 599b50d902SRodney W. Grimes void stime_arg2 __P((char *, int, struct timeval *)); 609b50d902SRodney W. Grimes void stime_file __P((char *, struct timeval *)); 619b50d902SRodney W. Grimes void usage __P((void)); 629b50d902SRodney W. Grimes 639b50d902SRodney W. Grimes int 649b50d902SRodney W. Grimes main(argc, argv) 659b50d902SRodney W. Grimes int argc; 669b50d902SRodney W. Grimes char *argv[]; 679b50d902SRodney W. Grimes { 689b50d902SRodney W. Grimes struct stat sb; 699b50d902SRodney W. Grimes struct timeval tv[2]; 709b50d902SRodney W. Grimes int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset; 719b50d902SRodney W. Grimes char *p; 729b50d902SRodney W. Grimes 739b50d902SRodney W. Grimes aflag = cflag = fflag = mflag = timeset = 0; 749b50d902SRodney W. Grimes if (gettimeofday(&tv[0], NULL)) 759b50d902SRodney W. Grimes err(1, "gettimeofday"); 769b50d902SRodney W. Grimes 771c8af878SWarner Losh while ((ch = getopt(argc, argv, "acfmr:t:")) != -1) 789b50d902SRodney W. Grimes switch(ch) { 799b50d902SRodney W. Grimes case 'a': 809b50d902SRodney W. Grimes aflag = 1; 819b50d902SRodney W. Grimes break; 829b50d902SRodney W. Grimes case 'c': 839b50d902SRodney W. Grimes cflag = 1; 849b50d902SRodney W. Grimes break; 859b50d902SRodney W. Grimes case 'f': 869b50d902SRodney W. Grimes fflag = 1; 879b50d902SRodney W. Grimes break; 889b50d902SRodney W. Grimes case 'm': 899b50d902SRodney W. Grimes mflag = 1; 909b50d902SRodney W. Grimes break; 919b50d902SRodney W. Grimes case 'r': 929b50d902SRodney W. Grimes timeset = 1; 939b50d902SRodney W. Grimes stime_file(optarg, tv); 949b50d902SRodney W. Grimes break; 959b50d902SRodney W. Grimes case 't': 969b50d902SRodney W. Grimes timeset = 1; 979b50d902SRodney W. Grimes stime_arg1(optarg, tv); 989b50d902SRodney W. Grimes break; 999b50d902SRodney W. Grimes case '?': 1009b50d902SRodney W. Grimes default: 1019b50d902SRodney W. Grimes usage(); 1029b50d902SRodney W. Grimes } 1039b50d902SRodney W. Grimes argc -= optind; 1049b50d902SRodney W. Grimes argv += optind; 1059b50d902SRodney W. Grimes 1069b50d902SRodney W. Grimes /* Default is both -a and -m. */ 1079b50d902SRodney W. Grimes if (aflag == 0 && mflag == 0) 1089b50d902SRodney W. Grimes aflag = mflag = 1; 1099b50d902SRodney W. Grimes 1109b50d902SRodney W. Grimes /* 1119b50d902SRodney W. Grimes * If no -r or -t flag, at least two operands, the first of which 1129b50d902SRodney W. Grimes * is an 8 or 10 digit number, use the obsolete time specification. 1139b50d902SRodney W. Grimes */ 1149b50d902SRodney W. Grimes if (!timeset && argc > 1) { 1159b50d902SRodney W. Grimes (void)strtol(argv[0], &p, 10); 1169b50d902SRodney W. Grimes len = p - argv[0]; 1179b50d902SRodney W. Grimes if (*p == '\0' && (len == 8 || len == 10)) { 1189b50d902SRodney W. Grimes timeset = 1; 1199b50d902SRodney W. Grimes stime_arg2(*argv++, len == 10, tv); 1209b50d902SRodney W. Grimes } 1219b50d902SRodney W. Grimes } 1229b50d902SRodney W. Grimes 1239b50d902SRodney W. Grimes /* Otherwise use the current time of day. */ 1249b50d902SRodney W. Grimes if (!timeset) 1259b50d902SRodney W. Grimes tv[1] = tv[0]; 1269b50d902SRodney W. Grimes 1279b50d902SRodney W. Grimes if (*argv == NULL) 1289b50d902SRodney W. Grimes usage(); 1299b50d902SRodney W. Grimes 1309b50d902SRodney W. Grimes for (rval = 0; *argv; ++argv) { 1319b50d902SRodney W. Grimes /* See if the file exists. */ 1329ef5c48bSBill Fumerola if (stat(*argv, &sb)) { 1339b50d902SRodney W. Grimes if (!cflag) { 1349b50d902SRodney W. Grimes /* Create the file. */ 1359b50d902SRodney W. Grimes fd = open(*argv, 1369b50d902SRodney W. Grimes O_WRONLY | O_CREAT, DEFFILEMODE); 1379b50d902SRodney W. Grimes if (fd == -1 || fstat(fd, &sb) || close(fd)) { 1389b50d902SRodney W. Grimes rval = 1; 1399b50d902SRodney W. Grimes warn("%s", *argv); 1409b50d902SRodney W. Grimes continue; 1419b50d902SRodney W. Grimes } 1429b50d902SRodney W. Grimes 1439b50d902SRodney W. Grimes /* If using the current time, we're done. */ 1449b50d902SRodney W. Grimes if (!timeset) 1459b50d902SRodney W. Grimes continue; 1469b50d902SRodney W. Grimes } else 1479b50d902SRodney W. Grimes continue; 1489ef5c48bSBill Fumerola } 1499b50d902SRodney W. Grimes 1509b50d902SRodney W. Grimes if (!aflag) 1519b50d902SRodney W. Grimes TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec); 1529b50d902SRodney W. Grimes if (!mflag) 1539b50d902SRodney W. Grimes TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec); 1549b50d902SRodney W. Grimes 1559b50d902SRodney W. Grimes /* Try utimes(2). */ 1569b50d902SRodney W. Grimes if (!utimes(*argv, tv)) 1579b50d902SRodney W. Grimes continue; 1589b50d902SRodney W. Grimes 1599b50d902SRodney W. Grimes /* If the user specified a time, nothing else we can do. */ 1609b50d902SRodney W. Grimes if (timeset) { 1619b50d902SRodney W. Grimes rval = 1; 1629b50d902SRodney W. Grimes warn("%s", *argv); 1639b50d902SRodney W. Grimes } 1649b50d902SRodney W. Grimes 1659b50d902SRodney W. Grimes /* 1669b50d902SRodney W. Grimes * System V and POSIX 1003.1 require that a NULL argument 1679b50d902SRodney W. Grimes * set the access/modification times to the current time. 1689b50d902SRodney W. Grimes * The permission checks are different, too, in that the 1699b50d902SRodney W. Grimes * ability to write the file is sufficient. Take a shot. 1709b50d902SRodney W. Grimes */ 1719b50d902SRodney W. Grimes if (!utimes(*argv, NULL)) 1729b50d902SRodney W. Grimes continue; 1739b50d902SRodney W. Grimes 1749b50d902SRodney W. Grimes /* Try reading/writing. */ 1759b50d902SRodney W. Grimes if (rw(*argv, &sb, fflag)) 1769b50d902SRodney W. Grimes rval = 1; 1779b50d902SRodney W. Grimes } 1789b50d902SRodney W. Grimes exit(rval); 1799b50d902SRodney W. Grimes } 1809b50d902SRodney W. Grimes 1819b50d902SRodney W. Grimes #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 1829b50d902SRodney W. Grimes 1839b50d902SRodney W. Grimes void 1849b50d902SRodney W. Grimes stime_arg1(arg, tvp) 1859b50d902SRodney W. Grimes char *arg; 1869b50d902SRodney W. Grimes struct timeval *tvp; 1879b50d902SRodney W. Grimes { 18839470616SBruce Evans time_t now; 1899b50d902SRodney W. Grimes struct tm *t; 1909b50d902SRodney W. Grimes int yearset; 1919b50d902SRodney W. Grimes char *p; 1929b50d902SRodney W. Grimes /* Start with the current time. */ 19339470616SBruce Evans now = tvp[0].tv_sec; 19439470616SBruce Evans if ((t = localtime(&now)) == NULL) 1959b50d902SRodney W. Grimes err(1, "localtime"); 1969b50d902SRodney W. Grimes /* [[CC]YY]MMDDhhmm[.SS] */ 1979b50d902SRodney W. Grimes if ((p = strchr(arg, '.')) == NULL) 1989b50d902SRodney W. Grimes t->tm_sec = 0; /* Seconds defaults to 0. */ 1999b50d902SRodney W. Grimes else { 2009b50d902SRodney W. Grimes if (strlen(p + 1) != 2) 2019b50d902SRodney W. Grimes goto terr; 2029b50d902SRodney W. Grimes *p++ = '\0'; 2039b50d902SRodney W. Grimes t->tm_sec = ATOI2(p); 2049b50d902SRodney W. Grimes } 2059b50d902SRodney W. Grimes 2069b50d902SRodney W. Grimes yearset = 0; 2079b50d902SRodney W. Grimes switch(strlen(arg)) { 2089b50d902SRodney W. Grimes case 12: /* CCYYMMDDhhmm */ 2099b50d902SRodney W. Grimes t->tm_year = ATOI2(arg); 210d38ca307SJoerg Wunsch t->tm_year *= 100; 2119b50d902SRodney W. Grimes yearset = 1; 2129b50d902SRodney W. Grimes /* FALLTHOUGH */ 2139b50d902SRodney W. Grimes case 10: /* YYMMDDhhmm */ 2149b50d902SRodney W. Grimes if (yearset) { 2159b50d902SRodney W. Grimes yearset = ATOI2(arg); 2169b50d902SRodney W. Grimes t->tm_year += yearset; 2179b50d902SRodney W. Grimes } else { 2189b50d902SRodney W. Grimes yearset = ATOI2(arg); 2199b50d902SRodney W. Grimes if (yearset < 69) 2209b50d902SRodney W. Grimes t->tm_year = yearset + 2000; 2219b50d902SRodney W. Grimes else 2229b50d902SRodney W. Grimes t->tm_year = yearset + 1900; 2239b50d902SRodney W. Grimes } 2249b50d902SRodney W. Grimes t->tm_year -= 1900; /* Convert to UNIX time. */ 2259b50d902SRodney W. Grimes /* FALLTHROUGH */ 2269b50d902SRodney W. Grimes case 8: /* MMDDhhmm */ 2279b50d902SRodney W. Grimes t->tm_mon = ATOI2(arg); 2289b50d902SRodney W. Grimes --t->tm_mon; /* Convert from 01-12 to 00-11 */ 2299b50d902SRodney W. Grimes t->tm_mday = ATOI2(arg); 2309b50d902SRodney W. Grimes t->tm_hour = ATOI2(arg); 2319b50d902SRodney W. Grimes t->tm_min = ATOI2(arg); 2329b50d902SRodney W. Grimes break; 2339b50d902SRodney W. Grimes default: 2349b50d902SRodney W. Grimes goto terr; 2359b50d902SRodney W. Grimes } 2369b50d902SRodney W. Grimes 2379b50d902SRodney W. Grimes t->tm_isdst = -1; /* Figure out DST. */ 2389b50d902SRodney W. Grimes tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 2399b50d902SRodney W. Grimes if (tvp[0].tv_sec == -1) 2409b50d902SRodney W. Grimes terr: errx(1, 2419b50d902SRodney W. Grimes "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 2429b50d902SRodney W. Grimes 2439b50d902SRodney W. Grimes tvp[0].tv_usec = tvp[1].tv_usec = 0; 2449b50d902SRodney W. Grimes } 2459b50d902SRodney W. Grimes 2469b50d902SRodney W. Grimes void 2479b50d902SRodney W. Grimes stime_arg2(arg, year, tvp) 2489b50d902SRodney W. Grimes char *arg; 2499b50d902SRodney W. Grimes int year; 2509b50d902SRodney W. Grimes struct timeval *tvp; 2519b50d902SRodney W. Grimes { 25239470616SBruce Evans time_t now; 2539b50d902SRodney W. Grimes struct tm *t; 2549b50d902SRodney W. Grimes /* Start with the current time. */ 25539470616SBruce Evans now = tvp[0].tv_sec; 25639470616SBruce Evans if ((t = localtime(&now)) == NULL) 2579b50d902SRodney W. Grimes err(1, "localtime"); 2589b50d902SRodney W. Grimes 2599b50d902SRodney W. Grimes t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */ 2609b50d902SRodney W. Grimes --t->tm_mon; /* Convert from 01-12 to 00-11 */ 2619b50d902SRodney W. Grimes t->tm_mday = ATOI2(arg); 2629b50d902SRodney W. Grimes t->tm_hour = ATOI2(arg); 2639b50d902SRodney W. Grimes t->tm_min = ATOI2(arg); 26434c7ff49SDaniel O'Callaghan if (year) { 2659b50d902SRodney W. Grimes t->tm_year = ATOI2(arg); 26625c4d008SDaniel O'Callaghan if (t->tm_year < 39) /* support 2000-2038 not 1902-1969 */ 26734c7ff49SDaniel O'Callaghan t->tm_year += 100; 26834c7ff49SDaniel O'Callaghan } 2699b50d902SRodney W. Grimes 2709b50d902SRodney W. Grimes t->tm_isdst = -1; /* Figure out DST. */ 2719b50d902SRodney W. Grimes tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 2729b50d902SRodney W. Grimes if (tvp[0].tv_sec == -1) 2739b50d902SRodney W. Grimes errx(1, 2749b50d902SRodney W. Grimes "out of range or illegal time specification: MMDDhhmm[yy]"); 2759b50d902SRodney W. Grimes 2769b50d902SRodney W. Grimes tvp[0].tv_usec = tvp[1].tv_usec = 0; 2779b50d902SRodney W. Grimes } 2789b50d902SRodney W. Grimes 2799b50d902SRodney W. Grimes void 2809b50d902SRodney W. Grimes stime_file(fname, tvp) 2819b50d902SRodney W. Grimes char *fname; 2829b50d902SRodney W. Grimes struct timeval *tvp; 2839b50d902SRodney W. Grimes { 2849b50d902SRodney W. Grimes struct stat sb; 2859b50d902SRodney W. Grimes 2869b50d902SRodney W. Grimes if (stat(fname, &sb)) 2879b50d902SRodney W. Grimes err(1, "%s", fname); 2889b50d902SRodney W. Grimes TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec); 2899b50d902SRodney W. Grimes TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec); 2909b50d902SRodney W. Grimes } 2919b50d902SRodney W. Grimes 2929b50d902SRodney W. Grimes int 2939b50d902SRodney W. Grimes rw(fname, sbp, force) 2949b50d902SRodney W. Grimes char *fname; 2959b50d902SRodney W. Grimes struct stat *sbp; 2969b50d902SRodney W. Grimes int force; 2979b50d902SRodney W. Grimes { 2989b50d902SRodney W. Grimes int fd, needed_chmod, rval; 2999b50d902SRodney W. Grimes u_char byte; 3009b50d902SRodney W. Grimes 3019b50d902SRodney W. Grimes /* Try regular files and directories. */ 3029b50d902SRodney W. Grimes if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) { 3039b50d902SRodney W. Grimes warnx("%s: %s", fname, strerror(EFTYPE)); 3049b50d902SRodney W. Grimes return (1); 3059b50d902SRodney W. Grimes } 3069b50d902SRodney W. Grimes 3079b50d902SRodney W. Grimes needed_chmod = rval = 0; 3089b50d902SRodney W. Grimes if ((fd = open(fname, O_RDWR, 0)) == -1) { 3099b50d902SRodney W. Grimes if (!force || chmod(fname, DEFFILEMODE)) 3109b50d902SRodney W. Grimes goto err; 3119b50d902SRodney W. Grimes if ((fd = open(fname, O_RDWR, 0)) == -1) 3129b50d902SRodney W. Grimes goto err; 3139b50d902SRodney W. Grimes needed_chmod = 1; 3149b50d902SRodney W. Grimes } 3159b50d902SRodney W. Grimes 3169b50d902SRodney W. Grimes if (sbp->st_size != 0) { 3179b50d902SRodney W. Grimes if (read(fd, &byte, sizeof(byte)) != sizeof(byte)) 3189b50d902SRodney W. Grimes goto err; 3199b50d902SRodney W. Grimes if (lseek(fd, (off_t)0, SEEK_SET) == -1) 3209b50d902SRodney W. Grimes goto err; 3219b50d902SRodney W. Grimes if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) 3229b50d902SRodney W. Grimes goto err; 3239b50d902SRodney W. Grimes } else { 3249b50d902SRodney W. Grimes if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) { 3259b50d902SRodney W. Grimes err: rval = 1; 3269b50d902SRodney W. Grimes warn("%s", fname); 3279b50d902SRodney W. Grimes } else if (ftruncate(fd, (off_t)0)) { 3289b50d902SRodney W. Grimes rval = 1; 3299b50d902SRodney W. Grimes warn("%s: file modified", fname); 3309b50d902SRodney W. Grimes } 3319b50d902SRodney W. Grimes } 3329b50d902SRodney W. Grimes 3339b50d902SRodney W. Grimes if (close(fd) && rval != 1) { 3349b50d902SRodney W. Grimes rval = 1; 3359b50d902SRodney W. Grimes warn("%s", fname); 3369b50d902SRodney W. Grimes } 3379b50d902SRodney W. Grimes if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) { 3389b50d902SRodney W. Grimes rval = 1; 3399b50d902SRodney W. Grimes warn("%s: permissions modified", fname); 3409b50d902SRodney W. Grimes } 3419b50d902SRodney W. Grimes return (rval); 3429b50d902SRodney W. Grimes } 3439b50d902SRodney W. Grimes 344eaa86f9dSBruce Evans void 3459b50d902SRodney W. Grimes usage() 3469b50d902SRodney W. Grimes { 347ecb80458SDag-Erling Smørgrav (void)fprintf(stderr, "usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n"); 3489b50d902SRodney W. Grimes exit(1); 3499b50d902SRodney W. Grimes } 350