touch.c revision 1.14
1/*	$OpenBSD: touch.c,v 1.14 2006/03/08 12:20:05 henning 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/types.h>
34#include <sys/stat.h>
35#include <sys/time.h>
36
37#include <err.h>
38#include <errno.h>
39#include <fcntl.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <locale.h>
44#include <time.h>
45#include <tzfile.h>
46#include <unistd.h>
47
48void		stime_arg1(char *, struct timeval *);
49void		stime_arg2(char *, int, struct timeval *);
50void		stime_file(char *, struct timeval *);
51__dead void	usage(void);
52
53int
54main(int argc, char *argv[])
55{
56	struct stat	 sb;
57	struct timeval	 tv[2];
58	int		 aflag, cflag, mflag, ch, fd, len, rval, timeset;
59	char		*p;
60
61	(void)setlocale(LC_ALL, "");
62
63	aflag = cflag = mflag = timeset = 0;
64	if (gettimeofday(&tv[0], NULL))
65		err(1, "gettimeofday");
66
67	while ((ch = getopt(argc, argv, "acfmr:t:")) != -1)
68		switch (ch) {
69		case 'a':
70			aflag = 1;
71			break;
72		case 'c':
73			cflag = 1;
74			break;
75		case 'f':
76			break;
77		case 'm':
78			mflag = 1;
79			break;
80		case 'r':
81			timeset = 1;
82			stime_file(optarg, tv);
83			break;
84		case 't':
85			timeset = 1;
86			stime_arg1(optarg, tv);
87			break;
88		default:
89			usage();
90		}
91	argc -= optind;
92	argv += optind;
93
94	/* Default is both -a and -m. */
95	if (aflag == 0 && mflag == 0)
96		aflag = mflag = 1;
97
98	/*
99	 * If no -r or -t flag, at least two operands, the first of which
100	 * is an 8 or 10 digit number, use the obsolete time specification.
101	 */
102	if (!timeset && argc > 1) {
103		(void)strtol(argv[0], &p, 10);
104		len = p - argv[0];
105		if (*p == '\0' && (len == 8 || len == 10)) {
106			timeset = 1;
107			stime_arg2(*argv++, len == 10, tv);
108		}
109	}
110
111	/* Otherwise use the current time of day. */
112	if (!timeset)
113		tv[1] = tv[0];
114
115	if (*argv == NULL)
116		usage();
117
118	for (rval = 0; *argv; ++argv) {
119		/* See if the file exists. */
120		if (stat(*argv, &sb)) {
121			if (!cflag) {
122				/* Create the file. */
123				fd = open(*argv,
124				    O_WRONLY | O_CREAT, DEFFILEMODE);
125				if (fd == -1 || fstat(fd, &sb) || close(fd)) {
126					rval = 1;
127					warn("%s", *argv);
128					continue;
129				}
130
131				/* If using the current time, we're done. */
132				if (!timeset)
133					continue;
134			} else
135				continue;
136		}
137
138		if (!aflag)
139			TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
140		if (!mflag)
141			TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
142
143		/* Try utimes(2). */
144		if (!utimes(*argv, tv))
145			continue;
146
147		/* If the user specified a time, nothing else we can do. */
148		if (timeset) {
149			rval = 1;
150			warn("%s", *argv);
151		}
152
153		/*
154		 * System V and POSIX 1003.1 require that a NULL argument
155		 * set the access/modification times to the current time.
156		 * The permission checks are different, too, in that the
157		 * ability to write the file is sufficient.  Take a shot.
158		 */
159		 if (!utimes(*argv, NULL))
160			continue;
161
162		rval = 1;
163		warn("%s", *argv);
164	}
165	exit(rval);
166}
167
168#define	ATOI2(s)	((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
169
170void
171stime_arg1(char *arg, struct timeval *tvp)
172{
173	struct tm	*t;
174	time_t		 tmptime;
175	int		 yearset;
176	char		*p;
177					/* Start with the current time. */
178	tmptime = tvp[0].tv_sec;
179	if ((t = localtime(&tmptime)) == NULL)
180		err(1, "localtime");
181					/* [[CC]YY]MMDDhhmm[.SS] */
182	if ((p = strchr(arg, '.')) == NULL)
183		t->tm_sec = 0;		/* Seconds defaults to 0. */
184	else {
185		if (strlen(p + 1) != 2)
186			goto terr;
187		*p++ = '\0';
188		t->tm_sec = ATOI2(p);
189	}
190
191	yearset = 0;
192	switch (strlen(arg)) {
193	case 12:			/* CCYYMMDDhhmm */
194		t->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE;
195		yearset = 1;
196		/* FALLTHROUGH */
197	case 10:			/* YYMMDDhhmm */
198		if (yearset) {
199			yearset = ATOI2(arg);
200			t->tm_year += yearset;
201		} else {
202			yearset = ATOI2(arg);
203			if (yearset < 69)
204				t->tm_year = yearset + 2000 - TM_YEAR_BASE;
205			else
206				t->tm_year = yearset + 1900 - TM_YEAR_BASE;
207		}
208		/* FALLTHROUGH */
209	case 8:				/* MMDDhhmm */
210		t->tm_mon = ATOI2(arg);
211		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
212		t->tm_mday = ATOI2(arg);
213		t->tm_hour = ATOI2(arg);
214		t->tm_min = ATOI2(arg);
215		break;
216	default:
217		goto terr;
218	}
219
220	t->tm_isdst = -1;		/* Figure out DST. */
221	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
222	if (tvp[0].tv_sec == -1)
223terr:		errx(1,
224	"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
225
226	tvp[0].tv_usec = tvp[1].tv_usec = 0;
227}
228
229void
230stime_arg2(char *arg, int year, struct timeval *tvp)
231{
232	struct tm	*t;
233	time_t		 tmptime;
234					/* Start with the current time. */
235	tmptime = tvp[0].tv_sec;
236	if ((t = localtime(&tmptime)) == NULL)
237		err(1, "localtime");
238
239	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[YY] */
240	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
241	t->tm_mday = ATOI2(arg);
242	t->tm_hour = ATOI2(arg);
243	t->tm_min = ATOI2(arg);
244	if (year) {
245		year = ATOI2(arg);
246		if (year < 69)
247			t->tm_year = year + 2000 - TM_YEAR_BASE;
248		else
249			t->tm_year = year + 1900 - TM_YEAR_BASE;
250	}
251	t->tm_sec = 0;
252
253	t->tm_isdst = -1;		/* Figure out DST. */
254	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
255	if (tvp[0].tv_sec == -1)
256		errx(1,
257	"out of range or illegal time specification: MMDDhhmm[YY]");
258
259	tvp[0].tv_usec = tvp[1].tv_usec = 0;
260}
261
262void
263stime_file(char *fname, struct timeval *tvp)
264{
265	struct stat	sb;
266
267	if (stat(fname, &sb))
268		err(1, "%s", fname);
269	TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
270	TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
271}
272
273__dead void
274usage(void)
275{
276	extern char	*__progname;
277
278	(void)fprintf(stderr,
279	    "usage: %s [-acm] [-r file] [-t time] file ...\n", __progname);
280	exit(1);
281}
282