xref: /titanic_51/usr/src/cmd/touch/touch.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
31*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <string.h>
39*7c478bd9Sstevel@tonic-gate #include <strings.h>
40*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
41*7c478bd9Sstevel@tonic-gate #include <ctype.h>
42*7c478bd9Sstevel@tonic-gate #include <libgen.h>
43*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
44*7c478bd9Sstevel@tonic-gate #include <pwd.h>
45*7c478bd9Sstevel@tonic-gate #include <time.h>
46*7c478bd9Sstevel@tonic-gate #include <unistd.h>
47*7c478bd9Sstevel@tonic-gate #include <locale.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
49*7c478bd9Sstevel@tonic-gate #include <errno.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #define	BADTIME	"bad time specification"
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static char	*myname;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate static int isnumber(char *);
56*7c478bd9Sstevel@tonic-gate static int atoi_for2(char *);
57*7c478bd9Sstevel@tonic-gate static void usage(const int);
58*7c478bd9Sstevel@tonic-gate static void touchabort(const char *);
59*7c478bd9Sstevel@tonic-gate static void parse_time(char *, timestruc_t *);
60*7c478bd9Sstevel@tonic-gate static void parse_datetime(char *, timestruc_t *);
61*7c478bd9Sstevel@tonic-gate static void timestruc_to_timeval(timestruc_t *, struct timeval *);
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate int
64*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
65*7c478bd9Sstevel@tonic-gate {
66*7c478bd9Sstevel@tonic-gate 	int c;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	int		aflag	= 0;
69*7c478bd9Sstevel@tonic-gate 	int		cflag	= 0;
70*7c478bd9Sstevel@tonic-gate 	int		rflag	= 0;
71*7c478bd9Sstevel@tonic-gate 	int		mflag	= 0;
72*7c478bd9Sstevel@tonic-gate 	int		tflag	= 0;
73*7c478bd9Sstevel@tonic-gate 	int		stflag	= 0;
74*7c478bd9Sstevel@tonic-gate 	int		status	= 0;
75*7c478bd9Sstevel@tonic-gate 	int		usecurrenttime = 1;
76*7c478bd9Sstevel@tonic-gate 	int		timespecified;
77*7c478bd9Sstevel@tonic-gate 	int		optc;
78*7c478bd9Sstevel@tonic-gate 	int		fd;
79*7c478bd9Sstevel@tonic-gate 	struct stat	stbuf;
80*7c478bd9Sstevel@tonic-gate 	struct stat	prstbuf;
81*7c478bd9Sstevel@tonic-gate 	struct timeval	times[2];
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
84*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
85*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
86*7c478bd9Sstevel@tonic-gate #endif
87*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	myname = basename(argv[0]);
90*7c478bd9Sstevel@tonic-gate 	if (strcmp(myname, "settime") == NULL) {
91*7c478bd9Sstevel@tonic-gate 		cflag++;
92*7c478bd9Sstevel@tonic-gate 		stflag++;
93*7c478bd9Sstevel@tonic-gate 		while ((optc = getopt(argc, argv, "f:")) != EOF)
94*7c478bd9Sstevel@tonic-gate 			switch (optc) {
95*7c478bd9Sstevel@tonic-gate 			case 'f':
96*7c478bd9Sstevel@tonic-gate 				rflag++;
97*7c478bd9Sstevel@tonic-gate 				usecurrenttime = 0;
98*7c478bd9Sstevel@tonic-gate 				if (stat(optarg, &prstbuf) == -1) {
99*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "%s: ", myname);
100*7c478bd9Sstevel@tonic-gate 					perror(optarg);
101*7c478bd9Sstevel@tonic-gate 					return (2);
102*7c478bd9Sstevel@tonic-gate 				}
103*7c478bd9Sstevel@tonic-gate 				break;
104*7c478bd9Sstevel@tonic-gate 			case '?':
105*7c478bd9Sstevel@tonic-gate 				usage(stflag);
106*7c478bd9Sstevel@tonic-gate 				break;
107*7c478bd9Sstevel@tonic-gate 			};
108*7c478bd9Sstevel@tonic-gate 	} else
109*7c478bd9Sstevel@tonic-gate 		while ((optc = getopt(argc, argv, "acfmr:t:")) != EOF)
110*7c478bd9Sstevel@tonic-gate 			switch (optc) {
111*7c478bd9Sstevel@tonic-gate 			case 'a':
112*7c478bd9Sstevel@tonic-gate 				aflag++;
113*7c478bd9Sstevel@tonic-gate 				usecurrenttime = 0;
114*7c478bd9Sstevel@tonic-gate 				break;
115*7c478bd9Sstevel@tonic-gate 			case 'c':
116*7c478bd9Sstevel@tonic-gate 				cflag++;
117*7c478bd9Sstevel@tonic-gate 				break;
118*7c478bd9Sstevel@tonic-gate 			case 'f':	/* silently ignore for UCB compat */
119*7c478bd9Sstevel@tonic-gate 				break;
120*7c478bd9Sstevel@tonic-gate 			case 'm':
121*7c478bd9Sstevel@tonic-gate 				mflag++;
122*7c478bd9Sstevel@tonic-gate 				usecurrenttime = 0;
123*7c478bd9Sstevel@tonic-gate 				break;
124*7c478bd9Sstevel@tonic-gate 			case 'r':	/* same as settime's -f option */
125*7c478bd9Sstevel@tonic-gate 				rflag++;
126*7c478bd9Sstevel@tonic-gate 				usecurrenttime = 0;
127*7c478bd9Sstevel@tonic-gate 				if (stat(optarg, &prstbuf) == -1) {
128*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "%s: ", myname);
129*7c478bd9Sstevel@tonic-gate 					perror(optarg);
130*7c478bd9Sstevel@tonic-gate 					return (2);
131*7c478bd9Sstevel@tonic-gate 				}
132*7c478bd9Sstevel@tonic-gate 				break;
133*7c478bd9Sstevel@tonic-gate 			case 't':
134*7c478bd9Sstevel@tonic-gate 				tflag++;
135*7c478bd9Sstevel@tonic-gate 				usecurrenttime = 0;
136*7c478bd9Sstevel@tonic-gate 				parse_time(optarg, &prstbuf.st_mtim);
137*7c478bd9Sstevel@tonic-gate 				prstbuf.st_atim = prstbuf.st_mtim;
138*7c478bd9Sstevel@tonic-gate 				break;
139*7c478bd9Sstevel@tonic-gate 			case '?':
140*7c478bd9Sstevel@tonic-gate 				usage(stflag);
141*7c478bd9Sstevel@tonic-gate 				break;
142*7c478bd9Sstevel@tonic-gate 			}
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	argc -= optind;
145*7c478bd9Sstevel@tonic-gate 	argv += optind;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	if ((argc < 1) || (rflag + tflag > 1))
148*7c478bd9Sstevel@tonic-gate 		usage(stflag);
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	if ((aflag == 0) && (mflag == 0)) {
151*7c478bd9Sstevel@tonic-gate 		aflag = 1;
152*7c478bd9Sstevel@tonic-gate 		mflag = 1;
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	/*
156*7c478bd9Sstevel@tonic-gate 	 * If either -r or -t has been specified,
157*7c478bd9Sstevel@tonic-gate 	 * use the specified time.
158*7c478bd9Sstevel@tonic-gate 	 */
159*7c478bd9Sstevel@tonic-gate 	timespecified = rflag || tflag;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	if (timespecified == 0) {
162*7c478bd9Sstevel@tonic-gate 		if (argc >= 2 && isnumber(*argv) && (strlen(*argv) == 8 ||
163*7c478bd9Sstevel@tonic-gate 			strlen(*argv) == 10)) {
164*7c478bd9Sstevel@tonic-gate 			/*
165*7c478bd9Sstevel@tonic-gate 			 * time is specified as an operand.
166*7c478bd9Sstevel@tonic-gate 			 * use it.
167*7c478bd9Sstevel@tonic-gate 			 */
168*7c478bd9Sstevel@tonic-gate 			parse_datetime(*argv++, &prstbuf.st_mtim);
169*7c478bd9Sstevel@tonic-gate 			prstbuf.st_atim = prstbuf.st_mtim;
170*7c478bd9Sstevel@tonic-gate 			usecurrenttime = 0;
171*7c478bd9Sstevel@tonic-gate 			timespecified = 1;
172*7c478bd9Sstevel@tonic-gate 			argc--;
173*7c478bd9Sstevel@tonic-gate 		} else {
174*7c478bd9Sstevel@tonic-gate 			/*
175*7c478bd9Sstevel@tonic-gate 			 * no time information is specified.
176*7c478bd9Sstevel@tonic-gate 			 * use the current time.
177*7c478bd9Sstevel@tonic-gate 			 */
178*7c478bd9Sstevel@tonic-gate 			(void) gettimeofday(times, NULL);
179*7c478bd9Sstevel@tonic-gate 			times[1] = times[0];
180*7c478bd9Sstevel@tonic-gate 		}
181*7c478bd9Sstevel@tonic-gate 	}
182*7c478bd9Sstevel@tonic-gate 	for (c = 0; c < argc; c++) {
183*7c478bd9Sstevel@tonic-gate 		if (stat(argv[c], &stbuf)) {
184*7c478bd9Sstevel@tonic-gate 			/*
185*7c478bd9Sstevel@tonic-gate 			 * if stat failed for reasons other than ENOENT,
186*7c478bd9Sstevel@tonic-gate 			 * the file should not be created, since this
187*7c478bd9Sstevel@tonic-gate 			 * can clobber the contents of an existing file
188*7c478bd9Sstevel@tonic-gate 			 * (for example, a large file that results in overflow).
189*7c478bd9Sstevel@tonic-gate 			 */
190*7c478bd9Sstevel@tonic-gate 			if (errno != ENOENT) {
191*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
192*7c478bd9Sstevel@tonic-gate 				    gettext("%s: %s cannot stat\n"),
193*7c478bd9Sstevel@tonic-gate 				    myname, argv[c]);
194*7c478bd9Sstevel@tonic-gate 				status++;
195*7c478bd9Sstevel@tonic-gate 				continue;
196*7c478bd9Sstevel@tonic-gate 			} else if (cflag) {
197*7c478bd9Sstevel@tonic-gate 				continue;
198*7c478bd9Sstevel@tonic-gate 			} else if ((fd = creat(argv[c],
199*7c478bd9Sstevel@tonic-gate 			    (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)))
200*7c478bd9Sstevel@tonic-gate 			    < 0) {
201*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
202*7c478bd9Sstevel@tonic-gate 				    gettext("%s: %s cannot create\n"),
203*7c478bd9Sstevel@tonic-gate 				    myname, argv[c]);
204*7c478bd9Sstevel@tonic-gate 				status++;
205*7c478bd9Sstevel@tonic-gate 				continue;
206*7c478bd9Sstevel@tonic-gate 			} else {
207*7c478bd9Sstevel@tonic-gate 				(void) close(fd);
208*7c478bd9Sstevel@tonic-gate 				if (stat(argv[c], &stbuf)) {
209*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
210*7c478bd9Sstevel@tonic-gate 					    gettext("%s: %s cannot stat\n"),
211*7c478bd9Sstevel@tonic-gate 					    myname, argv[c]);
212*7c478bd9Sstevel@tonic-gate 					status++;
213*7c478bd9Sstevel@tonic-gate 					continue;
214*7c478bd9Sstevel@tonic-gate 				}
215*7c478bd9Sstevel@tonic-gate 			}
216*7c478bd9Sstevel@tonic-gate 		}
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 		if (mflag == 0) {
219*7c478bd9Sstevel@tonic-gate 			/* Keep the mtime of the file */
220*7c478bd9Sstevel@tonic-gate 			timestruc_to_timeval(&stbuf.st_mtim, times + 1);
221*7c478bd9Sstevel@tonic-gate 		} else {
222*7c478bd9Sstevel@tonic-gate 			if (timespecified) {
223*7c478bd9Sstevel@tonic-gate 				/* Set the specified time */
224*7c478bd9Sstevel@tonic-gate 				timestruc_to_timeval(&prstbuf.st_mtim,
225*7c478bd9Sstevel@tonic-gate 				    times + 1);
226*7c478bd9Sstevel@tonic-gate 			}
227*7c478bd9Sstevel@tonic-gate 			/* Otherwise, use the current time by gettimeofday */
228*7c478bd9Sstevel@tonic-gate 		}
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 		if (aflag == 0) {
231*7c478bd9Sstevel@tonic-gate 			/* Keep the atime of the file */
232*7c478bd9Sstevel@tonic-gate 			timestruc_to_timeval(&stbuf.st_atim, times);
233*7c478bd9Sstevel@tonic-gate 		} else {
234*7c478bd9Sstevel@tonic-gate 			if (timespecified) {
235*7c478bd9Sstevel@tonic-gate 				/* Set the specified time */
236*7c478bd9Sstevel@tonic-gate 				timestruc_to_timeval(&prstbuf.st_atim, times);
237*7c478bd9Sstevel@tonic-gate 			}
238*7c478bd9Sstevel@tonic-gate 			/* Otherwise, use the current time by gettimeofday */
239*7c478bd9Sstevel@tonic-gate 		}
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 		if (utimes(argv[c], (usecurrenttime) ? NULL : times)) {
242*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
243*7c478bd9Sstevel@tonic-gate 			    gettext("%s: cannot change times on %s\n"),
244*7c478bd9Sstevel@tonic-gate 			    myname, argv[c]);
245*7c478bd9Sstevel@tonic-gate 			status++;
246*7c478bd9Sstevel@tonic-gate 			continue;
247*7c478bd9Sstevel@tonic-gate 		}
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate 	return (status);
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate static int
253*7c478bd9Sstevel@tonic-gate isnumber(char *s)
254*7c478bd9Sstevel@tonic-gate {
255*7c478bd9Sstevel@tonic-gate 	int c;
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 	while ((c = *s++) != '\0')
258*7c478bd9Sstevel@tonic-gate 		if (!isdigit(c))
259*7c478bd9Sstevel@tonic-gate 			return (0);
260*7c478bd9Sstevel@tonic-gate 	return (1);
261*7c478bd9Sstevel@tonic-gate }
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate static void
265*7c478bd9Sstevel@tonic-gate parse_time(char *t, timestruc_t *ts)
266*7c478bd9Sstevel@tonic-gate {
267*7c478bd9Sstevel@tonic-gate 	int		century = 0;
268*7c478bd9Sstevel@tonic-gate 	int		seconds = 0;
269*7c478bd9Sstevel@tonic-gate 	char		*p;
270*7c478bd9Sstevel@tonic-gate 	time_t		when;
271*7c478bd9Sstevel@tonic-gate 	struct tm	tm;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	/*
274*7c478bd9Sstevel@tonic-gate 	 * time in the following format (defined by the touch(1) spec):
275*7c478bd9Sstevel@tonic-gate 	 *	[[CC]YY]MMDDhhmm[.SS]
276*7c478bd9Sstevel@tonic-gate 	 */
277*7c478bd9Sstevel@tonic-gate 	if ((p = strchr(t, '.')) != NULL) {
278*7c478bd9Sstevel@tonic-gate 		if (strchr(p+1, '.') != NULL)
279*7c478bd9Sstevel@tonic-gate 			touchabort(BADTIME);
280*7c478bd9Sstevel@tonic-gate 		seconds = atoi_for2(p+1);
281*7c478bd9Sstevel@tonic-gate 		*p = '\0';
282*7c478bd9Sstevel@tonic-gate 	}
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 	(void) memset(&tm, 0, sizeof (struct tm));
285*7c478bd9Sstevel@tonic-gate 	when = time(0);
286*7c478bd9Sstevel@tonic-gate 	tm.tm_year = localtime(&when)->tm_year;
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	switch (strlen(t)) {
289*7c478bd9Sstevel@tonic-gate 		case 12:	/* CCYYMMDDhhmm */
290*7c478bd9Sstevel@tonic-gate 			century = atoi_for2(t);
291*7c478bd9Sstevel@tonic-gate 			t += 2;
292*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
293*7c478bd9Sstevel@tonic-gate 		case 10:	/* YYMMDDhhmm */
294*7c478bd9Sstevel@tonic-gate 			tm.tm_year = atoi_for2(t);
295*7c478bd9Sstevel@tonic-gate 			t += 2;
296*7c478bd9Sstevel@tonic-gate 			if (century == 0) {
297*7c478bd9Sstevel@tonic-gate 				if (tm.tm_year < 69)
298*7c478bd9Sstevel@tonic-gate 					tm.tm_year += 100;
299*7c478bd9Sstevel@tonic-gate 			} else
300*7c478bd9Sstevel@tonic-gate 				tm.tm_year += (century - 19) * 100;
301*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
302*7c478bd9Sstevel@tonic-gate 		case 8:		/* MMDDhhmm */
303*7c478bd9Sstevel@tonic-gate 			tm.tm_mon = atoi_for2(t) - 1;
304*7c478bd9Sstevel@tonic-gate 			t += 2;
305*7c478bd9Sstevel@tonic-gate 			tm.tm_mday = atoi_for2(t);
306*7c478bd9Sstevel@tonic-gate 			t += 2;
307*7c478bd9Sstevel@tonic-gate 			tm.tm_hour = atoi_for2(t);
308*7c478bd9Sstevel@tonic-gate 			t += 2;
309*7c478bd9Sstevel@tonic-gate 			tm.tm_min = atoi_for2(t);
310*7c478bd9Sstevel@tonic-gate 			tm.tm_sec = seconds;
311*7c478bd9Sstevel@tonic-gate 			break;
312*7c478bd9Sstevel@tonic-gate 		default:
313*7c478bd9Sstevel@tonic-gate 			touchabort(BADTIME);
314*7c478bd9Sstevel@tonic-gate 	}
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	if ((when = mktime(&tm)) == -1)
317*7c478bd9Sstevel@tonic-gate 		touchabort(BADTIME);
318*7c478bd9Sstevel@tonic-gate 	if (tm.tm_isdst)
319*7c478bd9Sstevel@tonic-gate 		when -= (timezone-altzone);
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	ts->tv_sec = when;
322*7c478bd9Sstevel@tonic-gate 	ts->tv_nsec = 0;
323*7c478bd9Sstevel@tonic-gate }
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate static void
326*7c478bd9Sstevel@tonic-gate parse_datetime(char *t, timestruc_t *ts)
327*7c478bd9Sstevel@tonic-gate {
328*7c478bd9Sstevel@tonic-gate 	time_t		when;
329*7c478bd9Sstevel@tonic-gate 	struct tm	tm;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	/*
332*7c478bd9Sstevel@tonic-gate 	 * time in the following format (defined by the touch(1) spec):
333*7c478bd9Sstevel@tonic-gate 	 *	MMDDhhmm[yy]
334*7c478bd9Sstevel@tonic-gate 	 */
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate 	(void) memset(&tm, 0, sizeof (struct tm));
337*7c478bd9Sstevel@tonic-gate 	when = time(0);
338*7c478bd9Sstevel@tonic-gate 	tm.tm_year = localtime(&when)->tm_year;
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	switch (strlen(t)) {
341*7c478bd9Sstevel@tonic-gate 		case 10:	/* MMDDhhmmyy */
342*7c478bd9Sstevel@tonic-gate 			tm.tm_year = atoi_for2(t+8);
343*7c478bd9Sstevel@tonic-gate 			if (tm.tm_year < 69)
344*7c478bd9Sstevel@tonic-gate 				tm.tm_year += 100;
345*7c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
346*7c478bd9Sstevel@tonic-gate 		case 8:		/* MMDDhhmm */
347*7c478bd9Sstevel@tonic-gate 			tm.tm_mon = atoi_for2(t) - 1;
348*7c478bd9Sstevel@tonic-gate 			t += 2;
349*7c478bd9Sstevel@tonic-gate 			tm.tm_mday = atoi_for2(t);
350*7c478bd9Sstevel@tonic-gate 			t += 2;
351*7c478bd9Sstevel@tonic-gate 			tm.tm_hour = atoi_for2(t);
352*7c478bd9Sstevel@tonic-gate 			t += 2;
353*7c478bd9Sstevel@tonic-gate 			tm.tm_min = atoi_for2(t);
354*7c478bd9Sstevel@tonic-gate 			break;
355*7c478bd9Sstevel@tonic-gate 		default:
356*7c478bd9Sstevel@tonic-gate 			touchabort(BADTIME);
357*7c478bd9Sstevel@tonic-gate 	}
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	if ((when = mktime(&tm)) == -1)
360*7c478bd9Sstevel@tonic-gate 		touchabort(BADTIME);
361*7c478bd9Sstevel@tonic-gate 	if (tm.tm_isdst)
362*7c478bd9Sstevel@tonic-gate 		when -= (timezone - altzone);
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 	ts->tv_sec = when;
365*7c478bd9Sstevel@tonic-gate 	ts->tv_nsec = 0;
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate static int
369*7c478bd9Sstevel@tonic-gate atoi_for2(char *p)
370*7c478bd9Sstevel@tonic-gate {
371*7c478bd9Sstevel@tonic-gate 	int value;
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 	value = (*p - '0') * 10 + *(p+1) - '0';
374*7c478bd9Sstevel@tonic-gate 	if ((value < 0) || (value > 99))
375*7c478bd9Sstevel@tonic-gate 		touchabort(BADTIME);
376*7c478bd9Sstevel@tonic-gate 	return (value);
377*7c478bd9Sstevel@tonic-gate }
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate static void
380*7c478bd9Sstevel@tonic-gate touchabort(const char *message)
381*7c478bd9Sstevel@tonic-gate {
382*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s\n", myname, gettext(message));
383*7c478bd9Sstevel@tonic-gate 	exit(1);
384*7c478bd9Sstevel@tonic-gate }
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate static void
387*7c478bd9Sstevel@tonic-gate usage(const int settime)
388*7c478bd9Sstevel@tonic-gate {
389*7c478bd9Sstevel@tonic-gate 	if (settime)
390*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
391*7c478bd9Sstevel@tonic-gate 		    "usage: %s [-f file] [mmddhhmm[yy]] file...\n"),
392*7c478bd9Sstevel@tonic-gate 		    myname);
393*7c478bd9Sstevel@tonic-gate 	else
394*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
395*7c478bd9Sstevel@tonic-gate 		    "usage: %s [-acm] [-r ref_file] file...\n"
396*7c478bd9Sstevel@tonic-gate 		    "       %s [-acm] [MMDDhhmm[yy]] file...\n"
397*7c478bd9Sstevel@tonic-gate 		    "       %s [-acm] [-t [[CC]YY]MMDDhhmm[.SS]] file...\n"),
398*7c478bd9Sstevel@tonic-gate 		    myname, myname, myname);
399*7c478bd9Sstevel@tonic-gate 	exit(2);
400*7c478bd9Sstevel@tonic-gate }
401*7c478bd9Sstevel@tonic-gate 
402*7c478bd9Sstevel@tonic-gate /*
403*7c478bd9Sstevel@tonic-gate  * nanoseconds are rounded off to microseconds by flooring.
404*7c478bd9Sstevel@tonic-gate  */
405*7c478bd9Sstevel@tonic-gate static void
406*7c478bd9Sstevel@tonic-gate timestruc_to_timeval(timestruc_t *ts, struct timeval *tv)
407*7c478bd9Sstevel@tonic-gate {
408*7c478bd9Sstevel@tonic-gate 	tv->tv_sec = ts->tv_sec;
409*7c478bd9Sstevel@tonic-gate 	tv->tv_usec = ts->tv_nsec / 1000;
410*7c478bd9Sstevel@tonic-gate }
411