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