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