xref: /original-bsd/usr.bin/touch/touch.c (revision 0842ddeb)
1 /*
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)touch.c	8.2 (Berkeley) 04/28/95";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 
22 #include <err.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <unistd.h>
30 
31 int	rw __P((char *, struct stat *, int));
32 void	stime_arg1 __P((char *, struct timeval *));
33 void	stime_arg2 __P((char *, int, struct timeval *));
34 void	stime_file __P((char *, struct timeval *));
35 void	usage __P((void));
36 
37 int
38 main(argc, argv)
39 	int argc;
40 	char *argv[];
41 {
42 	struct stat sb;
43 	struct timeval tv[2];
44 	int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
45 	char *p;
46 
47 	aflag = cflag = fflag = mflag = timeset = 0;
48 	if (gettimeofday(&tv[0], NULL))
49 		err(1, "gettimeofday");
50 
51 	while ((ch = getopt(argc, argv, "acfmr:t:")) != EOF)
52 		switch(ch) {
53 		case 'a':
54 			aflag = 1;
55 			break;
56 		case 'c':
57 			cflag = 1;
58 			break;
59 		case 'f':
60 			fflag = 1;
61 			break;
62 		case 'm':
63 			mflag = 1;
64 			break;
65 		case 'r':
66 			timeset = 1;
67 			stime_file(optarg, tv);
68 			break;
69 		case 't':
70 			timeset = 1;
71 			stime_arg1(optarg, tv);
72 			break;
73 		case '?':
74 		default:
75 			usage();
76 		}
77 	argc -= optind;
78 	argv += optind;
79 
80 	/* Default is both -a and -m. */
81 	if (aflag == 0 && mflag == 0)
82 		aflag = mflag = 1;
83 
84 	/*
85 	 * If no -r or -t flag, at least two operands, the first of which
86 	 * is an 8 or 10 digit number, use the obsolete time specification.
87 	 */
88 	if (!timeset && argc > 1) {
89 		(void)strtol(argv[0], &p, 10);
90 		len = p - argv[0];
91 		if (*p == '\0' && (len == 8 || len == 10)) {
92 			timeset = 1;
93 			stime_arg2(*argv++, len == 10, tv);
94 		}
95 	}
96 
97 	/* Otherwise use the current time of day. */
98 	if (!timeset)
99 		tv[1] = tv[0];
100 
101 	if (*argv == NULL)
102 		usage();
103 
104 	for (rval = 0; *argv; ++argv) {
105 		/* See if the file exists. */
106 		if (stat(*argv, &sb))
107 			if (!cflag) {
108 				/* Create the file. */
109 				fd = open(*argv,
110 				    O_WRONLY | O_CREAT, DEFFILEMODE);
111 				if (fd == -1 || fstat(fd, &sb) || close(fd)) {
112 					rval = 1;
113 					warn("%s", *argv);
114 					continue;
115 				}
116 
117 				/* If using the current time, we're done. */
118 				if (!timeset)
119 					continue;
120 			} else
121 				continue;
122 
123 		if (!aflag)
124 			TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
125 		if (!mflag)
126 			TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
127 
128 		/* Try utimes(2). */
129 		if (!utimes(*argv, tv))
130 			continue;
131 
132 		/* If the user specified a time, nothing else we can do. */
133 		if (timeset) {
134 			rval = 1;
135 			warn("%s", *argv);
136 		}
137 
138 		/*
139 		 * System V and POSIX 1003.1 require that a NULL argument
140 		 * set the access/modification times to the current time.
141 		 * The permission checks are different, too, in that the
142 		 * ability to write the file is sufficient.  Take a shot.
143 		 */
144 		 if (!utimes(*argv, NULL))
145 			continue;
146 
147 		/* Try reading/writing. */
148 		if (rw(*argv, &sb, fflag))
149 			rval = 1;
150 	}
151 	exit(rval);
152 }
153 
154 #define	ATOI2(ar)	((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
155 
156 void
157 stime_arg1(arg, tvp)
158 	char *arg;
159 	struct timeval *tvp;
160 {
161 	struct tm *t;
162 	int yearset;
163 	char *p;
164 					/* Start with the current time. */
165 	if ((t = localtime(&tvp[0].tv_sec)) == NULL)
166 		err(1, "localtime");
167 					/* [[CC]YY]MMDDhhmm[.SS] */
168 	if ((p = strchr(arg, '.')) == NULL)
169 		t->tm_sec = 0;		/* Seconds defaults to 0. */
170 	else {
171 		if (strlen(p + 1) != 2)
172 			goto terr;
173 		*p++ = '\0';
174 		t->tm_sec = ATOI2(p);
175 	}
176 
177 	yearset = 0;
178 	switch(strlen(arg)) {
179 	case 12:			/* CCYYMMDDhhmm */
180 		t->tm_year = ATOI2(arg);
181 		t->tm_year *= 100;
182 		yearset = 1;
183 		/* FALLTHOUGH */
184 	case 10:			/* YYMMDDhhmm */
185 		if (yearset) {
186 			yearset = ATOI2(arg);
187 			t->tm_year += yearset;
188 		} else {
189 			yearset = ATOI2(arg);
190 			if (yearset < 69)
191 				t->tm_year = yearset + 2000;
192 			else
193 				t->tm_year = yearset + 1900;
194 		}
195 		t->tm_year -= 1900;	/* Convert to UNIX time. */
196 		/* FALLTHROUGH */
197 	case 8:				/* MMDDhhmm */
198 		t->tm_mon = ATOI2(arg);
199 		--t->tm_mon;		/* Convert from 01-12 to 00-11 */
200 		t->tm_mday = ATOI2(arg);
201 		t->tm_hour = ATOI2(arg);
202 		t->tm_min = ATOI2(arg);
203 		break;
204 	default:
205 		goto terr;
206 	}
207 
208 	t->tm_isdst = -1;		/* Figure out DST. */
209 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
210 	if (tvp[0].tv_sec == -1)
211 terr:		errx(1,
212 	"out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
213 
214 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
215 }
216 
217 void
218 stime_arg2(arg, year, tvp)
219 	char *arg;
220 	int year;
221 	struct timeval *tvp;
222 {
223 	struct tm *t;
224 					/* Start with the current time. */
225 	if ((t = localtime(&tvp[0].tv_sec)) == NULL)
226 		err(1, "localtime");
227 
228 	t->tm_mon = ATOI2(arg);		/* MMDDhhmm[yy] */
229 	--t->tm_mon;			/* Convert from 01-12 to 00-11 */
230 	t->tm_mday = ATOI2(arg);
231 	t->tm_hour = ATOI2(arg);
232 	t->tm_min = ATOI2(arg);
233 	if (year)
234 		t->tm_year = ATOI2(arg);
235 
236 	t->tm_isdst = -1;		/* Figure out DST. */
237 	tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
238 	if (tvp[0].tv_sec == -1)
239 		errx(1,
240 	"out of range or illegal time specification: MMDDhhmm[yy]");
241 
242 	tvp[0].tv_usec = tvp[1].tv_usec = 0;
243 }
244 
245 void
246 stime_file(fname, tvp)
247 	char *fname;
248 	struct timeval *tvp;
249 {
250 	struct stat sb;
251 
252 	if (stat(fname, &sb))
253 		err(1, "%s", fname);
254 	TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
255 	TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
256 }
257 
258 int
259 rw(fname, sbp, force)
260 	char *fname;
261 	struct stat *sbp;
262 	int force;
263 {
264 	int fd, needed_chmod, rval;
265 	u_char byte;
266 
267 	/* Try regular files and directories. */
268 	if (!S_ISREG(sbp->st_mode) && !S_ISDIR(sbp->st_mode)) {
269 		warnx("%s: %s", fname, strerror(EFTYPE));
270 		return (1);
271 	}
272 
273 	needed_chmod = rval = 0;
274 	if ((fd = open(fname, O_RDWR, 0)) == -1) {
275 		if (!force || chmod(fname, DEFFILEMODE))
276 			goto err;
277 		if ((fd = open(fname, O_RDWR, 0)) == -1)
278 			goto err;
279 		needed_chmod = 1;
280 	}
281 
282 	if (sbp->st_size != 0) {
283 		if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
284 			goto err;
285 		if (lseek(fd, (off_t)0, SEEK_SET) == -1)
286 			goto err;
287 		if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
288 			goto err;
289 	} else {
290 		if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
291 err:			rval = 1;
292 			warn("%s", fname);
293 		} else if (ftruncate(fd, (off_t)0)) {
294 			rval = 1;
295 			warn("%s: file modified", fname);
296 		}
297 	}
298 
299 	if (close(fd) && rval != 1) {
300 		rval = 1;
301 		warn("%s", fname);
302 	}
303 	if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
304 		rval = 1;
305 		warn("%s: permissions modified", fname);
306 	}
307 	return (rval);
308 }
309 
310 __dead void
311 usage()
312 {
313 	(void)fprintf(stderr,
314 	    "usage: touch [-acfm] [-r file] [-t time] file ...\n");
315 	exit(1);
316 }
317