xref: /netbsd-src/usr.bin/touch/touch.c (revision 4b30c543a0b21e3ba94f2c569e9a82b4fdb2075f)
1 /*
2  * Copyright (c) 1993 Regents of the University of California.
3  * 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
35 char copyright[] =
36 "@(#) Copyright (c) 1993 Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)touch.c	5.5 (Berkeley) 3/7/93";*/
42 static char rcsid[] = "$Id: touch.c,v 1.7 1993/09/16 21:44:35 cgd 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 <time.h>
56 #include <unistd.h>
57 
58 int	rw __P((char *, struct stat *, int));
59 void	stime_arg1 __P((char *, struct timeval *));
60 void	stime_arg2 __P((char *, int, struct timeval *));
61 void	stime_file __P((char *, struct timeval *));
62 void	usage __P((void));
63 
64 #ifndef notyet
65 #define TIMET_TO_TIMEVAL(timevalp, tp) (timevalp)->tv_sec = *(tp);
66 #endif
67 
68 int
69 main(argc, argv)
70 	int argc;
71 	char *argv[];
72 {
73 	struct stat sb;
74 	struct timeval tv[2];
75 	int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
76 	char *p;
77 
78 	aflag = cflag = fflag = mflag = timeset = 0;
79 	if (gettimeofday(&tv[0], NULL))
80 		err(1, "gettimeofday");
81 
82 	while ((ch = getopt(argc, argv, "acfmr:t:")) != EOF)
83 		switch(ch) {
84 		case 'a':
85 			aflag = 1;
86 			break;
87 		case 'c':
88 			cflag = 1;
89 			break;
90 		case 'f':
91 			fflag = 1;
92 			break;
93 		case 'm':
94 			mflag = 1;
95 			break;
96 		case 'r':
97 			timeset = 1;
98 			stime_file(optarg, tv);
99 			break;
100 		case 't':
101 			timeset = 1;
102 			stime_arg1(optarg, tv);
103 			break;
104 		case '?':
105 		default:
106 			usage();
107 		}
108 	argc -= optind;
109 	argv += optind;
110 
111 	/* Default is both -a and -m. */
112 	if (aflag == 0 && mflag == 0)
113 		aflag = mflag = 1;
114 
115 	/*
116 	 * If no -r or -t flag, at least two operands, the first of which
117 	 * is an 8 or 10 digit number, use the obsolete time specification.
118 	 */
119 	if (!timeset && argc > 1) {
120 		(void)strtol(argv[0], &p, 10);
121 		len = p - argv[0];
122 		if (*p == '\0' && (len == 8 || len == 10)) {
123 			timeset = 1;
124 			stime_arg2(argv[0], len == 10, tv);
125 			argv++;
126 		}
127 	}
128 
129 	/* Otherwise use the current time of day. */
130 	if (!timeset)
131 		tv[1] = tv[0];
132 
133 	if (*argv == NULL)
134 		usage();
135 
136 	for (rval = 0; *argv; ++argv) {
137 		/* See if the file exists. */
138 		if (stat(*argv, &sb))
139 			if (!cflag) {
140 				/* Create the file. */
141 				fd = open(*argv,
142 				    O_WRONLY | O_CREAT, DEFFILEMODE);
143 				if (fd == -1 || fstat(fd, &sb) || close(fd)) {
144 					rval = 1;
145 					warn("%s", *argv);
146 					continue;
147 				}
148 
149 				/* If using the current time, we're done. */
150 				if (!timeset)
151 					continue;
152 			} else
153 				continue;
154 
155 		if (!aflag)
156 #ifdef notyet
157 			TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
158 #else
159 			TIMET_TO_TIMEVAL(&tv[0], &sb.st_atime);
160 #endif
161 		if (!mflag)
162 #ifdef notyet
163 			TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
164 #else
165 			TIMET_TO_TIMEVAL(&tv[1], &sb.st_mtime);
166 #endif
167 
168 
169 		/* Try utimes(2). */
170 		if (!utimes(*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 (!utimes(*argv, NULL))
186 			continue;
187 
188 		/* Try reading/writing. */
189 		if (rw(*argv, &sb, fflag))
190 			rval = 1;
191 	}
192 	exit(rval);
193 }
194 
195 #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
196 
197 void
198 stime_arg1(arg, tvp)
199 	char *arg;
200 	struct timeval *tvp;
201 {
202 	struct tm *t;
203 	int yearset;
204 	char *p;
205 					/* Start with the current time. */
206 	if ((t = localtime(&tvp[0].tv_sec)) == NULL)
207 		err(1, "localtime");
208 					/* [[CC]YY]MMDDhhmm[.SS] */
209 	if ((p = strchr(arg, '.')) == NULL)
210 		t->tm_sec = 0;		/* Seconds defaults to 0. */
211 	else {
212 		if (strlen(p + 1) != 2)
213 			goto terr;
214 		*p++ = '\0';
215 		t->tm_sec = ATOI2(p);
216 	}
217 
218 	yearset = 0;
219 	switch(strlen(arg)) {
220 	case 12:			/* CCYYMMDDhhmm */
221 		t->tm_year = ATOI2(arg);
222 		t->tm_year *= 100;
223 		yearset = 1;
224 		/* FALLTHOUGH */
225 	case 10:			/* YYMMDDhhmm */
226 		if (yearset) {
227 			yearset = ATOI2(arg);
228 			t->tm_year += yearset;
229 		} else {
230 			yearset = ATOI2(arg);
231 			if (yearset < 69)
232 				t->tm_year = yearset + 2000;
233 			else
234 				t->tm_year = yearset + 1900;
235 		}
236 		t->tm_year -= 1900;	/* Convert to UNIX time. */
237 		/* FALLTHROUGH */
238 	case 8:				/* MMDDhhmm */
239 		t->tm_mon = ATOI2(arg);
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 		break;
245 	default:
246 		goto terr;
247 	}
248 
249 	t->tm_isdst = -1;		/* Figure out DST. */
250 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
251 	if (tvp[0].tv_sec == -1)
252 terr:		errx(1,
253 	"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
254 
255 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
256 }
257 
258 void
259 stime_arg2(arg, year, tvp)
260 	char *arg;
261 	int year;
262 	struct timeval *tvp;
263 {
264 	struct tm *t;
265 					/* Start with the current time. */
266 	if ((t = localtime(&tvp[0].tv_sec)) == NULL)
267 		err(1, "localtime");
268 
269 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
270 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
271 	t->tm_mday = ATOI2(arg);
272 	t->tm_hour = ATOI2(arg);
273 	t->tm_min = ATOI2(arg);
274 	if (year)
275 		t->tm_year = ATOI2(arg);
276 
277 	t->tm_isdst = -1;		/* Figure out DST. */
278 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
279 	if (tvp[0].tv_sec == -1)
280 		errx(1,
281 	"out of range or illegal time specification: MMDDhhmm[yy]");
282 
283 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
284 }
285 
286 void
287 stime_file(fname, tvp)
288 	char *fname;
289 	struct timeval *tvp;
290 {
291 	struct stat sb;
292 
293 	if (stat(fname, &sb))
294 		err(1, "%s", fname);
295 #ifdef notyet
296 	TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
297 #else
298 	TIMET_TO_TIMEVAL(tvp, &sb.st_atime);
299 #endif
300 #ifdef notyet
301 	TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
302 #else
303 	TIMET_TO_TIMEVAL(tvp + 1, &sb.st_mtime);
304 #endif
305 }
306 
307 int
308 rw(fname, sbp, force)
309 	char *fname;
310 	struct stat *sbp;
311 	int force;
312 {
313 	int fd, needed_chmod, rval;
314 	u_char byte;
315 
316 	/* Try regular files and directories. */
317 	if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
318 		warnx("%s: %s", fname, strerror(EFTYPE));
319 		return (1);
320 	}
321 
322 	needed_chmod = rval = 0;
323 	if ((fd = open(fname, O_RDWR, 0)) == -1) {
324 		if (!force || chmod(fname, DEFFILEMODE))
325 			goto err;
326 		if ((fd = open(fname, O_RDWR, 0)) == -1)
327 			goto err;
328 		needed_chmod = 1;
329 	}
330 
331 	if (sbp->st_size != 0) {
332 		if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
333 			goto err;
334 		if (lseek(fd, (off_t)0, SEEK_SET) == -1)
335 			goto err;
336 		if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
337 			goto err;
338 	} else {
339 		if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
340 err:			rval = 1;
341 			warn("%s", fname);
342 		} else if (ftruncate(fd, (off_t)0)) {
343 			rval = 1;
344 			warn("%s: file modified", fname);
345 		}
346 	}
347 
348 	if (close(fd) && rval != 1) {
349 		rval = 1;
350 		warn("%s", fname);
351 	}
352 	if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
353 		rval = 1;
354 		warn("%s: permissions modified", fname);
355 	}
356 	return (rval);
357 }
358 
359 volatile void
360 usage()
361 {
362 	(void)fprintf(stderr,
363 	    "usage: touch [-acfm] [-r file] [-t time] file ...\n");
364 	exit(1);
365 }
366