Home | History | Annotate | Line # | Download | only in touch
touch.c revision 1.36
      1  1.36       kre /*	$NetBSD: touch.c,v 1.36 2024/02/08 02:53:40 kre 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.24       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.15     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.27     lukem __COPYRIGHT("@(#) Copyright (c) 1993\
     35  1.27     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39   1.9       jtc #if 0
     40  1.11       jtc static char sccsid[] = "@(#)touch.c	8.2 (Berkeley) 4/28/95";
     41   1.9       jtc #endif
     42  1.36       kre __RCSID("$NetBSD: touch.c,v 1.36 2024/02/08 02:53:40 kre Exp $");
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/types.h>
     46   1.1       cgd #include <sys/stat.h>
     47   1.2     glass #include <sys/time.h>
     48   1.2     glass 
     49   1.9       jtc #include <err.h>
     50   1.2     glass #include <errno.h>
     51   1.2     glass #include <fcntl.h>
     52   1.1       cgd #include <stdio.h>
     53   1.2     glass #include <stdlib.h>
     54   1.2     glass #include <string.h>
     55   1.8       jtc #include <locale.h>
     56   1.2     glass #include <time.h>
     57  1.19   mycroft #include <tzfile.h>
     58   1.2     glass #include <unistd.h>
     59  1.31  christos #include <util.h>
     60  1.32  christos #include <getopt.h>
     61   1.2     glass 
     62  1.36       kre static void	stime_arg0(const char *, struct timespec *);
     63  1.33     enami static void	stime_arg1(char *, struct timespec *);
     64  1.36       kre static void	stime_arg2(const char *, int, struct timespec *);
     65  1.36       kre static void	stime_file(const char *, struct timespec *);
     66  1.30     joerg __dead static void	usage(void);
     67   1.1       cgd 
     68  1.32  christos struct option touch_longopts[] = {
     69  1.32  christos 	{ "date",		required_argument,	0,
     70  1.32  christos 						'd' },
     71  1.32  christos 	{ "reference",		required_argument,	0,
     72  1.32  christos 						'r' },
     73  1.32  christos 	{ NULL,			0,			0,
     74  1.32  christos 						0 },
     75  1.32  christos };
     76  1.32  christos 
     77  1.35       kre #define	YEAR_BOUNDARY		69
     78  1.35       kre #define	LOW_YEAR_CENTURY	2000	/* for 2 digit years < YEAR_BOUNDARY */
     79  1.35       kre #define	HIGH_YEAR_CENTURY	1900	/* for 2 digit years >=  "  */
     80  1.35       kre 
     81  1.35       kre #define	NO_TIME		((time_t)-1)	/* time_t might be unsigned */
     82  1.35       kre 
     83   1.2     glass int
     84  1.30     joerg main(int argc, char *argv[])
     85   1.1       cgd {
     86   1.2     glass 	struct stat sb;
     87  1.33     enami 	struct timespec ts[2];
     88  1.29     pooka 	int aflag, cflag, hflag, mflag, ch, fd, len, rval, timeset;
     89   1.2     glass 	char *p;
     90  1.33     enami 	int (*change_file_times)(const char *, const struct timespec *);
     91  1.30     joerg 	int (*get_file_status)(const char *, struct stat *);
     92   1.2     glass 
     93   1.8       jtc 	setlocale(LC_ALL, "");
     94   1.8       jtc 
     95  1.29     pooka 	aflag = cflag = hflag = mflag = timeset = 0;
     96  1.33     enami 	if (clock_gettime(CLOCK_REALTIME, &ts[0]))
     97  1.33     enami 		err(1, "clock_gettime");
     98   1.2     glass 
     99  1.32  christos 	while ((ch = getopt_long(argc, argv, "acd:fhmr:t:", touch_longopts,
    100  1.32  christos 	    NULL)) != -1)
    101  1.34       kre 		switch (ch) {
    102   1.2     glass 		case 'a':
    103   1.2     glass 			aflag = 1;
    104   1.2     glass 			break;
    105   1.1       cgd 		case 'c':
    106   1.2     glass 			cflag = 1;
    107   1.1       cgd 			break;
    108  1.31  christos 		case 'd':
    109  1.31  christos 			timeset = 1;
    110  1.33     enami 			stime_arg0(optarg, ts);
    111  1.31  christos 			break;
    112   1.1       cgd 		case 'f':
    113   1.2     glass 			break;
    114  1.12     enami 		case 'h':
    115  1.12     enami 			hflag = 1;
    116  1.12     enami 			break;
    117   1.2     glass 		case 'm':
    118   1.2     glass 			mflag = 1;
    119   1.2     glass 			break;
    120   1.2     glass 		case 'r':
    121   1.2     glass 			timeset = 1;
    122  1.33     enami 			stime_file(optarg, ts);
    123   1.1       cgd 			break;
    124   1.2     glass 		case 't':
    125   1.2     glass 			timeset = 1;
    126  1.33     enami 			stime_arg1(optarg, ts);
    127   1.2     glass 			break;
    128   1.1       cgd 		case '?':
    129   1.1       cgd 		default:
    130   1.1       cgd 			usage();
    131   1.1       cgd 		}
    132   1.2     glass 	argc -= optind;
    133   1.2     glass 	argv += optind;
    134   1.2     glass 
    135   1.2     glass 	/* Default is both -a and -m. */
    136   1.2     glass 	if (aflag == 0 && mflag == 0)
    137   1.2     glass 		aflag = mflag = 1;
    138   1.2     glass 
    139  1.17      fvdl 	if (hflag) {
    140  1.12     enami 		cflag = 1;		/* Don't create new file */
    141  1.33     enami 		change_file_times = lutimens;
    142  1.17      fvdl 		get_file_status = lstat;
    143  1.17      fvdl 	} else {
    144  1.33     enami 		change_file_times = utimens;
    145  1.17      fvdl 		get_file_status = stat;
    146  1.17      fvdl 	}
    147  1.12     enami 
    148   1.2     glass 	/*
    149   1.2     glass 	 * If no -r or -t flag, at least two operands, the first of which
    150   1.2     glass 	 * is an 8 or 10 digit number, use the obsolete time specification.
    151   1.2     glass 	 */
    152   1.2     glass 	if (!timeset && argc > 1) {
    153   1.2     glass 		(void)strtol(argv[0], &p, 10);
    154   1.2     glass 		len = p - argv[0];
    155   1.2     glass 		if (*p == '\0' && (len == 8 || len == 10)) {
    156   1.2     glass 			timeset = 1;
    157  1.33     enami 			stime_arg2(*argv++, len == 10, ts);
    158   1.2     glass 		}
    159   1.2     glass 	}
    160   1.2     glass 
    161   1.2     glass 	/* Otherwise use the current time of day. */
    162   1.2     glass 	if (!timeset)
    163  1.33     enami 		ts[1] = ts[0];
    164   1.2     glass 
    165   1.2     glass 	if (*argv == NULL)
    166   1.1       cgd 		usage();
    167   1.2     glass 
    168  1.31  christos 	for (rval = EXIT_SUCCESS; *argv; ++argv) {
    169   1.2     glass 		/* See if the file exists. */
    170  1.22      ross 		if ((*get_file_status)(*argv, &sb)) {
    171   1.2     glass 			if (!cflag) {
    172   1.2     glass 				/* Create the file. */
    173   1.2     glass 				fd = open(*argv,
    174   1.2     glass 				    O_WRONLY | O_CREAT, DEFFILEMODE);
    175   1.2     glass 				if (fd == -1 || fstat(fd, &sb) || close(fd)) {
    176  1.31  christos 					rval = EXIT_FAILURE;
    177   1.2     glass 					warn("%s", *argv);
    178   1.2     glass 					continue;
    179   1.2     glass 				}
    180   1.2     glass 
    181   1.2     glass 				/* If using the current time, we're done. */
    182   1.2     glass 				if (!timeset)
    183   1.2     glass 					continue;
    184   1.2     glass 			} else
    185   1.2     glass 				continue;
    186  1.22      ross 		}
    187   1.2     glass 		if (!aflag)
    188  1.33     enami 			ts[0] = sb.st_atimespec;
    189   1.2     glass 		if (!mflag)
    190  1.33     enami 			ts[1] = sb.st_mtimespec;
    191   1.2     glass 
    192   1.2     glass 		/* Try utimes(2). */
    193  1.33     enami 		if (!(*change_file_times)(*argv, ts))
    194   1.2     glass 			continue;
    195   1.2     glass 
    196   1.2     glass 		/* If the user specified a time, nothing else we can do. */
    197   1.2     glass 		if (timeset) {
    198  1.31  christos 			rval = EXIT_FAILURE;
    199   1.2     glass 			warn("%s", *argv);
    200   1.2     glass 		}
    201   1.2     glass 
    202   1.2     glass 		/*
    203   1.2     glass 		 * System V and POSIX 1003.1 require that a NULL argument
    204   1.2     glass 		 * set the access/modification times to the current time.
    205   1.2     glass 		 * The permission checks are different, too, in that the
    206   1.2     glass 		 * ability to write the file is sufficient.  Take a shot.
    207   1.2     glass 		 */
    208  1.17      fvdl 		 if (!(*change_file_times)(*argv, NULL))
    209   1.2     glass 			continue;
    210   1.2     glass 
    211  1.31  christos 		rval = EXIT_FAILURE;
    212  1.28      yamt 		warn("%s", *argv);
    213   1.2     glass 	}
    214   1.2     glass 	exit(rval);
    215   1.1       cgd }
    216   1.1       cgd 
    217  1.20   mycroft #define	ATOI2(s)	((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    218   1.2     glass 
    219  1.30     joerg static void
    220  1.36       kre stime_arg0(const char *arg, struct timespec *tsp)
    221  1.31  christos {
    222  1.33     enami 	tsp[1].tv_sec = tsp[0].tv_sec = parsedate(arg, NULL, NULL);
    223  1.35       kre 	if (tsp[0].tv_sec == NO_TIME)
    224  1.31  christos 		errx(EXIT_FAILURE, "Could not parse `%s'", arg);
    225  1.33     enami 	tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
    226  1.31  christos }
    227  1.31  christos 
    228  1.31  christos static void
    229  1.33     enami stime_arg1(char *arg, struct timespec *tsp)
    230   1.1       cgd {
    231   1.2     glass 	struct tm *t;
    232  1.10       cgd 	time_t tmptime;
    233   1.2     glass 	int yearset;
    234   1.2     glass 	char *p;
    235  1.35       kre 	char *initarg = arg;
    236  1.35       kre 
    237   1.2     glass 					/* Start with the current time. */
    238  1.33     enami 	tmptime = tsp[0].tv_sec;
    239  1.10       cgd 	if ((t = localtime(&tmptime)) == NULL)
    240  1.31  christos 		err(EXIT_FAILURE, "localtime");
    241  1.35       kre 					/* [[CC]YY]MMDDhhmm[.ss] */
    242   1.2     glass 	if ((p = strchr(arg, '.')) == NULL)
    243   1.2     glass 		t->tm_sec = 0;		/* Seconds defaults to 0. */
    244   1.2     glass 	else {
    245   1.2     glass 		if (strlen(p + 1) != 2)
    246   1.2     glass 			goto terr;
    247   1.2     glass 		*p++ = '\0';
    248   1.2     glass 		t->tm_sec = ATOI2(p);
    249   1.2     glass 	}
    250   1.2     glass 
    251   1.2     glass 	yearset = 0;
    252  1.20   mycroft 	switch (strlen(arg)) {
    253   1.2     glass 	case 12:			/* CCYYMMDDhhmm */
    254  1.20   mycroft 		t->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE;
    255   1.2     glass 		yearset = 1;
    256  1.25   tsutsui 		/* FALLTHROUGH */
    257   1.2     glass 	case 10:			/* YYMMDDhhmm */
    258   1.2     glass 		if (yearset) {
    259  1.20   mycroft 			t->tm_year += ATOI2(arg);
    260   1.2     glass 		} else {
    261   1.2     glass 			yearset = ATOI2(arg);
    262  1.35       kre 			if (yearset < YEAR_BOUNDARY)
    263  1.35       kre 				t->tm_year = yearset +
    264  1.35       kre 				    LOW_YEAR_CENTURY - TM_YEAR_BASE;
    265   1.2     glass 			else
    266  1.35       kre 				t->tm_year = yearset +
    267  1.35       kre 				    HIGH_YEAR_CENTURY - TM_YEAR_BASE;
    268   1.1       cgd 		}
    269   1.2     glass 		/* FALLTHROUGH */
    270   1.2     glass 	case 8:				/* MMDDhhmm */
    271   1.2     glass 		t->tm_mon = ATOI2(arg);
    272   1.2     glass 		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
    273  1.20   mycroft 		/* FALLTHROUGH */
    274  1.20   mycroft 	case 6:
    275   1.2     glass 		t->tm_mday = ATOI2(arg);
    276  1.20   mycroft 		/* FALLTHROUGH */
    277  1.20   mycroft 	case 4:
    278   1.2     glass 		t->tm_hour = ATOI2(arg);
    279  1.20   mycroft 		/* FALLTHROUGH */
    280  1.20   mycroft 	case 2:
    281   1.2     glass 		t->tm_min = ATOI2(arg);
    282   1.2     glass 		break;
    283   1.2     glass 	default:
    284   1.2     glass 		goto terr;
    285   1.1       cgd 	}
    286   1.2     glass 
    287   1.2     glass 	t->tm_isdst = -1;		/* Figure out DST. */
    288  1.33     enami 	tsp[0].tv_sec = tsp[1].tv_sec = mktime(t);
    289  1.35       kre 	if (tsp[0].tv_sec == NO_TIME)
    290  1.35       kre  terr:		errx(EXIT_FAILURE, "out of range or bad time specification:\n"
    291  1.35       kre 		    "\t'%s' should be [[CC]YY]MMDDhhmm[.ss]", initarg);
    292   1.2     glass 
    293  1.33     enami 	tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
    294   1.2     glass }
    295   1.2     glass 
    296  1.30     joerg static void
    297  1.36       kre stime_arg2(const char *arg, int year, struct timespec *tsp)
    298   1.2     glass {
    299   1.2     glass 	struct tm *t;
    300  1.10       cgd 	time_t tmptime;
    301   1.2     glass 					/* Start with the current time. */
    302  1.33     enami 	tmptime = tsp[0].tv_sec;
    303  1.10       cgd 	if ((t = localtime(&tmptime)) == NULL)
    304  1.31  christos 		err(EXIT_FAILURE, "localtime");
    305   1.2     glass 
    306   1.2     glass 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
    307   1.2     glass 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
    308   1.2     glass 	t->tm_mday = ATOI2(arg);
    309   1.2     glass 	t->tm_hour = ATOI2(arg);
    310   1.2     glass 	t->tm_min = ATOI2(arg);
    311  1.21   mycroft 	if (year) {
    312  1.21   mycroft 		year = ATOI2(arg);
    313  1.35       kre 		if (year < YEAR_BOUNDARY)
    314  1.35       kre 			t->tm_year = year + LOW_YEAR_CENTURY - TM_YEAR_BASE;
    315  1.21   mycroft 		else
    316  1.35       kre 			t->tm_year = year + HIGH_YEAR_CENTURY - TM_YEAR_BASE;
    317  1.21   mycroft 	}
    318  1.23   mycroft 	t->tm_sec = 0;
    319   1.2     glass 
    320   1.2     glass 	t->tm_isdst = -1;		/* Figure out DST. */
    321  1.33     enami 	tsp[0].tv_sec = tsp[1].tv_sec = mktime(t);
    322  1.35       kre 	if (tsp[0].tv_sec == NO_TIME)
    323  1.31  christos 		errx(EXIT_FAILURE,
    324  1.35       kre 		    "out of range or bad time specification: MMDDhhmm[YY]");
    325   1.2     glass 
    326  1.33     enami 	tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
    327   1.2     glass }
    328   1.2     glass 
    329  1.30     joerg static void
    330  1.36       kre stime_file(const char *fname, struct timespec *tsp)
    331   1.2     glass {
    332   1.2     glass 	struct stat sb;
    333   1.2     glass 
    334   1.2     glass 	if (stat(fname, &sb))
    335   1.2     glass 		err(1, "%s", fname);
    336  1.33     enami 	tsp[0] = sb.st_atimespec;
    337  1.33     enami 	tsp[1] = sb.st_mtimespec;
    338   1.1       cgd }
    339   1.1       cgd 
    340  1.30     joerg static void
    341  1.30     joerg usage(void)
    342   1.1       cgd {
    343   1.2     glass 	(void)fprintf(stderr,
    344  1.34       kre 	    "Usage: %s [-acfhm] [-d|--date datetime] [-r|--reference file]"
    345  1.34       kre 	    " [-t time] file ...\n", getprogname());
    346  1.31  christos 	exit(EXIT_FAILURE);
    347   1.1       cgd }
    348