Home | History | Annotate | Line # | Download | only in touch
touch.c revision 1.31
      1  1.31  christos /*	$NetBSD: touch.c,v 1.31 2012/07/25 01:23:46 christos 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.31  christos __RCSID("$NetBSD: touch.c,v 1.31 2012/07/25 01:23:46 christos 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.2     glass 
     61  1.31  christos static void	stime_arg0(char *, struct timeval *);
     62  1.30     joerg static void	stime_arg1(char *, struct timeval *);
     63  1.30     joerg static void	stime_arg2(char *, int, struct timeval *);
     64  1.30     joerg static void	stime_file(char *, struct timeval *);
     65  1.30     joerg __dead static void	usage(void);
     66   1.1       cgd 
     67   1.2     glass int
     68  1.30     joerg main(int argc, char *argv[])
     69   1.1       cgd {
     70   1.2     glass 	struct stat sb;
     71   1.2     glass 	struct timeval tv[2];
     72  1.29     pooka 	int aflag, cflag, hflag, mflag, ch, fd, len, rval, timeset;
     73   1.2     glass 	char *p;
     74  1.30     joerg 	int (*change_file_times)(const char *, const struct timeval *);
     75  1.30     joerg 	int (*get_file_status)(const char *, struct stat *);
     76   1.2     glass 
     77   1.8       jtc 	setlocale(LC_ALL, "");
     78   1.8       jtc 
     79  1.29     pooka 	aflag = cflag = hflag = mflag = timeset = 0;
     80   1.2     glass 	if (gettimeofday(&tv[0], NULL))
     81   1.2     glass 		err(1, "gettimeofday");
     82   1.2     glass 
     83  1.31  christos 	while ((ch = getopt(argc, argv, "acd:fhmr:t:")) != -1)
     84   1.2     glass 		switch(ch) {
     85   1.2     glass 		case 'a':
     86   1.2     glass 			aflag = 1;
     87   1.2     glass 			break;
     88   1.1       cgd 		case 'c':
     89   1.2     glass 			cflag = 1;
     90   1.1       cgd 			break;
     91  1.31  christos 		case 'd':
     92  1.31  christos 			timeset = 1;
     93  1.31  christos 			stime_arg0(optarg, tv);
     94  1.31  christos 			break;
     95   1.1       cgd 		case 'f':
     96   1.2     glass 			break;
     97  1.12     enami 		case 'h':
     98  1.12     enami 			hflag = 1;
     99  1.12     enami 			break;
    100   1.2     glass 		case 'm':
    101   1.2     glass 			mflag = 1;
    102   1.2     glass 			break;
    103   1.2     glass 		case 'r':
    104   1.2     glass 			timeset = 1;
    105   1.2     glass 			stime_file(optarg, tv);
    106   1.1       cgd 			break;
    107   1.2     glass 		case 't':
    108   1.2     glass 			timeset = 1;
    109   1.2     glass 			stime_arg1(optarg, tv);
    110   1.2     glass 			break;
    111   1.1       cgd 		case '?':
    112   1.1       cgd 		default:
    113   1.1       cgd 			usage();
    114   1.1       cgd 		}
    115   1.2     glass 	argc -= optind;
    116   1.2     glass 	argv += optind;
    117   1.2     glass 
    118   1.2     glass 	/* Default is both -a and -m. */
    119   1.2     glass 	if (aflag == 0 && mflag == 0)
    120   1.2     glass 		aflag = mflag = 1;
    121   1.2     glass 
    122  1.17      fvdl 	if (hflag) {
    123  1.12     enami 		cflag = 1;		/* Don't create new file */
    124  1.17      fvdl 		change_file_times = lutimes;
    125  1.17      fvdl 		get_file_status = lstat;
    126  1.17      fvdl 	} else {
    127  1.17      fvdl 		change_file_times = utimes;
    128  1.17      fvdl 		get_file_status = stat;
    129  1.17      fvdl 	}
    130  1.12     enami 
    131   1.2     glass 	/*
    132   1.2     glass 	 * If no -r or -t flag, at least two operands, the first of which
    133   1.2     glass 	 * is an 8 or 10 digit number, use the obsolete time specification.
    134   1.2     glass 	 */
    135   1.2     glass 	if (!timeset && argc > 1) {
    136   1.2     glass 		(void)strtol(argv[0], &p, 10);
    137   1.2     glass 		len = p - argv[0];
    138   1.2     glass 		if (*p == '\0' && (len == 8 || len == 10)) {
    139   1.2     glass 			timeset = 1;
    140   1.9       jtc 			stime_arg2(*argv++, len == 10, tv);
    141   1.2     glass 		}
    142   1.2     glass 	}
    143   1.2     glass 
    144   1.2     glass 	/* Otherwise use the current time of day. */
    145   1.2     glass 	if (!timeset)
    146   1.2     glass 		tv[1] = tv[0];
    147   1.2     glass 
    148   1.2     glass 	if (*argv == NULL)
    149   1.1       cgd 		usage();
    150   1.2     glass 
    151  1.31  christos 	for (rval = EXIT_SUCCESS; *argv; ++argv) {
    152   1.2     glass 		/* See if the file exists. */
    153  1.22      ross 		if ((*get_file_status)(*argv, &sb)) {
    154   1.2     glass 			if (!cflag) {
    155   1.2     glass 				/* Create the file. */
    156   1.2     glass 				fd = open(*argv,
    157   1.2     glass 				    O_WRONLY | O_CREAT, DEFFILEMODE);
    158   1.2     glass 				if (fd == -1 || fstat(fd, &sb) || close(fd)) {
    159  1.31  christos 					rval = EXIT_FAILURE;
    160   1.2     glass 					warn("%s", *argv);
    161   1.2     glass 					continue;
    162   1.2     glass 				}
    163   1.2     glass 
    164   1.2     glass 				/* If using the current time, we're done. */
    165   1.2     glass 				if (!timeset)
    166   1.2     glass 					continue;
    167   1.2     glass 			} else
    168   1.2     glass 				continue;
    169  1.22      ross 		}
    170   1.2     glass 		if (!aflag)
    171   1.6   mycroft 			TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
    172   1.2     glass 		if (!mflag)
    173   1.6   mycroft 			TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
    174   1.2     glass 
    175   1.2     glass 		/* Try utimes(2). */
    176  1.17      fvdl 		if (!(*change_file_times)(*argv, tv))
    177   1.2     glass 			continue;
    178   1.2     glass 
    179   1.2     glass 		/* If the user specified a time, nothing else we can do. */
    180   1.2     glass 		if (timeset) {
    181  1.31  christos 			rval = EXIT_FAILURE;
    182   1.2     glass 			warn("%s", *argv);
    183   1.2     glass 		}
    184   1.2     glass 
    185   1.2     glass 		/*
    186   1.2     glass 		 * System V and POSIX 1003.1 require that a NULL argument
    187   1.2     glass 		 * set the access/modification times to the current time.
    188   1.2     glass 		 * The permission checks are different, too, in that the
    189   1.2     glass 		 * ability to write the file is sufficient.  Take a shot.
    190   1.2     glass 		 */
    191  1.17      fvdl 		 if (!(*change_file_times)(*argv, NULL))
    192   1.2     glass 			continue;
    193   1.2     glass 
    194  1.31  christos 		rval = EXIT_FAILURE;
    195  1.28      yamt 		warn("%s", *argv);
    196   1.2     glass 	}
    197   1.2     glass 	exit(rval);
    198   1.1       cgd }
    199   1.1       cgd 
    200  1.20   mycroft #define	ATOI2(s)	((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    201   1.2     glass 
    202  1.30     joerg static void
    203  1.31  christos stime_arg0(char *arg, struct timeval *tvp)
    204  1.31  christos {
    205  1.31  christos 	tvp[1].tv_sec = tvp[0].tv_sec = parsedate(arg, NULL, NULL);
    206  1.31  christos 	if (tvp[0].tv_sec == -1)
    207  1.31  christos 		errx(EXIT_FAILURE, "Could not parse `%s'", arg);
    208  1.31  christos 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
    209  1.31  christos }
    210  1.31  christos 
    211  1.31  christos static void
    212  1.30     joerg stime_arg1(char *arg, struct timeval *tvp)
    213   1.1       cgd {
    214   1.2     glass 	struct tm *t;
    215  1.10       cgd 	time_t tmptime;
    216   1.2     glass 	int yearset;
    217   1.2     glass 	char *p;
    218   1.2     glass 					/* Start with the current time. */
    219  1.10       cgd 	tmptime = tvp[0].tv_sec;
    220  1.10       cgd 	if ((t = localtime(&tmptime)) == NULL)
    221  1.31  christos 		err(EXIT_FAILURE, "localtime");
    222   1.2     glass 					/* [[CC]YY]MMDDhhmm[.SS] */
    223   1.2     glass 	if ((p = strchr(arg, '.')) == NULL)
    224   1.2     glass 		t->tm_sec = 0;		/* Seconds defaults to 0. */
    225   1.2     glass 	else {
    226   1.2     glass 		if (strlen(p + 1) != 2)
    227   1.2     glass 			goto terr;
    228   1.2     glass 		*p++ = '\0';
    229   1.2     glass 		t->tm_sec = ATOI2(p);
    230   1.2     glass 	}
    231   1.2     glass 
    232   1.2     glass 	yearset = 0;
    233  1.20   mycroft 	switch (strlen(arg)) {
    234   1.2     glass 	case 12:			/* CCYYMMDDhhmm */
    235  1.20   mycroft 		t->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE;
    236   1.2     glass 		yearset = 1;
    237  1.25   tsutsui 		/* FALLTHROUGH */
    238   1.2     glass 	case 10:			/* YYMMDDhhmm */
    239   1.2     glass 		if (yearset) {
    240  1.20   mycroft 			t->tm_year += ATOI2(arg);
    241   1.2     glass 		} else {
    242   1.2     glass 			yearset = ATOI2(arg);
    243   1.2     glass 			if (yearset < 69)
    244  1.20   mycroft 				t->tm_year = yearset + 2000 - TM_YEAR_BASE;
    245   1.2     glass 			else
    246  1.20   mycroft 				t->tm_year = yearset + 1900 - TM_YEAR_BASE;
    247   1.1       cgd 		}
    248   1.2     glass 		/* FALLTHROUGH */
    249   1.2     glass 	case 8:				/* MMDDhhmm */
    250   1.2     glass 		t->tm_mon = ATOI2(arg);
    251   1.2     glass 		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
    252  1.20   mycroft 		/* FALLTHROUGH */
    253  1.20   mycroft 	case 6:
    254   1.2     glass 		t->tm_mday = ATOI2(arg);
    255  1.20   mycroft 		/* FALLTHROUGH */
    256  1.20   mycroft 	case 4:
    257   1.2     glass 		t->tm_hour = ATOI2(arg);
    258  1.20   mycroft 		/* FALLTHROUGH */
    259  1.20   mycroft 	case 2:
    260   1.2     glass 		t->tm_min = ATOI2(arg);
    261   1.2     glass 		break;
    262   1.2     glass 	default:
    263   1.2     glass 		goto terr;
    264   1.1       cgd 	}
    265   1.2     glass 
    266   1.2     glass 	t->tm_isdst = -1;		/* Figure out DST. */
    267   1.2     glass 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
    268   1.2     glass 	if (tvp[0].tv_sec == -1)
    269  1.31  christos terr:		errx(EXIT_FAILURE,
    270   1.2     glass 	"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
    271   1.2     glass 
    272   1.2     glass 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
    273   1.2     glass }
    274   1.2     glass 
    275  1.30     joerg static void
    276  1.30     joerg stime_arg2(char *arg, int year, struct timeval *tvp)
    277   1.2     glass {
    278   1.2     glass 	struct tm *t;
    279  1.10       cgd 	time_t tmptime;
    280   1.2     glass 					/* Start with the current time. */
    281  1.10       cgd 	tmptime = tvp[0].tv_sec;
    282  1.10       cgd 	if ((t = localtime(&tmptime)) == NULL)
    283  1.31  christos 		err(EXIT_FAILURE, "localtime");
    284   1.2     glass 
    285   1.2     glass 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
    286   1.2     glass 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
    287   1.2     glass 	t->tm_mday = ATOI2(arg);
    288   1.2     glass 	t->tm_hour = ATOI2(arg);
    289   1.2     glass 	t->tm_min = ATOI2(arg);
    290  1.21   mycroft 	if (year) {
    291  1.21   mycroft 		year = ATOI2(arg);
    292  1.21   mycroft 		if (year < 69)
    293  1.21   mycroft 			t->tm_year = year + 2000 - TM_YEAR_BASE;
    294  1.21   mycroft 		else
    295  1.21   mycroft 			t->tm_year = year + 1900 - TM_YEAR_BASE;
    296  1.21   mycroft 	}
    297  1.23   mycroft 	t->tm_sec = 0;
    298   1.2     glass 
    299   1.2     glass 	t->tm_isdst = -1;		/* Figure out DST. */
    300   1.2     glass 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
    301   1.2     glass 	if (tvp[0].tv_sec == -1)
    302  1.31  christos 		errx(EXIT_FAILURE,
    303   1.2     glass 	"out of range or illegal time specification: MMDDhhmm[yy]");
    304   1.2     glass 
    305   1.2     glass 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
    306   1.2     glass }
    307   1.2     glass 
    308  1.30     joerg static void
    309  1.30     joerg stime_file(char *fname, struct timeval *tvp)
    310   1.2     glass {
    311   1.2     glass 	struct stat sb;
    312   1.2     glass 
    313   1.2     glass 	if (stat(fname, &sb))
    314   1.2     glass 		err(1, "%s", fname);
    315  1.14   mycroft 	TIMESPEC_TO_TIMEVAL(&tvp[0], &sb.st_atimespec);
    316  1.14   mycroft 	TIMESPEC_TO_TIMEVAL(&tvp[1], &sb.st_mtimespec);
    317   1.1       cgd }
    318   1.1       cgd 
    319  1.30     joerg static void
    320  1.30     joerg usage(void)
    321   1.1       cgd {
    322   1.2     glass 	(void)fprintf(stderr,
    323  1.31  christos 	    "Usage: %s [-acfhm] [-d datetime] [-r file] [-t time] file ...\n",
    324  1.31  christos 	    getprogname());
    325  1.31  christos 	exit(EXIT_FAILURE);
    326   1.1       cgd }
    327