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