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