touch.c revision 48566
1/*
2 * Copyright (c) 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)touch.c	8.1 (Berkeley) 6/6/93";
42#endif /* not lint */
43
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <sys/time.h>
47
48#include <err.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <time.h>
55#include <unistd.h>
56
57int	rw __P((char *, struct stat *, int));
58void	stime_arg1 __P((char *, struct timeval *));
59void	stime_arg2 __P((char *, int, struct timeval *));
60void	stime_file __P((char *, struct timeval *));
61void	usage __P((void));
62
63int
64main(argc, argv)
65	int argc;
66	char *argv[];
67{
68	struct stat sb;
69	struct timeval tv[2];
70	int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
71	char *p;
72
73	aflag = cflag = fflag = mflag = timeset = 0;
74	if (gettimeofday(&tv[0], NULL))
75		err(1, "gettimeofday");
76
77	while ((ch = getopt(argc, argv, "acfmr:t:")) != -1)
78		switch(ch) {
79		case 'a':
80			aflag = 1;
81			break;
82		case 'c':
83			cflag = 1;
84			break;
85		case 'f':
86			fflag = 1;
87			break;
88		case 'm':
89			mflag = 1;
90			break;
91		case 'r':
92			timeset = 1;
93			stime_file(optarg, tv);
94			break;
95		case 't':
96			timeset = 1;
97			stime_arg1(optarg, tv);
98			break;
99		case '?':
100		default:
101			usage();
102		}
103	argc -= optind;
104	argv += optind;
105
106	/* Default is both -a and -m. */
107	if (aflag == 0 && mflag == 0)
108		aflag = mflag = 1;
109
110	/*
111	 * If no -r or -t flag, at least two operands, the first of which
112	 * is an 8 or 10 digit number, use the obsolete time specification.
113	 */
114	if (!timeset && argc > 1) {
115		(void)strtol(argv[0], &p, 10);
116		len = p - argv[0];
117		if (*p == '\0' && (len == 8 || len == 10)) {
118			timeset = 1;
119			stime_arg2(*argv++, len == 10, tv);
120		}
121	}
122
123	/* Otherwise use the current time of day. */
124	if (!timeset)
125		tv[1] = tv[0];
126
127	if (*argv == NULL)
128		usage();
129
130	for (rval = 0; *argv; ++argv) {
131		/* See if the file exists. */
132		if (stat(*argv, &sb)) {
133			if (!cflag) {
134				/* Create the file. */
135				fd = open(*argv,
136				    O_WRONLY | O_CREAT, DEFFILEMODE);
137				if (fd == -1 || fstat(fd, &sb) || close(fd)) {
138					rval = 1;
139					warn("%s", *argv);
140					continue;
141				}
142
143				/* If using the current time, we're done. */
144				if (!timeset)
145					continue;
146			} else
147				continue;
148		}
149
150		if (!aflag)
151			TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
152		if (!mflag)
153			TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
154
155		/* Try utimes(2). */
156		if (!utimes(*argv, tv))
157			continue;
158
159		/* If the user specified a time, nothing else we can do. */
160		if (timeset) {
161			rval = 1;
162			warn("%s", *argv);
163		}
164
165		/*
166		 * System V and POSIX 1003.1 require that a NULL argument
167		 * set the access/modification times to the current time.
168		 * The permission checks are different, too, in that the
169		 * ability to write the file is sufficient.  Take a shot.
170		 */
171		 if (!utimes(*argv, NULL))
172			continue;
173
174		/* Try reading/writing. */
175		if (rw(*argv, &sb, fflag))
176			rval = 1;
177	}
178	exit(rval);
179}
180
181#define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
182
183void
184stime_arg1(arg, tvp)
185	char *arg;
186	struct timeval *tvp;
187{
188	time_t now;
189	struct tm *t;
190	int yearset;
191	char *p;
192					/* Start with the current time. */
193	now = tvp[0].tv_sec;
194	if ((t = localtime(&now)) == NULL)
195		err(1, "localtime");
196					/* [[CC]YY]MMDDhhmm[.SS] */
197	if ((p = strchr(arg, '.')) == NULL)
198		t->tm_sec = 0;		/* Seconds defaults to 0. */
199	else {
200		if (strlen(p + 1) != 2)
201			goto terr;
202		*p++ = '\0';
203		t->tm_sec = ATOI2(p);
204	}
205
206	yearset = 0;
207	switch(strlen(arg)) {
208	case 12:			/* CCYYMMDDhhmm */
209		t->tm_year = ATOI2(arg);
210		t->tm_year *= 100;
211		yearset = 1;
212		/* FALLTHOUGH */
213	case 10:			/* YYMMDDhhmm */
214		if (yearset) {
215			yearset = ATOI2(arg);
216			t->tm_year += yearset;
217		} else {
218			yearset = ATOI2(arg);
219			if (yearset < 69)
220				t->tm_year = yearset + 2000;
221			else
222				t->tm_year = yearset + 1900;
223		}
224		t->tm_year -= 1900;	/* Convert to UNIX time. */
225		/* FALLTHROUGH */
226	case 8:				/* MMDDhhmm */
227		t->tm_mon = ATOI2(arg);
228		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
229		t->tm_mday = ATOI2(arg);
230		t->tm_hour = ATOI2(arg);
231		t->tm_min = ATOI2(arg);
232		break;
233	default:
234		goto terr;
235	}
236
237	t->tm_isdst = -1;		/* Figure out DST. */
238	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
239	if (tvp[0].tv_sec == -1)
240terr:		errx(1,
241	"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
242
243	tvp[0].tv_usec = tvp[1].tv_usec = 0;
244}
245
246void
247stime_arg2(arg, year, tvp)
248	char *arg;
249	int year;
250	struct timeval *tvp;
251{
252	time_t now;
253	struct tm *t;
254					/* Start with the current time. */
255	now = tvp[0].tv_sec;
256	if ((t = localtime(&now)) == NULL)
257		err(1, "localtime");
258
259	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
260	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
261	t->tm_mday = ATOI2(arg);
262	t->tm_hour = ATOI2(arg);
263	t->tm_min = ATOI2(arg);
264	if (year) {
265		t->tm_year = ATOI2(arg);
266		if (t->tm_year < 39)	/* support 2000-2038 not 1902-1969 */
267			t->tm_year += 100;
268	}
269
270	t->tm_isdst = -1;		/* Figure out DST. */
271	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
272	if (tvp[0].tv_sec == -1)
273		errx(1,
274	"out of range or illegal time specification: MMDDhhmm[yy]");
275
276	tvp[0].tv_usec = tvp[1].tv_usec = 0;
277}
278
279void
280stime_file(fname, tvp)
281	char *fname;
282	struct timeval *tvp;
283{
284	struct stat sb;
285
286	if (stat(fname, &sb))
287		err(1, "%s", fname);
288	TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
289	TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
290}
291
292int
293rw(fname, sbp, force)
294	char *fname;
295	struct stat *sbp;
296	int force;
297{
298	int fd, needed_chmod, rval;
299	u_char byte;
300
301	/* Try regular files and directories. */
302	if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
303		warnx("%s: %s", fname, strerror(EFTYPE));
304		return (1);
305	}
306
307	needed_chmod = rval = 0;
308	if ((fd = open(fname, O_RDWR, 0)) == -1) {
309		if (!force || chmod(fname, DEFFILEMODE))
310			goto err;
311		if ((fd = open(fname, O_RDWR, 0)) == -1)
312			goto err;
313		needed_chmod = 1;
314	}
315
316	if (sbp->st_size != 0) {
317		if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
318			goto err;
319		if (lseek(fd, (off_t)0, SEEK_SET) == -1)
320			goto err;
321		if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
322			goto err;
323	} else {
324		if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
325err:			rval = 1;
326			warn("%s", fname);
327		} else if (ftruncate(fd, (off_t)0)) {
328			rval = 1;
329			warn("%s: file modified", fname);
330		}
331	}
332
333	if (close(fd) && rval != 1) {
334		rval = 1;
335		warn("%s", fname);
336	}
337	if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
338		rval = 1;
339		warn("%s: permissions modified", fname);
340	}
341	return (rval);
342}
343
344void
345usage()
346{
347	(void)fprintf(stderr, "usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n");
348	exit(1);
349}
350