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