]>
git.saurik.com Git - apple/file_cmds.git/blob - touch/touch.c
1 /* $NetBSD: touch.c,v 1.22 1998/08/25 20:59:42 ross Exp $ */
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)touch.c 8.2 (Berkeley) 4/28/95";
46 __RCSID("$NetBSD: touch.c,v 1.22 1998/08/25 20:59:42 ross Exp $");
49 #include <sys/types.h>
64 int main
__P((int, char **));
65 int rw
__P((char *, struct stat
*, int));
66 void stime_arg1
__P((char *, struct timeval
*));
67 void stime_arg2
__P((char *, int, struct timeval
*));
68 void stime_file
__P((char *, struct timeval
*));
69 void usage
__P((void));
78 int aflag
, cflag
, fflag
, hflag
, mflag
, ch
, fd
, len
, rval
, timeset
;
80 int (*change_file_times
) __P((const char *, const struct timeval
*));
81 int (*get_file_status
) __P((const char *, struct stat
*));
83 setlocale(LC_ALL
, "");
85 aflag
= cflag
= fflag
= hflag
= mflag
= timeset
= 0;
86 if (gettimeofday(&tv
[0], NULL
))
87 err(1, "gettimeofday");
89 while ((ch
= getopt(argc
, argv
, "acfhmr:t:")) != -1)
110 stime_file(optarg
, tv
);
114 stime_arg1(optarg
, tv
);
123 /* Default is both -a and -m. */
124 if (aflag
== 0 && mflag
== 0)
129 cflag
= 1; /* Don't create new file */
130 change_file_times
= lutimes
;
131 get_file_status
= lstat
;
134 change_file_times
= utimes
;
135 get_file_status
= stat
;
141 * If no -r or -t flag, at least two operands, the first of which
142 * is an 8 or 10 digit number, use the obsolete time specification.
144 if (!timeset
&& argc
> 1) {
145 (void)strtol(argv
[0], &p
, 10);
147 if (*p
== '\0' && (len
== 8 || len
== 10)) {
149 stime_arg2(*argv
++, len
== 10, tv
);
153 /* Otherwise use the current time of day. */
160 for (rval
= 0; *argv
; ++argv
) {
161 /* See if the file exists. */
162 if ((*get_file_status
)(*argv
, &sb
)) {
164 /* Create the file. */
166 O_WRONLY
| O_CREAT
, DEFFILEMODE
);
167 if (fd
== -1 || fstat(fd
, &sb
) || close(fd
)) {
173 /* If using the current time, we're done. */
180 TIMESPEC_TO_TIMEVAL(&tv
[0], &sb
.st_atimespec
);
182 TIMESPEC_TO_TIMEVAL(&tv
[1], &sb
.st_mtimespec
);
185 if (!(*change_file_times
)(*argv
, tv
))
188 /* If the user specified a time, nothing else we can do. */
195 * System V and POSIX 1003.1 require that a NULL argument
196 * set the access/modification times to the current time.
197 * The permission checks are different, too, in that the
198 * ability to write the file is sufficient. Take a shot.
200 if (!(*change_file_times
)(*argv
, NULL
))
203 /* Try reading/writing. */
204 if (!S_ISLNK(sb
.st_mode
) && rw(*argv
, &sb
, fflag
))
210 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
221 /* Start with the current time. */
222 tmptime
= tvp
[0].tv_sec
;
223 if ((t
= localtime(&tmptime
)) == NULL
)
225 /* [[CC]YY]MMDDhhmm[.SS] */
226 if ((p
= strchr(arg
, '.')) == NULL
)
227 t
->tm_sec
= 0; /* Seconds defaults to 0. */
229 if (strlen(p
+ 1) != 2)
232 t
->tm_sec
= ATOI2(p
);
236 switch (strlen(arg
)) {
237 case 12: /* CCYYMMDDhhmm */
238 t
->tm_year
= ATOI2(arg
) * 100 - TM_YEAR_BASE
;
241 case 10: /* YYMMDDhhmm */
243 t
->tm_year
+= ATOI2(arg
);
245 yearset
= ATOI2(arg
);
247 t
->tm_year
= yearset
+ 2000 - TM_YEAR_BASE
;
249 t
->tm_year
= yearset
+ 1900 - TM_YEAR_BASE
;
252 case 8: /* MMDDhhmm */
253 t
->tm_mon
= ATOI2(arg
);
254 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
257 t
->tm_mday
= ATOI2(arg
);
260 t
->tm_hour
= ATOI2(arg
);
263 t
->tm_min
= ATOI2(arg
);
269 t
->tm_isdst
= -1; /* Figure out DST. */
270 tvp
[0].tv_sec
= tvp
[1].tv_sec
= mktime(t
);
271 if (tvp
[0].tv_sec
== -1)
273 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
275 tvp
[0].tv_usec
= tvp
[1].tv_usec
= 0;
279 stime_arg2(arg
, year
, tvp
)
286 /* Start with the current time. */
287 tmptime
= tvp
[0].tv_sec
;
288 if ((t
= localtime(&tmptime
)) == NULL
)
291 t
->tm_mon
= ATOI2(arg
); /* MMDDhhmm[yy] */
292 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
293 t
->tm_mday
= ATOI2(arg
);
294 t
->tm_hour
= ATOI2(arg
);
295 t
->tm_min
= ATOI2(arg
);
299 t
->tm_year
= year
+ 2000 - TM_YEAR_BASE
;
301 t
->tm_year
= year
+ 1900 - TM_YEAR_BASE
;
304 t
->tm_isdst
= -1; /* Figure out DST. */
305 tvp
[0].tv_sec
= tvp
[1].tv_sec
= mktime(t
);
306 if (tvp
[0].tv_sec
== -1)
308 "out of range or illegal time specification: MMDDhhmm[yy]");
310 tvp
[0].tv_usec
= tvp
[1].tv_usec
= 0;
314 stime_file(fname
, tvp
)
320 if (stat(fname
, &sb
))
322 TIMESPEC_TO_TIMEVAL(&tvp
[0], &sb
.st_atimespec
);
323 TIMESPEC_TO_TIMEVAL(&tvp
[1], &sb
.st_mtimespec
);
327 rw(fname
, sbp
, force
)
332 int fd
, needed_chmod
, rval
;
335 /* Try regular files and directories. */
336 if (!S_ISREG(sbp
->st_mode
) && !S_ISDIR(sbp
->st_mode
)) {
337 warnx("%s: %s", fname
, strerror(EFTYPE
));
341 needed_chmod
= rval
= 0;
342 if ((fd
= open(fname
, O_RDWR
, 0)) == -1) {
343 if (!force
|| chmod(fname
, DEFFILEMODE
))
345 if ((fd
= open(fname
, O_RDWR
, 0)) == -1)
350 if (sbp
->st_size
!= 0) {
351 if (read(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
353 if (lseek(fd
, (off_t
)0, SEEK_SET
) == -1)
355 if (write(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
358 if (write(fd
, &byte
, sizeof(byte
)) != sizeof(byte
)) {
361 } else if (ftruncate(fd
, (off_t
)0)) {
363 warn("%s: file modified", fname
);
367 if (close(fd
) && rval
!= 1) {
371 if (needed_chmod
&& chmod(fname
, sbp
->st_mode
) && rval
!= 1) {
373 warn("%s: permissions modified", fname
);
382 (void)fprintf(stderr
,
383 "usage: touch [-acfhm] [-r file] [-t time] file ...\n");
385 (void)fprintf(stderr
,
386 "usage: touch [-acfm] [-r file] [-t time] file ...\n");