Home | History | Annotate | Line # | Download | only in touch
touch.c revision 1.16
      1 /*	$NetBSD: touch.c,v 1.16 1997/10/21 18:25:26 thorpej 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.16 1997/10/21 18:25:26 thorpej 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 <unistd.h>
     62 
     63 int	main __P((int, char **));
     64 int	rw __P((char *, struct stat *, int));
     65 void	stime_arg1 __P((char *, struct timeval *));
     66 void	stime_arg2 __P((char *, int, struct timeval *));
     67 void	stime_file __P((char *, struct timeval *));
     68 void	usage __P((void));
     69 
     70 int	hflag;		/* needed by the following */
     71 
     72 int	change_file_times __P((const char *, const struct timeval *));
     73 int	get_file_status __P((const char *, struct stat *));
     74 
     75 int
     76 main(argc, argv)
     77 	int argc;
     78 	char *argv[];
     79 {
     80 	struct stat sb;
     81 	struct timeval tv[2];
     82 	int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
     83 	char *p;
     84 
     85 	setlocale(LC_ALL, "");
     86 
     87 	aflag = cflag = fflag = hflag = mflag = timeset = 0;
     88 	if (gettimeofday(&tv[0], NULL))
     89 		err(1, "gettimeofday");
     90 
     91 	while ((ch = getopt(argc, argv, "acfhmr:t:")) != -1)
     92 		switch(ch) {
     93 		case 'a':
     94 			aflag = 1;
     95 			break;
     96 		case 'c':
     97 			cflag = 1;
     98 			break;
     99 		case 'f':
    100 			fflag = 1;
    101 			break;
    102 		case 'h':
    103 			hflag = 1;
    104 			break;
    105 		case 'm':
    106 			mflag = 1;
    107 			break;
    108 		case 'r':
    109 			timeset = 1;
    110 			stime_file(optarg, tv);
    111 			break;
    112 		case 't':
    113 			timeset = 1;
    114 			stime_arg1(optarg, tv);
    115 			break;
    116 		case '?':
    117 		default:
    118 			usage();
    119 		}
    120 	argc -= optind;
    121 	argv += optind;
    122 
    123 	/* Default is both -a and -m. */
    124 	if (aflag == 0 && mflag == 0)
    125 		aflag = mflag = 1;
    126 
    127 	if (hflag)
    128 		cflag = 1;		/* Don't create new file */
    129 
    130 	/*
    131 	 * If no -r or -t flag, at least two operands, the first of which
    132 	 * is an 8 or 10 digit number, use the obsolete time specification.
    133 	 */
    134 	if (!timeset && argc > 1) {
    135 		(void)strtol(argv[0], &p, 10);
    136 		len = p - argv[0];
    137 		if (*p == '\0' && (len == 8 || len == 10)) {
    138 			timeset = 1;
    139 			stime_arg2(*argv++, len == 10, tv);
    140 		}
    141 	}
    142 
    143 	/* Otherwise use the current time of day. */
    144 	if (!timeset)
    145 		tv[1] = tv[0];
    146 
    147 	if (*argv == NULL)
    148 		usage();
    149 
    150 	for (rval = 0; *argv; ++argv) {
    151 		/* See if the file exists. */
    152 		if (get_file_status(*argv, &sb))
    153 			if (!cflag) {
    154 				/* Create the file. */
    155 				fd = open(*argv,
    156 				    O_WRONLY | O_CREAT, DEFFILEMODE);
    157 				if (fd == -1 || fstat(fd, &sb) || close(fd)) {
    158 					rval = 1;
    159 					warn("%s", *argv);
    160 					continue;
    161 				}
    162 
    163 				/* If using the current time, we're done. */
    164 				if (!timeset)
    165 					continue;
    166 			} else
    167 				continue;
    168 
    169 		if (!aflag)
    170 			TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
    171 		if (!mflag)
    172 			TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
    173 
    174 		/* Try utimes(2). */
    175 		if (!change_file_times(*argv, tv))
    176 			continue;
    177 
    178 		/* If the user specified a time, nothing else we can do. */
    179 		if (timeset) {
    180 			rval = 1;
    181 			warn("%s", *argv);
    182 		}
    183 
    184 		/*
    185 		 * System V and POSIX 1003.1 require that a NULL argument
    186 		 * set the access/modification times to the current time.
    187 		 * The permission checks are different, too, in that the
    188 		 * ability to write the file is sufficient.  Take a shot.
    189 		 */
    190 		 if (!change_file_times(*argv, NULL))
    191 			continue;
    192 
    193 		/* Try reading/writing. */
    194 		if (!S_ISLNK(sb.st_mode) && rw(*argv, &sb, fflag))
    195 			rval = 1;
    196 	}
    197 	exit(rval);
    198 }
    199 
    200 #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
    201 
    202 void
    203 stime_arg1(arg, tvp)
    204 	char *arg;
    205 	struct timeval *tvp;
    206 {
    207 	struct tm *t;
    208 	time_t tmptime;
    209 	int yearset;
    210 	char *p;
    211 					/* Start with the current time. */
    212 	tmptime = tvp[0].tv_sec;
    213 	if ((t = localtime(&tmptime)) == NULL)
    214 		err(1, "localtime");
    215 					/* [[CC]YY]MMDDhhmm[.SS] */
    216 	if ((p = strchr(arg, '.')) == NULL)
    217 		t->tm_sec = 0;		/* Seconds defaults to 0. */
    218 	else {
    219 		if (strlen(p + 1) != 2)
    220 			goto terr;
    221 		*p++ = '\0';
    222 		t->tm_sec = ATOI2(p);
    223 	}
    224 
    225 	yearset = 0;
    226 	switch(strlen(arg)) {
    227 	case 12:			/* CCYYMMDDhhmm */
    228 		t->tm_year = ATOI2(arg);
    229 		t->tm_year *= 100;
    230 		yearset = 1;
    231 		/* FALLTHOUGH */
    232 	case 10:			/* YYMMDDhhmm */
    233 		if (yearset) {
    234 			yearset = ATOI2(arg);
    235 			t->tm_year += yearset;
    236 		} else {
    237 			yearset = ATOI2(arg);
    238 			if (yearset < 69)
    239 				t->tm_year = yearset + 2000;
    240 			else
    241 				t->tm_year = yearset + 1900;
    242 		}
    243 		t->tm_year -= 1900;	/* Convert to UNIX time. */
    244 		/* FALLTHROUGH */
    245 	case 8:				/* MMDDhhmm */
    246 		t->tm_mon = ATOI2(arg);
    247 		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
    248 		t->tm_mday = ATOI2(arg);
    249 		t->tm_hour = ATOI2(arg);
    250 		t->tm_min = ATOI2(arg);
    251 		break;
    252 	default:
    253 		goto terr;
    254 	}
    255 
    256 	t->tm_isdst = -1;		/* Figure out DST. */
    257 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
    258 	if (tvp[0].tv_sec == -1)
    259 terr:		errx(1,
    260 	"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
    261 
    262 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
    263 }
    264 
    265 void
    266 stime_arg2(arg, year, tvp)
    267 	char *arg;
    268 	int year;
    269 	struct timeval *tvp;
    270 {
    271 	struct tm *t;
    272 	time_t tmptime;
    273 					/* Start with the current time. */
    274 	tmptime = tvp[0].tv_sec;
    275 	if ((t = localtime(&tmptime)) == NULL)
    276 		err(1, "localtime");
    277 
    278 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
    279 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
    280 	t->tm_mday = ATOI2(arg);
    281 	t->tm_hour = ATOI2(arg);
    282 	t->tm_min = ATOI2(arg);
    283 	if (year)
    284 		t->tm_year = ATOI2(arg);
    285 
    286 	t->tm_isdst = -1;		/* Figure out DST. */
    287 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
    288 	if (tvp[0].tv_sec == -1)
    289 		errx(1,
    290 	"out of range or illegal time specification: MMDDhhmm[yy]");
    291 
    292 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
    293 }
    294 
    295 void
    296 stime_file(fname, tvp)
    297 	char *fname;
    298 	struct timeval *tvp;
    299 {
    300 	struct stat sb;
    301 
    302 	if (stat(fname, &sb))
    303 		err(1, "%s", fname);
    304 	TIMESPEC_TO_TIMEVAL(&tvp[0], &sb.st_atimespec);
    305 	TIMESPEC_TO_TIMEVAL(&tvp[1], &sb.st_mtimespec);
    306 }
    307 
    308 int
    309 rw(fname, sbp, force)
    310 	char *fname;
    311 	struct stat *sbp;
    312 	int force;
    313 {
    314 	int fd, needed_chmod, rval;
    315 	u_char byte;
    316 
    317 	/* Try regular files and directories. */
    318 	if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
    319 		warnx("%s: %s", fname, strerror(EFTYPE));
    320 		return (1);
    321 	}
    322 
    323 	needed_chmod = rval = 0;
    324 	if ((fd = open(fname, O_RDWR, 0)) == -1) {
    325 		if (!force || chmod(fname, DEFFILEMODE))
    326 			goto err;
    327 		if ((fd = open(fname, O_RDWR, 0)) == -1)
    328 			goto err;
    329 		needed_chmod = 1;
    330 	}
    331 
    332 	if (sbp->st_size != 0) {
    333 		if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
    334 			goto err;
    335 		if (lseek(fd, (off_t)0, SEEK_SET) == -1)
    336 			goto err;
    337 		if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
    338 			goto err;
    339 	} else {
    340 		if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
    341 err:			rval = 1;
    342 			warn("%s", fname);
    343 		} else if (ftruncate(fd, (off_t)0)) {
    344 			rval = 1;
    345 			warn("%s: file modified", fname);
    346 		}
    347 	}
    348 
    349 	if (close(fd) && rval != 1) {
    350 		rval = 1;
    351 		warn("%s", fname);
    352 	}
    353 	if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
    354 		rval = 1;
    355 		warn("%s: permissions modified", fname);
    356 	}
    357 	return (rval);
    358 }
    359 
    360 int
    361 change_file_times(path, tvp)
    362 	const char *path;
    363 	const struct timeval *tvp;
    364 {
    365 
    366 	if (hflag)
    367 		return (lutimes(path, tvp));
    368 
    369 	return (utimes(path, tvp));
    370 }
    371 
    372 int
    373 get_file_status(path, sb)
    374 	const char *path;
    375 	struct stat *sb;
    376 {
    377 
    378 	if (hflag)
    379 		return (lstat(path, sb));
    380 
    381 	return (stat(path, sb));
    382 }
    383 
    384 __dead void
    385 usage()
    386 {
    387 	(void)fprintf(stderr,
    388 	    "usage: touch [-acfhm] [-r file] [-t time] file ...\n");
    389 	exit(1);
    390 }
    391