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