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 341a9e1c9dSMark Murray #include <sys/cdefs.h> 351a9e1c9dSMark Murray 361a9e1c9dSMark Murray __FBSDID("$FreeBSD$"); 371a9e1c9dSMark Murray 389b50d902SRodney W. Grimes #ifndef lint 3916dd925fSKris Kennaway static const char copyright[] = 409b50d902SRodney W. Grimes "@(#) Copyright (c) 1993\n\ 419b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 421a9e1c9dSMark Murray #endif 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #ifndef lint 4516dd925fSKris Kennaway static const char sccsid[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93"; 4616dd925fSKris Kennaway #endif 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include <sys/types.h> 499b50d902SRodney W. Grimes #include <sys/stat.h> 509b50d902SRodney W. Grimes #include <sys/time.h> 519b50d902SRodney W. Grimes 529b50d902SRodney W. Grimes #include <err.h> 539b50d902SRodney W. Grimes #include <errno.h> 549b50d902SRodney W. Grimes #include <fcntl.h> 551b54381bSGreg Lehey #include <libgen.h> 569b50d902SRodney W. Grimes #include <stdio.h> 579b50d902SRodney W. Grimes #include <stdlib.h> 589b50d902SRodney W. Grimes #include <string.h> 599b50d902SRodney W. Grimes #include <time.h> 609b50d902SRodney W. Grimes #include <unistd.h> 619b50d902SRodney W. Grimes 623f330d7dSWarner Losh int rw(char *, struct stat *, int); 633f330d7dSWarner Losh void stime_arg1(char *, struct timeval *); 643f330d7dSWarner Losh void stime_arg2(char *, int, struct timeval *); 653f330d7dSWarner Losh void stime_file(char *, struct timeval *); 663481910dSGreg Lehey int timeoffset(char *); 673481910dSGreg Lehey void usage(char *); 689b50d902SRodney W. Grimes 699b50d902SRodney W. Grimes int 70f4ac32deSDavid Malone main(int argc, char *argv[]) 719b50d902SRodney W. Grimes { 729b50d902SRodney W. Grimes struct stat sb; 739b50d902SRodney W. Grimes struct timeval tv[2]; 74842d1c6cSDavid E. O'Brien int (*stat_f)(const char *, struct stat *); 75842d1c6cSDavid E. O'Brien int (*utimes_f)(const char *, const struct timeval *); 763481910dSGreg Lehey int Aflag, aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset; 779b50d902SRodney W. Grimes char *p; 783481910dSGreg Lehey char *myname; 799b50d902SRodney W. Grimes 801b54381bSGreg Lehey myname = basename(argv[0]); 813481910dSGreg Lehey Aflag = aflag = cflag = fflag = mflag = timeset = 0; 82842d1c6cSDavid E. O'Brien stat_f = stat; 83842d1c6cSDavid E. O'Brien utimes_f = utimes; 849b50d902SRodney W. Grimes if (gettimeofday(&tv[0], NULL)) 859b50d902SRodney W. Grimes err(1, "gettimeofday"); 869b50d902SRodney W. Grimes 873481910dSGreg Lehey while ((ch = getopt(argc, argv, "A:acfhmr:t:")) != -1) 889b50d902SRodney W. Grimes switch(ch) { 893481910dSGreg Lehey case 'A': 903481910dSGreg Lehey Aflag = timeoffset(optarg); 913481910dSGreg Lehey break; 929b50d902SRodney W. Grimes case 'a': 939b50d902SRodney W. Grimes aflag = 1; 949b50d902SRodney W. Grimes break; 959b50d902SRodney W. Grimes case 'c': 969b50d902SRodney W. Grimes cflag = 1; 979b50d902SRodney W. Grimes break; 989b50d902SRodney W. Grimes case 'f': 999b50d902SRodney W. Grimes fflag = 1; 1009b50d902SRodney W. Grimes break; 101842d1c6cSDavid E. O'Brien case 'h': 102842d1c6cSDavid E. O'Brien cflag = 1; 103842d1c6cSDavid E. O'Brien stat_f = lstat; 104842d1c6cSDavid E. O'Brien utimes_f = lutimes; 105842d1c6cSDavid E. O'Brien break; 1069b50d902SRodney W. Grimes case 'm': 1079b50d902SRodney W. Grimes mflag = 1; 1089b50d902SRodney W. Grimes break; 1099b50d902SRodney W. Grimes case 'r': 1109b50d902SRodney W. Grimes timeset = 1; 1119b50d902SRodney W. Grimes stime_file(optarg, tv); 1129b50d902SRodney W. Grimes break; 1139b50d902SRodney W. Grimes case 't': 1149b50d902SRodney W. Grimes timeset = 1; 1159b50d902SRodney W. Grimes stime_arg1(optarg, tv); 1169b50d902SRodney W. Grimes break; 1179b50d902SRodney W. Grimes case '?': 1189b50d902SRodney W. Grimes default: 1193481910dSGreg Lehey usage(myname); 1209b50d902SRodney W. Grimes } 1219b50d902SRodney W. Grimes argc -= optind; 1229b50d902SRodney W. Grimes argv += optind; 1239b50d902SRodney W. Grimes 1241b54381bSGreg Lehey if (aflag == 0 && mflag == 0) 1259b50d902SRodney W. Grimes aflag = mflag = 1; 1269b50d902SRodney W. Grimes 1271b54381bSGreg Lehey if (timeset) { 1281b54381bSGreg Lehey if (Aflag) { 1299b50d902SRodney W. Grimes /* 1301b54381bSGreg Lehey * We're setting the time to an offset from a specified 1311b54381bSGreg Lehey * time. God knows why, but it means that we can set 1321b54381bSGreg Lehey * that time once and for all here. 1339b50d902SRodney W. Grimes */ 1341b54381bSGreg Lehey if (aflag) 1351b54381bSGreg Lehey tv[0].tv_sec += Aflag; 1361b54381bSGreg Lehey if (mflag) 1371b54381bSGreg Lehey tv[1].tv_sec += Aflag; 1381b54381bSGreg Lehey Aflag = 0; /* done our job */ 1391b54381bSGreg Lehey } 1401b54381bSGreg Lehey } else { 1411b54381bSGreg Lehey /* 1421b54381bSGreg Lehey * If no -r or -t flag, at least two operands, the first of 1431b54381bSGreg Lehey * which is an 8 or 10 digit number, use the obsolete time 1441b54381bSGreg Lehey * specification, otherwise use the current time. 1451b54381bSGreg Lehey */ 1461b54381bSGreg Lehey if (argc > 1) { 1471b54381bSGreg Lehey strtol(argv[0], &p, 10); 1489b50d902SRodney W. Grimes len = p - argv[0]; 1499b50d902SRodney W. Grimes if (*p == '\0' && (len == 8 || len == 10)) { 1509b50d902SRodney W. Grimes timeset = 1; 1519b50d902SRodney W. Grimes stime_arg2(*argv++, len == 10, tv); 1529b50d902SRodney W. Grimes } 1539b50d902SRodney W. Grimes } 1541b54381bSGreg Lehey /* Both times default to the same. */ 1559b50d902SRodney W. Grimes tv[1] = tv[0]; 1561b54381bSGreg Lehey } 1579b50d902SRodney W. Grimes 1589b50d902SRodney W. Grimes if (*argv == NULL) 1593481910dSGreg Lehey usage(myname); 1609b50d902SRodney W. Grimes 1611b54381bSGreg Lehey if (Aflag) 1621b54381bSGreg Lehey cflag = 1; 1631b54381bSGreg Lehey 1649b50d902SRodney W. Grimes for (rval = 0; *argv; ++argv) { 1659b50d902SRodney W. Grimes /* See if the file exists. */ 166842d1c6cSDavid E. O'Brien if (stat_f(*argv, &sb) != 0) { 167c64b097bSJaakko Heinonen if (errno != ENOENT) { 168c64b097bSJaakko Heinonen rval = 1; 169c64b097bSJaakko Heinonen warn("%s", *argv); 170c64b097bSJaakko Heinonen continue; 171c64b097bSJaakko Heinonen } 1729b50d902SRodney W. Grimes if (!cflag) { 1739b50d902SRodney W. Grimes /* Create the file. */ 1749b50d902SRodney W. Grimes fd = open(*argv, 1759b50d902SRodney W. Grimes O_WRONLY | O_CREAT, DEFFILEMODE); 1769b50d902SRodney W. Grimes if (fd == -1 || fstat(fd, &sb) || close(fd)) { 1779b50d902SRodney W. Grimes rval = 1; 1789b50d902SRodney W. Grimes warn("%s", *argv); 1799b50d902SRodney W. Grimes continue; 1809b50d902SRodney W. Grimes } 1819b50d902SRodney W. Grimes 1829b50d902SRodney W. Grimes /* If using the current time, we're done. */ 1839b50d902SRodney W. Grimes if (!timeset) 1849b50d902SRodney W. Grimes continue; 1859b50d902SRodney W. Grimes } else 1869b50d902SRodney W. Grimes continue; 1879ef5c48bSBill Fumerola } 1889b50d902SRodney W. Grimes 1899b50d902SRodney W. Grimes if (!aflag) 1909b50d902SRodney W. Grimes TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec); 1919b50d902SRodney W. Grimes if (!mflag) 1929b50d902SRodney W. Grimes TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec); 1931b54381bSGreg Lehey 1941b54381bSGreg Lehey /* 1951b54381bSGreg Lehey * We're adjusting the times based on the file times, not a 1961b54381bSGreg Lehey * specified time (that gets handled above). 1971b54381bSGreg Lehey */ 1983481910dSGreg Lehey if (Aflag) { 1991b54381bSGreg Lehey if (aflag) { 2001b54381bSGreg Lehey TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec); 2013481910dSGreg Lehey tv[0].tv_sec += Aflag; 2021b54381bSGreg Lehey } 2031b54381bSGreg Lehey if (mflag) { 2041b54381bSGreg Lehey TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec); 2053481910dSGreg Lehey tv[1].tv_sec += Aflag; 2063481910dSGreg Lehey } 2071b54381bSGreg Lehey } 2089b50d902SRodney W. Grimes 2099b50d902SRodney W. Grimes /* Try utimes(2). */ 210842d1c6cSDavid E. O'Brien if (!utimes_f(*argv, tv)) 2119b50d902SRodney W. Grimes continue; 2129b50d902SRodney W. Grimes 2139b50d902SRodney W. Grimes /* If the user specified a time, nothing else we can do. */ 214c64b097bSJaakko Heinonen if (timeset || Aflag) { 2159b50d902SRodney W. Grimes rval = 1; 2169b50d902SRodney W. Grimes warn("%s", *argv); 217fb1a11d7SAndrey A. Chernov continue; 2189b50d902SRodney W. Grimes } 2199b50d902SRodney W. Grimes 2209b50d902SRodney W. Grimes /* 2219b50d902SRodney W. Grimes * System V and POSIX 1003.1 require that a NULL argument 2229b50d902SRodney W. Grimes * set the access/modification times to the current time. 2239b50d902SRodney W. Grimes * The permission checks are different, too, in that the 2249b50d902SRodney W. Grimes * ability to write the file is sufficient. Take a shot. 2259b50d902SRodney W. Grimes */ 226842d1c6cSDavid E. O'Brien if (!utimes_f(*argv, NULL)) 2279b50d902SRodney W. Grimes continue; 2289b50d902SRodney W. Grimes 2299b50d902SRodney W. Grimes /* Try reading/writing. */ 230c64b097bSJaakko Heinonen if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode)) { 231c64b097bSJaakko Heinonen if (rw(*argv, &sb, fflag)) 2329b50d902SRodney W. Grimes rval = 1; 233c64b097bSJaakko Heinonen } else { 234c64b097bSJaakko Heinonen rval = 1; 2354d01a8fbSEric Melville warn("%s", *argv); 2369b50d902SRodney W. Grimes } 237c64b097bSJaakko Heinonen } 2389b50d902SRodney W. Grimes exit(rval); 2399b50d902SRodney W. Grimes } 2409b50d902SRodney W. Grimes 2419b50d902SRodney W. Grimes #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 2429b50d902SRodney W. Grimes 2439b50d902SRodney W. Grimes void 244f4ac32deSDavid Malone stime_arg1(char *arg, struct timeval *tvp) 2459b50d902SRodney W. Grimes { 24639470616SBruce Evans time_t now; 2479b50d902SRodney W. Grimes struct tm *t; 2489b50d902SRodney W. Grimes int yearset; 2499b50d902SRodney W. Grimes char *p; 2509b50d902SRodney W. Grimes /* Start with the current time. */ 25139470616SBruce Evans now = tvp[0].tv_sec; 25239470616SBruce Evans if ((t = localtime(&now)) == NULL) 2539b50d902SRodney W. Grimes err(1, "localtime"); 2549b50d902SRodney W. Grimes /* [[CC]YY]MMDDhhmm[.SS] */ 2559b50d902SRodney W. Grimes if ((p = strchr(arg, '.')) == NULL) 2569b50d902SRodney W. Grimes t->tm_sec = 0; /* Seconds defaults to 0. */ 2579b50d902SRodney W. Grimes else { 2589b50d902SRodney W. Grimes if (strlen(p + 1) != 2) 2599b50d902SRodney W. Grimes goto terr; 2609b50d902SRodney W. Grimes *p++ = '\0'; 2619b50d902SRodney W. Grimes t->tm_sec = ATOI2(p); 2629b50d902SRodney W. Grimes } 2639b50d902SRodney W. Grimes 2649b50d902SRodney W. Grimes yearset = 0; 2659b50d902SRodney W. Grimes switch(strlen(arg)) { 2669b50d902SRodney W. Grimes case 12: /* CCYYMMDDhhmm */ 2679b50d902SRodney W. Grimes t->tm_year = ATOI2(arg); 268d38ca307SJoerg Wunsch t->tm_year *= 100; 2699b50d902SRodney W. Grimes yearset = 1; 27093b0017fSPhilippe Charnier /* FALLTHROUGH */ 2719b50d902SRodney W. Grimes case 10: /* YYMMDDhhmm */ 2729b50d902SRodney W. Grimes if (yearset) { 2739b50d902SRodney W. Grimes yearset = ATOI2(arg); 2749b50d902SRodney W. Grimes t->tm_year += yearset; 2759b50d902SRodney W. Grimes } else { 2769b50d902SRodney W. Grimes yearset = ATOI2(arg); 2779b50d902SRodney W. Grimes if (yearset < 69) 2789b50d902SRodney W. Grimes t->tm_year = yearset + 2000; 2799b50d902SRodney W. Grimes else 2809b50d902SRodney W. Grimes t->tm_year = yearset + 1900; 2819b50d902SRodney W. Grimes } 2829b50d902SRodney W. Grimes t->tm_year -= 1900; /* Convert to UNIX time. */ 2839b50d902SRodney W. Grimes /* FALLTHROUGH */ 2849b50d902SRodney W. Grimes case 8: /* MMDDhhmm */ 2859b50d902SRodney W. Grimes t->tm_mon = ATOI2(arg); 2869b50d902SRodney W. Grimes --t->tm_mon; /* Convert from 01-12 to 00-11 */ 2879b50d902SRodney W. Grimes t->tm_mday = ATOI2(arg); 2889b50d902SRodney W. Grimes t->tm_hour = ATOI2(arg); 2899b50d902SRodney W. Grimes t->tm_min = ATOI2(arg); 2909b50d902SRodney W. Grimes break; 2919b50d902SRodney W. Grimes default: 2929b50d902SRodney W. Grimes goto terr; 2939b50d902SRodney W. Grimes } 2949b50d902SRodney W. Grimes 2959b50d902SRodney W. Grimes t->tm_isdst = -1; /* Figure out DST. */ 2969b50d902SRodney W. Grimes tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 2979b50d902SRodney W. Grimes if (tvp[0].tv_sec == -1) 2989b50d902SRodney W. Grimes terr: errx(1, 2999b50d902SRodney W. Grimes "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]"); 3009b50d902SRodney W. Grimes 3019b50d902SRodney W. Grimes tvp[0].tv_usec = tvp[1].tv_usec = 0; 3029b50d902SRodney W. Grimes } 3039b50d902SRodney W. Grimes 3049b50d902SRodney W. Grimes void 305f4ac32deSDavid Malone stime_arg2(char *arg, int year, struct timeval *tvp) 3069b50d902SRodney W. Grimes { 30739470616SBruce Evans time_t now; 3089b50d902SRodney W. Grimes struct tm *t; 3099b50d902SRodney W. Grimes /* Start with the current time. */ 31039470616SBruce Evans now = tvp[0].tv_sec; 31139470616SBruce Evans if ((t = localtime(&now)) == NULL) 3129b50d902SRodney W. Grimes err(1, "localtime"); 3139b50d902SRodney W. Grimes 3149b50d902SRodney W. Grimes t->tm_mon = ATOI2(arg); /* MMDDhhmm[yy] */ 3159b50d902SRodney W. Grimes --t->tm_mon; /* Convert from 01-12 to 00-11 */ 3169b50d902SRodney W. Grimes t->tm_mday = ATOI2(arg); 3179b50d902SRodney W. Grimes t->tm_hour = ATOI2(arg); 3189b50d902SRodney W. Grimes t->tm_min = ATOI2(arg); 31934c7ff49SDaniel O'Callaghan if (year) { 3209b50d902SRodney W. Grimes t->tm_year = ATOI2(arg); 32125c4d008SDaniel O'Callaghan if (t->tm_year < 39) /* support 2000-2038 not 1902-1969 */ 32234c7ff49SDaniel O'Callaghan t->tm_year += 100; 32334c7ff49SDaniel O'Callaghan } 3249b50d902SRodney W. Grimes 3259b50d902SRodney W. Grimes t->tm_isdst = -1; /* Figure out DST. */ 3269b50d902SRodney W. Grimes tvp[0].tv_sec = tvp[1].tv_sec = mktime(t); 3279b50d902SRodney W. Grimes if (tvp[0].tv_sec == -1) 3289b50d902SRodney W. Grimes errx(1, 3299b50d902SRodney W. Grimes "out of range or illegal time specification: MMDDhhmm[yy]"); 3309b50d902SRodney W. Grimes 3319b50d902SRodney W. Grimes tvp[0].tv_usec = tvp[1].tv_usec = 0; 3329b50d902SRodney W. Grimes } 3339b50d902SRodney W. Grimes 3343481910dSGreg Lehey /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */ 3353481910dSGreg Lehey int 3363481910dSGreg Lehey timeoffset(char *arg) 3373481910dSGreg Lehey { 3383481910dSGreg Lehey int offset; 3393481910dSGreg Lehey int isneg; 3403481910dSGreg Lehey 3413481910dSGreg Lehey offset = 0; 3423481910dSGreg Lehey isneg = *arg == '-'; 3433481910dSGreg Lehey if (isneg) 3443481910dSGreg Lehey arg++; 3453481910dSGreg Lehey switch (strlen(arg)) { 3463481910dSGreg Lehey default: /* invalid */ 3473481910dSGreg Lehey errx(1, "Invalid offset spec, must be [-][[HH]MM]SS"); 3483481910dSGreg Lehey 3493481910dSGreg Lehey case 6: /* HHMMSS */ 3503481910dSGreg Lehey offset = ATOI2(arg); 3513481910dSGreg Lehey /* FALLTHROUGH */ 3523481910dSGreg Lehey case 4: /* MMSS */ 3533481910dSGreg Lehey offset = offset * 60 + ATOI2(arg); 3543481910dSGreg Lehey /* FALLTHROUGH */ 3553481910dSGreg Lehey case 2: /* SS */ 3563481910dSGreg Lehey offset = offset * 60 + ATOI2(arg); 3573481910dSGreg Lehey } 3583481910dSGreg Lehey if (isneg) 3593481910dSGreg Lehey return (-offset); 3603481910dSGreg Lehey else 3613481910dSGreg Lehey return (offset); 3623481910dSGreg Lehey } 3633481910dSGreg Lehey 3649b50d902SRodney W. Grimes void 365f4ac32deSDavid Malone stime_file(char *fname, struct timeval *tvp) 3669b50d902SRodney W. Grimes { 3679b50d902SRodney W. Grimes struct stat sb; 3689b50d902SRodney W. Grimes 3699b50d902SRodney W. Grimes if (stat(fname, &sb)) 3709b50d902SRodney W. Grimes err(1, "%s", fname); 3719b50d902SRodney W. Grimes TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec); 3729b50d902SRodney W. Grimes TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec); 3739b50d902SRodney W. Grimes } 3749b50d902SRodney W. Grimes 3759b50d902SRodney W. Grimes int 376f4ac32deSDavid Malone rw(char *fname, struct stat *sbp, int force) 3779b50d902SRodney W. Grimes { 3789b50d902SRodney W. Grimes int fd, needed_chmod, rval; 3799b50d902SRodney W. Grimes u_char byte; 3809b50d902SRodney W. Grimes 3814d01a8fbSEric Melville /* Try regular files. */ 3824d01a8fbSEric Melville if (!S_ISREG(sbp->st_mode)) { 3839b50d902SRodney W. Grimes warnx("%s: %s", fname, strerror(EFTYPE)); 3849b50d902SRodney W. Grimes return (1); 3859b50d902SRodney W. Grimes } 3869b50d902SRodney W. Grimes 3879b50d902SRodney W. Grimes needed_chmod = rval = 0; 3889b50d902SRodney W. Grimes if ((fd = open(fname, O_RDWR, 0)) == -1) { 3899b50d902SRodney W. Grimes if (!force || chmod(fname, DEFFILEMODE)) 3909b50d902SRodney W. Grimes goto err; 3919b50d902SRodney W. Grimes if ((fd = open(fname, O_RDWR, 0)) == -1) 3929b50d902SRodney W. Grimes goto err; 3939b50d902SRodney W. Grimes needed_chmod = 1; 3949b50d902SRodney W. Grimes } 3959b50d902SRodney W. Grimes 3969b50d902SRodney W. Grimes if (sbp->st_size != 0) { 3979b50d902SRodney W. Grimes if (read(fd, &byte, sizeof(byte)) != sizeof(byte)) 3989b50d902SRodney W. Grimes goto err; 3999b50d902SRodney W. Grimes if (lseek(fd, (off_t)0, SEEK_SET) == -1) 4009b50d902SRodney W. Grimes goto err; 4019b50d902SRodney W. Grimes if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) 4029b50d902SRodney W. Grimes goto err; 4039b50d902SRodney W. Grimes } else { 4049b50d902SRodney W. Grimes if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) { 4059b50d902SRodney W. Grimes err: rval = 1; 4069b50d902SRodney W. Grimes warn("%s", fname); 4079b50d902SRodney W. Grimes } else if (ftruncate(fd, (off_t)0)) { 4089b50d902SRodney W. Grimes rval = 1; 4099b50d902SRodney W. Grimes warn("%s: file modified", fname); 4109b50d902SRodney W. Grimes } 4119b50d902SRodney W. Grimes } 4129b50d902SRodney W. Grimes 4139b50d902SRodney W. Grimes if (close(fd) && rval != 1) { 4149b50d902SRodney W. Grimes rval = 1; 4159b50d902SRodney W. Grimes warn("%s", fname); 4169b50d902SRodney W. Grimes } 4179b50d902SRodney W. Grimes if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) { 4189b50d902SRodney W. Grimes rval = 1; 4199b50d902SRodney W. Grimes warn("%s: permissions modified", fname); 4209b50d902SRodney W. Grimes } 4219b50d902SRodney W. Grimes return (rval); 4229b50d902SRodney W. Grimes } 4239b50d902SRodney W. Grimes 424eaa86f9dSBruce Evans void 4253481910dSGreg Lehey usage(char *myname) 4269b50d902SRodney W. Grimes { 4273481910dSGreg Lehey fprintf(stderr, "usage:\n" "%s [-A [-][[hh]mm]SS] [-acfhm] [-r file] " 4283481910dSGreg Lehey "[-t [[CC]YY]MMDDhhmm[.SS]] file ...\n", myname); 4299b50d902SRodney W. Grimes exit(1); 4309b50d902SRodney W. Grimes } 431