Home | History | Annotate | Line # | Download | only in touch
touch.c revision 1.40
      1  1.40       kre /*	$NetBSD: touch.c,v 1.40 2024/02/10 00:19:30 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.40       kre __RCSID("$NetBSD: touch.c,v 1.40 2024/02/10 00:19:30 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.37       kre #include <ctype.h>
     50   1.9       jtc #include <err.h>
     51   1.2     glass #include <errno.h>
     52   1.2     glass #include <fcntl.h>
     53  1.37       kre #include <limits.h>
     54  1.37       kre #include <math.h>
     55   1.1       cgd #include <stdio.h>
     56   1.2     glass #include <stdlib.h>
     57   1.2     glass #include <string.h>
     58   1.8       jtc #include <locale.h>
     59   1.2     glass #include <time.h>
     60  1.19   mycroft #include <tzfile.h>
     61   1.2     glass #include <unistd.h>
     62  1.31  christos #include <util.h>
     63  1.32  christos #include <getopt.h>
     64   1.2     glass 
     65  1.36       kre static void	stime_arg0(const char *, struct timespec *);
     66  1.33     enami static void	stime_arg1(char *, struct timespec *);
     67  1.36       kre static void	stime_arg2(const char *, int, struct timespec *);
     68  1.39       kre static void	stime_file(const char *, struct timespec *,
     69  1.39       kre 		   int (const char *, struct stat *));
     70  1.37       kre static int	stime_posix(const char *, struct timespec *);
     71  1.38       kre static int	difftm(const struct tm *, const struct tm *);
     72  1.30     joerg __dead static void	usage(void);
     73   1.1       cgd 
     74  1.32  christos struct option touch_longopts[] = {
     75  1.32  christos 	{ "date",		required_argument,	0,
     76  1.32  christos 						'd' },
     77  1.32  christos 	{ "reference",		required_argument,	0,
     78  1.32  christos 						'r' },
     79  1.32  christos 	{ NULL,			0,			0,
     80  1.32  christos 						0 },
     81  1.32  christos };
     82  1.32  christos 
     83  1.35       kre #define	YEAR_BOUNDARY		69
     84  1.35       kre #define	LOW_YEAR_CENTURY	2000	/* for 2 digit years < YEAR_BOUNDARY */
     85  1.35       kre #define	HIGH_YEAR_CENTURY	1900	/* for 2 digit years >=  "  */
     86  1.35       kre 
     87  1.35       kre #define	NO_TIME		((time_t)-1)	/* time_t might be unsigned */
     88  1.35       kre 
     89   1.2     glass int
     90  1.30     joerg main(int argc, char *argv[])
     91   1.1       cgd {
     92   1.2     glass 	struct stat sb;
     93  1.33     enami 	struct timespec ts[2];
     94  1.40       kre 	int aflag, cflag, Dflag, hflag, mflag, ch, fd, len, rval, timeset;
     95   1.2     glass 	char *p;
     96  1.33     enami 	int (*change_file_times)(const char *, const struct timespec *);
     97  1.30     joerg 	int (*get_file_status)(const char *, struct stat *);
     98   1.2     glass 
     99   1.8       jtc 	setlocale(LC_ALL, "");
    100   1.8       jtc 
    101  1.40       kre 	aflag = cflag = Dflag = hflag = mflag = timeset = 0;
    102  1.33     enami 	if (clock_gettime(CLOCK_REALTIME, &ts[0]))
    103  1.33     enami 		err(1, "clock_gettime");
    104   1.2     glass 
    105  1.40       kre 	while ((ch = getopt_long(argc, argv, "acDd:fhmR:r:t:", touch_longopts,
    106  1.32  christos 	    NULL)) != -1)
    107  1.34       kre 		switch (ch) {
    108   1.2     glass 		case 'a':
    109   1.2     glass 			aflag = 1;
    110   1.2     glass 			break;
    111   1.1       cgd 		case 'c':
    112   1.2     glass 			cflag = 1;
    113   1.1       cgd 			break;
    114  1.40       kre 		case 'D':
    115  1.40       kre 			Dflag = 1;
    116  1.40       kre 			break;
    117  1.31  christos 		case 'd':
    118  1.31  christos 			timeset = 1;
    119  1.37       kre 			if (!stime_posix(optarg, ts))
    120  1.37       kre 				stime_arg0(optarg, ts);
    121  1.31  christos 			break;
    122   1.1       cgd 		case 'f':
    123   1.2     glass 			break;
    124  1.12     enami 		case 'h':
    125  1.12     enami 			hflag = 1;
    126  1.12     enami 			break;
    127   1.2     glass 		case 'm':
    128   1.2     glass 			mflag = 1;
    129   1.2     glass 			break;
    130  1.39       kre 		case 'R':
    131  1.39       kre 			timeset = 1;
    132  1.39       kre 			stime_file(optarg, ts, lstat);
    133  1.39       kre 			break;
    134   1.2     glass 		case 'r':
    135   1.2     glass 			timeset = 1;
    136  1.39       kre 			stime_file(optarg, ts, stat);
    137   1.1       cgd 			break;
    138   1.2     glass 		case 't':
    139   1.2     glass 			timeset = 1;
    140  1.33     enami 			stime_arg1(optarg, ts);
    141   1.2     glass 			break;
    142   1.1       cgd 		case '?':
    143   1.1       cgd 		default:
    144   1.1       cgd 			usage();
    145   1.1       cgd 		}
    146   1.2     glass 	argc -= optind;
    147   1.2     glass 	argv += optind;
    148   1.2     glass 
    149   1.2     glass 	/* Default is both -a and -m. */
    150   1.2     glass 	if (aflag == 0 && mflag == 0)
    151   1.2     glass 		aflag = mflag = 1;
    152   1.2     glass 
    153  1.17      fvdl 	if (hflag) {
    154  1.12     enami 		cflag = 1;		/* Don't create new file */
    155  1.33     enami 		change_file_times = lutimens;
    156  1.17      fvdl 		get_file_status = lstat;
    157  1.17      fvdl 	} else {
    158  1.33     enami 		change_file_times = utimens;
    159  1.17      fvdl 		get_file_status = stat;
    160  1.17      fvdl 	}
    161  1.12     enami 
    162   1.2     glass 	/*
    163   1.2     glass 	 * If no -r or -t flag, at least two operands, the first of which
    164   1.2     glass 	 * is an 8 or 10 digit number, use the obsolete time specification.
    165   1.2     glass 	 */
    166   1.2     glass 	if (!timeset && argc > 1) {
    167   1.2     glass 		(void)strtol(argv[0], &p, 10);
    168   1.2     glass 		len = p - argv[0];
    169   1.2     glass 		if (*p == '\0' && (len == 8 || len == 10)) {
    170   1.2     glass 			timeset = 1;
    171  1.33     enami 			stime_arg2(*argv++, len == 10, ts);
    172   1.2     glass 		}
    173   1.2     glass 	}
    174   1.2     glass 
    175   1.2     glass 	/* Otherwise use the current time of day. */
    176   1.2     glass 	if (!timeset)
    177  1.33     enami 		ts[1] = ts[0];
    178   1.2     glass 
    179   1.2     glass 	if (*argv == NULL)
    180   1.1       cgd 		usage();
    181   1.2     glass 
    182  1.31  christos 	for (rval = EXIT_SUCCESS; *argv; ++argv) {
    183   1.2     glass 		/* See if the file exists. */
    184  1.22      ross 		if ((*get_file_status)(*argv, &sb)) {
    185   1.2     glass 			if (!cflag) {
    186   1.2     glass 				/* Create the file. */
    187   1.2     glass 				fd = open(*argv,
    188   1.2     glass 				    O_WRONLY | O_CREAT, DEFFILEMODE);
    189   1.2     glass 				if (fd == -1 || fstat(fd, &sb) || close(fd)) {
    190  1.31  christos 					rval = EXIT_FAILURE;
    191   1.2     glass 					warn("%s", *argv);
    192   1.2     glass 					continue;
    193   1.2     glass 				}
    194   1.2     glass 
    195   1.2     glass 				/* If using the current time, we're done. */
    196   1.2     glass 				if (!timeset)
    197   1.2     glass 					continue;
    198   1.2     glass 			} else
    199   1.2     glass 				continue;
    200  1.22      ross 		}
    201   1.2     glass 		if (!aflag)
    202  1.33     enami 			ts[0] = sb.st_atimespec;
    203   1.2     glass 		if (!mflag)
    204  1.33     enami 			ts[1] = sb.st_mtimespec;
    205   1.2     glass 
    206  1.40       kre 		if (Dflag &&
    207  1.40       kre 		    timespeccmp(&ts[0], &sb.st_atimespec, ==) &&
    208  1.40       kre 		    timespeccmp(&ts[1], &sb.st_mtimespec, ==))
    209  1.40       kre 			continue;
    210  1.40       kre 
    211   1.2     glass 		/* Try utimes(2). */
    212  1.33     enami 		if (!(*change_file_times)(*argv, ts))
    213   1.2     glass 			continue;
    214   1.2     glass 
    215   1.2     glass 		/* If the user specified a time, nothing else we can do. */
    216   1.2     glass 		if (timeset) {
    217  1.31  christos 			rval = EXIT_FAILURE;
    218   1.2     glass 			warn("%s", *argv);
    219   1.2     glass 		}
    220   1.2     glass 
    221   1.2     glass 		/*
    222   1.2     glass 		 * System V and POSIX 1003.1 require that a NULL argument
    223   1.2     glass 		 * set the access/modification times to the current time.
    224   1.2     glass 		 * The permission checks are different, too, in that the
    225   1.2     glass 		 * ability to write the file is sufficient.  Take a shot.
    226   1.2     glass 		 */
    227  1.17      fvdl 		 if (!(*change_file_times)(*argv, NULL))
    228   1.2     glass 			continue;
    229   1.2     glass 
    230  1.31  christos 		rval = EXIT_FAILURE;
    231  1.28      yamt 		warn("%s", *argv);
    232   1.2     glass 	}
    233   1.2     glass 	exit(rval);
    234   1.1       cgd }
    235   1.1       cgd 
    236  1.20   mycroft #define	ATOI2(s)	((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
    237   1.2     glass 
    238  1.30     joerg static void
    239  1.36       kre stime_arg0(const char *arg, struct timespec *tsp)
    240  1.31  christos {
    241  1.33     enami 	tsp[1].tv_sec = tsp[0].tv_sec = parsedate(arg, NULL, NULL);
    242  1.35       kre 	if (tsp[0].tv_sec == NO_TIME)
    243  1.31  christos 		errx(EXIT_FAILURE, "Could not parse `%s'", arg);
    244  1.33     enami 	tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
    245  1.31  christos }
    246  1.31  christos 
    247  1.31  christos static void
    248  1.33     enami stime_arg1(char *arg, struct timespec *tsp)
    249   1.1       cgd {
    250  1.38       kre 	struct tm *t, tm;
    251  1.10       cgd 	time_t tmptime;
    252   1.2     glass 	int yearset;
    253   1.2     glass 	char *p;
    254  1.35       kre 	char *initarg = arg;
    255  1.35       kre 
    256   1.2     glass 					/* Start with the current time. */
    257  1.33     enami 	tmptime = tsp[0].tv_sec;
    258  1.10       cgd 	if ((t = localtime(&tmptime)) == NULL)
    259  1.31  christos 		err(EXIT_FAILURE, "localtime");
    260  1.35       kre 					/* [[CC]YY]MMDDhhmm[.ss] */
    261   1.2     glass 	if ((p = strchr(arg, '.')) == NULL)
    262   1.2     glass 		t->tm_sec = 0;		/* Seconds defaults to 0. */
    263   1.2     glass 	else {
    264   1.2     glass 		if (strlen(p + 1) != 2)
    265   1.2     glass 			goto terr;
    266   1.2     glass 		*p++ = '\0';
    267   1.2     glass 		t->tm_sec = ATOI2(p);
    268   1.2     glass 	}
    269   1.2     glass 
    270   1.2     glass 	yearset = 0;
    271  1.20   mycroft 	switch (strlen(arg)) {
    272   1.2     glass 	case 12:			/* CCYYMMDDhhmm */
    273  1.20   mycroft 		t->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE;
    274   1.2     glass 		yearset = 1;
    275  1.25   tsutsui 		/* FALLTHROUGH */
    276   1.2     glass 	case 10:			/* YYMMDDhhmm */
    277   1.2     glass 		if (yearset) {
    278  1.20   mycroft 			t->tm_year += ATOI2(arg);
    279   1.2     glass 		} else {
    280   1.2     glass 			yearset = ATOI2(arg);
    281  1.35       kre 			if (yearset < YEAR_BOUNDARY)
    282  1.35       kre 				t->tm_year = yearset +
    283  1.35       kre 				    LOW_YEAR_CENTURY - TM_YEAR_BASE;
    284   1.2     glass 			else
    285  1.35       kre 				t->tm_year = yearset +
    286  1.35       kre 				    HIGH_YEAR_CENTURY - TM_YEAR_BASE;
    287   1.1       cgd 		}
    288   1.2     glass 		/* FALLTHROUGH */
    289   1.2     glass 	case 8:				/* MMDDhhmm */
    290   1.2     glass 		t->tm_mon = ATOI2(arg);
    291   1.2     glass 		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
    292  1.20   mycroft 		/* FALLTHROUGH */
    293  1.20   mycroft 	case 6:
    294   1.2     glass 		t->tm_mday = ATOI2(arg);
    295  1.20   mycroft 		/* FALLTHROUGH */
    296  1.20   mycroft 	case 4:
    297   1.2     glass 		t->tm_hour = ATOI2(arg);
    298  1.20   mycroft 		/* FALLTHROUGH */
    299  1.20   mycroft 	case 2:
    300   1.2     glass 		t->tm_min = ATOI2(arg);
    301   1.2     glass 		break;
    302   1.2     glass 	default:
    303   1.2     glass 		goto terr;
    304   1.1       cgd 	}
    305   1.2     glass 
    306   1.2     glass 	t->tm_isdst = -1;		/* Figure out DST. */
    307  1.38       kre 	tm = *t;
    308  1.33     enami 	tsp[0].tv_sec = tsp[1].tv_sec = mktime(t);
    309  1.38       kre 	if (tsp[0].tv_sec == NO_TIME || difftm(t, &tm))
    310  1.35       kre  terr:		errx(EXIT_FAILURE, "out of range or bad time specification:\n"
    311  1.35       kre 		    "\t'%s' should be [[CC]YY]MMDDhhmm[.ss]", initarg);
    312   1.2     glass 
    313  1.33     enami 	tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
    314   1.2     glass }
    315   1.2     glass 
    316  1.30     joerg static void
    317  1.36       kre stime_arg2(const char *arg, int year, struct timespec *tsp)
    318   1.2     glass {
    319  1.38       kre 	struct tm *t, tm;
    320  1.10       cgd 	time_t tmptime;
    321   1.2     glass 					/* Start with the current time. */
    322  1.33     enami 	tmptime = tsp[0].tv_sec;
    323  1.10       cgd 	if ((t = localtime(&tmptime)) == NULL)
    324  1.31  christos 		err(EXIT_FAILURE, "localtime");
    325   1.2     glass 
    326   1.2     glass 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
    327   1.2     glass 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
    328   1.2     glass 	t->tm_mday = ATOI2(arg);
    329   1.2     glass 	t->tm_hour = ATOI2(arg);
    330   1.2     glass 	t->tm_min = ATOI2(arg);
    331  1.21   mycroft 	if (year) {
    332  1.21   mycroft 		year = ATOI2(arg);
    333  1.35       kre 		if (year < YEAR_BOUNDARY)
    334  1.35       kre 			t->tm_year = year + LOW_YEAR_CENTURY - TM_YEAR_BASE;
    335  1.21   mycroft 		else
    336  1.35       kre 			t->tm_year = year + HIGH_YEAR_CENTURY - TM_YEAR_BASE;
    337  1.21   mycroft 	}
    338  1.23   mycroft 	t->tm_sec = 0;
    339   1.2     glass 
    340   1.2     glass 	t->tm_isdst = -1;		/* Figure out DST. */
    341  1.38       kre 	tm = *t;
    342  1.33     enami 	tsp[0].tv_sec = tsp[1].tv_sec = mktime(t);
    343  1.38       kre 	if (tsp[0].tv_sec == NO_TIME || difftm(t, &tm))
    344  1.31  christos 		errx(EXIT_FAILURE,
    345  1.35       kre 		    "out of range or bad time specification: MMDDhhmm[YY]");
    346   1.2     glass 
    347  1.33     enami 	tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
    348   1.2     glass }
    349   1.2     glass 
    350  1.30     joerg static void
    351  1.39       kre stime_file(const char *fname, struct timespec *tsp,
    352  1.39       kre     int statfunc(const char *, struct stat *))
    353   1.2     glass {
    354   1.2     glass 	struct stat sb;
    355   1.2     glass 
    356  1.39       kre 	if (statfunc(fname, &sb))
    357   1.2     glass 		err(1, "%s", fname);
    358  1.33     enami 	tsp[0] = sb.st_atimespec;
    359  1.33     enami 	tsp[1] = sb.st_mtimespec;
    360   1.1       cgd }
    361   1.1       cgd 
    362  1.37       kre static int
    363  1.37       kre stime_posix(const char *arg, struct timespec *tsp)
    364  1.37       kre {
    365  1.38       kre 	struct tm tm, tms;
    366  1.37       kre 	const char *p;
    367  1.37       kre 	char *ep;
    368  1.37       kre 	int utc = 0;
    369  1.37       kre 	long val;
    370  1.37       kre 
    371  1.37       kre #define	isdigch(c)	(isdigit((int)((unsigned char)(c))))
    372  1.37       kre 
    373  1.37       kre 	if ((p = strchr(arg, '-')) == NULL)
    374  1.37       kre 		return 0;
    375  1.37       kre 	if (p - arg < 4)	/* at least 4 year digits required */
    376  1.37       kre 		return 0;
    377  1.37       kre 
    378  1.37       kre 	if (!isdigch(arg[0]))	/* and the first must be a digit! */
    379  1.37       kre 		return 0;
    380  1.37       kre 
    381  1.38       kre 	(void)memset(&tm, 0, sizeof tm);
    382  1.38       kre 
    383  1.37       kre 	errno = 0;
    384  1.37       kre 	val = strtol(arg, &ep, 10);		/* YYYY */
    385  1.37       kre 	if (val < 0 || val > INT_MAX)
    386  1.37       kre 		return 0;
    387  1.37       kre 	if (*ep != '-')
    388  1.37       kre 		return 0;
    389  1.37       kre 	tm.tm_year = (int)val - 1900;
    390  1.37       kre 
    391  1.37       kre 	p = ep + 1;
    392  1.37       kre 
    393  1.37       kre 	if (!isdigch(*p))
    394  1.37       kre 		return 0;
    395  1.37       kre 	val = strtol(p, &ep, 10);		/* MM */
    396  1.37       kre 	if (val < 1 || val > 12)
    397  1.37       kre 		return 0;
    398  1.37       kre 	if (*ep != '-' || ep != p + 2)
    399  1.37       kre 		return 0;
    400  1.37       kre 	tm.tm_mon = (int)val - 1;
    401  1.37       kre 
    402  1.37       kre 	p = ep + 1;
    403  1.37       kre 
    404  1.37       kre 	if (!isdigch(*p))
    405  1.37       kre 		return 0;
    406  1.37       kre 	val = strtol(p, &ep, 10);		/* DD */
    407  1.37       kre 	if (val < 1 || val > 31)
    408  1.37       kre 		return 0;
    409  1.37       kre 	if ((*ep != 'T' && *ep != ' ') || ep != p + 2)
    410  1.37       kre 		return 0;
    411  1.37       kre 	tm.tm_mday = (int)val;
    412  1.37       kre 
    413  1.37       kre 	p = ep + 1;
    414  1.37       kre 
    415  1.37       kre 	if (!isdigch(*p))
    416  1.37       kre 		return 0;
    417  1.37       kre 	val = strtol(p, &ep, 10);		/* hh */
    418  1.37       kre 	if (val < 0 || val > 23)
    419  1.37       kre 		return 0;
    420  1.37       kre 	if (*ep != ':' || ep != p + 2)
    421  1.37       kre 		return 0;
    422  1.37       kre 	tm.tm_hour = (int)val;
    423  1.37       kre 
    424  1.37       kre 	p = ep + 1;
    425  1.37       kre 
    426  1.37       kre 	if (!isdigch(*p))
    427  1.37       kre 		return 0;
    428  1.37       kre 	val = strtol(p, &ep, 10);		/* mm */
    429  1.37       kre 	if (val < 0 || val > 59)
    430  1.37       kre 		return 0;
    431  1.37       kre 	if (*ep != ':' || ep != p + 2)
    432  1.37       kre 		return 0;
    433  1.37       kre 	tm.tm_min = (int)val;
    434  1.37       kre 
    435  1.37       kre 	p = ep + 1;
    436  1.37       kre 
    437  1.37       kre 	if (!isdigch(*p))
    438  1.37       kre 		return 0;
    439  1.37       kre 	val = strtol(p, &ep, 10);		/* ss (or in POSIX, SS) */
    440  1.37       kre 	if (val < 0 || val > 60)
    441  1.37       kre 		return 0;
    442  1.37       kre 	if ((*ep != '.' && *ep != ',' && *ep != 'Z' && *ep != '\0') ||
    443  1.37       kre 	      ep != p + 2)
    444  1.37       kre 		return 0;
    445  1.37       kre 	tm.tm_sec = (int)val;
    446  1.37       kre 
    447  1.37       kre 	if (*ep == ',' || *ep == '.') {
    448  1.37       kre 		double frac;
    449  1.37       kre 		ptrdiff_t fdigs;
    450  1.37       kre 
    451  1.37       kre 		p = ep + 1;
    452  1.37       kre 		if (!isdigch(*p))
    453  1.37       kre 			return 0;
    454  1.37       kre 		val = strtol(p, &ep, 10);
    455  1.37       kre 		if (val < 0)
    456  1.37       kre 			return 0;
    457  1.37       kre 		if (ep == p)	/* at least 1 digit required */
    458  1.37       kre 			return 0;
    459  1.37       kre 		if (*ep != 'Z' && *ep != '\0')
    460  1.37       kre 			return 0;
    461  1.37       kre 
    462  1.37       kre 		if (errno != 0)
    463  1.37       kre 			return 0;
    464  1.37       kre 
    465  1.37       kre 		fdigs = ep - p;
    466  1.37       kre 		if (fdigs > 15) {
    467  1.37       kre 			/* avoid being absurd */
    468  1.37       kre 			/* don't want to risk 10^fdigs being INF */
    469  1.37       kre 			if (val == 0)
    470  1.37       kre 				fdigs = 1;
    471  1.37       kre 			else while (fdigs > 15) {
    472  1.37       kre 				val = (val + 5) / 10;
    473  1.37       kre 				fdigs--;
    474  1.37       kre 			}
    475  1.37       kre 		}
    476  1.37       kre 
    477  1.37       kre 		frac = pow(10.0, (double)fdigs);
    478  1.37       kre 
    479  1.37       kre 		tsp[0].tv_nsec = tsp[1].tv_nsec =
    480  1.37       kre 			(long)round(((double)val / frac) * 1000000000.0);
    481  1.37       kre 	} else
    482  1.37       kre 		tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
    483  1.37       kre 
    484  1.37       kre 	if (*ep == 'Z') {
    485  1.37       kre 		if (ep[1] != '\0')
    486  1.37       kre 			return 0;
    487  1.37       kre 		utc = 1;
    488  1.37       kre 	}
    489  1.37       kre 
    490  1.37       kre 	if (errno != 0)
    491  1.37       kre 		return 0;
    492  1.37       kre 
    493  1.37       kre 	tm.tm_isdst = -1;
    494  1.38       kre 	tms = tm;
    495  1.37       kre 	if (utc)
    496  1.37       kre 		tsp[0].tv_sec = tsp[1].tv_sec = timegm(&tm);
    497  1.37       kre 	else
    498  1.37       kre 		tsp[0].tv_sec = tsp[1].tv_sec = mktime(&tm);
    499  1.37       kre 
    500  1.38       kre 	if ((errno != 0 && tsp[1].tv_sec == NO_TIME) || difftm(&tm, &tms))
    501  1.37       kre 		return 0;
    502  1.37       kre 
    503  1.37       kre 	return 1;
    504  1.37       kre }
    505  1.37       kre 
    506  1.38       kre /*
    507  1.38       kre  * Determine whether 2 struct tn's are different
    508  1.38       kre  * return true (1) if theu are, false (0) otherwise.
    509  1.38       kre  *
    510  1.38       kre  * Note that we only consider the fields that are set
    511  1.38       kre  * for mktime() to use - if mktime() returns them
    512  1.38       kre  * differently than was set, then there was a problem
    513  1.38       kre  * with the setting.
    514  1.38       kre  */
    515  1.38       kre static int
    516  1.38       kre difftm(const struct tm *t1, const struct tm *t2)
    517  1.38       kre {
    518  1.38       kre #define CHK(fld) do {						\
    519  1.38       kre 			if (t1->tm_##fld != t2->tm_##fld) {	\
    520  1.38       kre 				return  1;			\
    521  1.38       kre 			}					\
    522  1.38       kre 		} while(/*CONSTCOND*/0)
    523  1.38       kre 
    524  1.38       kre 	CHK(year);
    525  1.38       kre 	CHK(mon);
    526  1.38       kre 	CHK(mday);
    527  1.38       kre 	CHK(hour);
    528  1.38       kre 	CHK(min);
    529  1.38       kre 	CHK(sec);
    530  1.38       kre 
    531  1.38       kre 	return 0;
    532  1.38       kre }
    533  1.38       kre 
    534  1.30     joerg static void
    535  1.30     joerg usage(void)
    536   1.1       cgd {
    537   1.2     glass 	(void)fprintf(stderr,
    538  1.40       kre 	    "Usage: %s [-acDfhm] [-d|--date datetime] [-R|-r|--reference file]"
    539  1.34       kre 	    " [-t time] file ...\n", getprogname());
    540  1.31  christos 	exit(EXIT_FAILURE);
    541   1.1       cgd }
    542