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