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