]>
git.saurik.com Git - apple/file_cmds.git/blob - touch/touch.c
5436285c4bd73cb4fac4682c5532338ccdf9e3e5
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
36 __RCSID("$FreeBSD: src/usr.bin/touch/touch.c,v 1.20 2002/09/04 23:29:07 dwmalone Exp $");
39 static const char copyright
[] =
40 "@(#) Copyright (c) 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid
[] = "@(#)touch.c 8.1 (Berkeley) 6/6/93";
48 #include <sys/types.h>
61 int rw(char *, struct stat
*, int);
62 void stime_arg1(char *, struct timeval
*);
63 void stime_arg2(char *, int, struct timeval
*);
64 void stime_file(char *, struct timeval
*);
68 main(int argc
, char *argv
[])
72 int (*stat_f
)(const char *, struct stat
*);
73 int (*utimes_f
)(const char *, const struct timeval
*);
74 int aflag
, cflag
, fflag
, mflag
, ch
, fd
, len
, rval
, timeset
;
77 aflag
= cflag
= fflag
= mflag
= timeset
= 0;
80 if (gettimeofday(&tv
[0], NULL
))
81 err(1, "gettimeofday");
83 while ((ch
= getopt(argc
, argv
, "acfhmr:t:")) != -1)
100 #endif /* __APPLE__ */
106 stime_file(optarg
, tv
);
110 stime_arg1(optarg
, tv
);
119 /* Default is both -a and -m. */
120 if (aflag
== 0 && mflag
== 0)
124 * If no -r or -t flag, at least two operands, the first of which
125 * is an 8 or 10 digit number, use the obsolete time specification.
127 if (!timeset
&& argc
> 1) {
128 (void)strtol(argv
[0], &p
, 10);
130 if (*p
== '\0' && (len
== 8 || len
== 10)) {
132 stime_arg2(*argv
++, len
== 10, tv
);
136 /* Otherwise use the current time of day. */
143 for (rval
= 0; *argv
; ++argv
) {
144 /* See if the file exists. */
145 if (stat_f(*argv
, &sb
) != 0) {
147 /* Create the file. */
149 O_WRONLY
| O_CREAT
, DEFFILEMODE
);
150 if (fd
== -1 || fstat(fd
, &sb
) || close(fd
)) {
156 /* If using the current time, we're done. */
164 TIMESPEC_TO_TIMEVAL(&tv
[0], &sb
.st_atimespec
);
166 TIMESPEC_TO_TIMEVAL(&tv
[1], &sb
.st_mtimespec
);
169 if (!utimes_f(*argv
, tv
))
172 /* If the user specified a time, nothing else we can do. */
179 * System V and POSIX 1003.1 require that a NULL argument
180 * set the access/modification times to the current time.
181 * The permission checks are different, too, in that the
182 * ability to write the file is sufficient. Take a shot.
184 if (!utimes_f(*argv
, NULL
))
187 /* Try reading/writing. */
188 if (!S_ISLNK(sb
.st_mode
) && !S_ISDIR(sb
.st_mode
) &&
189 rw(*argv
, &sb
, fflag
))
197 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
200 stime_arg1(char *arg
, struct timeval
*tvp
)
206 /* Start with the current time. */
208 if ((t
= localtime(&now
)) == NULL
)
210 /* [[CC]YY]MMDDhhmm[.SS] */
211 if ((p
= strchr(arg
, '.')) == NULL
)
212 t
->tm_sec
= 0; /* Seconds defaults to 0. */
214 if (strlen(p
+ 1) != 2)
217 t
->tm_sec
= ATOI2(p
);
221 switch(strlen(arg
)) {
222 case 12: /* CCYYMMDDhhmm */
223 t
->tm_year
= ATOI2(arg
);
227 case 10: /* YYMMDDhhmm */
229 yearset
= ATOI2(arg
);
230 t
->tm_year
+= yearset
;
232 yearset
= ATOI2(arg
);
234 t
->tm_year
= yearset
+ 2000;
236 t
->tm_year
= yearset
+ 1900;
238 t
->tm_year
-= 1900; /* Convert to UNIX time. */
240 case 8: /* MMDDhhmm */
241 t
->tm_mon
= ATOI2(arg
);
242 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
243 t
->tm_mday
= ATOI2(arg
);
244 t
->tm_hour
= ATOI2(arg
);
245 t
->tm_min
= ATOI2(arg
);
251 t
->tm_isdst
= -1; /* Figure out DST. */
252 tvp
[0].tv_sec
= tvp
[1].tv_sec
= mktime(t
);
253 if (tvp
[0].tv_sec
== -1)
255 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
257 tvp
[0].tv_usec
= tvp
[1].tv_usec
= 0;
261 stime_arg2(char *arg
, int year
, struct timeval
*tvp
)
265 /* Start with the current time. */
267 if ((t
= localtime(&now
)) == NULL
)
270 t
->tm_mon
= ATOI2(arg
); /* MMDDhhmm[yy] */
271 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
272 t
->tm_mday
= ATOI2(arg
);
273 t
->tm_hour
= ATOI2(arg
);
274 t
->tm_min
= ATOI2(arg
);
276 t
->tm_year
= ATOI2(arg
);
277 if (t
->tm_year
< 39) /* support 2000-2038 not 1902-1969 */
281 t
->tm_isdst
= -1; /* Figure out DST. */
282 tvp
[0].tv_sec
= tvp
[1].tv_sec
= mktime(t
);
283 if (tvp
[0].tv_sec
== -1)
285 "out of range or illegal time specification: MMDDhhmm[yy]");
287 tvp
[0].tv_usec
= tvp
[1].tv_usec
= 0;
291 stime_file(char *fname
, struct timeval
*tvp
)
295 if (stat(fname
, &sb
))
297 TIMESPEC_TO_TIMEVAL(tvp
, &sb
.st_atimespec
);
298 TIMESPEC_TO_TIMEVAL(tvp
+ 1, &sb
.st_mtimespec
);
302 rw(char *fname
, struct stat
*sbp
, int force
)
304 int fd
, needed_chmod
, rval
;
307 /* Try regular files. */
308 if (!S_ISREG(sbp
->st_mode
)) {
309 warnx("%s: %s", fname
, strerror(EFTYPE
));
313 needed_chmod
= rval
= 0;
314 if ((fd
= open(fname
, O_RDWR
, 0)) == -1) {
315 if (!force
|| chmod(fname
, DEFFILEMODE
))
317 if ((fd
= open(fname
, O_RDWR
, 0)) == -1)
322 if (sbp
->st_size
!= 0) {
323 if (read(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
325 if (lseek(fd
, (off_t
)0, SEEK_SET
) == -1)
327 if (write(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
330 if (write(fd
, &byte
, sizeof(byte
)) != sizeof(byte
)) {
333 } else if (ftruncate(fd
, (off_t
)0)) {
335 warn("%s: file modified", fname
);
339 if (close(fd
) && rval
!= 1) {
343 if (needed_chmod
&& chmod(fname
, sbp
->st_mode
) && rval
!= 1) {
345 warn("%s: permissions modified", fname
);
353 (void)fprintf(stderr
, "usage: touch [-acfhm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n");