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
33 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved.
34 * @(#)touch.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/touch/touch.c,v 1.11.2.2 2002/07/28 06:52:15 eric Exp $
36 * $DragonFly: src/usr.bin/touch/touch.c,v 1.8 2004/09/14 00:13:36 drhodus Exp $
39 #include <sys/types.h>
52 static int rw(const char *, const struct stat
*, int);
53 static void stime_arg1(const char *, struct timeval
*);
54 static void stime_arg2(const char *, int, struct timeval
*);
55 static void stime_file(const char *, struct timeval
*);
56 static void usage(void);
59 main(int argc
, char **argv
)
63 int aflag
, cflag
, fflag
, mflag
, ch
, fd
, len
, rval
, timeset
;
66 aflag
= cflag
= fflag
= mflag
= timeset
= 0;
67 if (gettimeofday(&tv
[0], NULL
) == -1)
68 err(1, "gettimeofday");
70 while ((ch
= getopt(argc
, argv
, "acfmr:t:")) != -1)
86 stime_file(optarg
, tv
);
90 stime_arg1(optarg
, tv
);
98 /* Default is both -a and -m. */
99 if (aflag
== 0 && mflag
== 0)
103 * If no -r or -t flag, at least two operands, the first of which
104 * is an 8 or 10 digit number, use the obsolete time specification.
106 if (!timeset
&& argc
> 1) {
107 strtol(argv
[0], &p
, 10);
109 if (*p
== '\0' && (len
== 8 || len
== 10)) {
111 stime_arg2(*argv
++, len
== 10, tv
);
115 /* Otherwise use the current time of day. */
122 for (rval
= 0; *argv
; ++argv
) {
123 /* See if the file exists. */
124 if (stat(*argv
, &sb
)) {
126 /* Create the file. */
128 O_WRONLY
| O_CREAT
, DEFFILEMODE
);
129 if (fd
== -1 || fstat(fd
, &sb
) || close(fd
)) {
135 /* If using the current time, we're done. */
143 TIMESPEC_TO_TIMEVAL(&tv
[0], &sb
.st_atimespec
);
145 TIMESPEC_TO_TIMEVAL(&tv
[1], &sb
.st_mtimespec
);
148 if (!utimes(*argv
, tv
))
151 /* If the user specified a time, nothing else we can do. */
158 * System V and POSIX 1003.1 require that a NULL argument
159 * set the access/modification times to the current time.
160 * The permission checks are different, too, in that the
161 * ability to write the file is sufficient. Take a shot.
163 if (!utimes(*argv
, NULL
))
166 /* Try reading/writing. */
167 if (!S_ISLNK(sb
.st_mode
) && !S_ISDIR(sb
.st_mode
) &&
168 rw(*argv
, &sb
, fflag
))
176 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
179 stime_arg1(const char *arg
, struct timeval
*tvp
)
185 /* Start with the current time. */
187 if ((t
= localtime(&now
)) == NULL
)
189 /* [[CC]YY]MMDDhhmm[.SS] */
190 if ((p
= strchr(arg
, '.')) == NULL
) {
191 t
->tm_sec
= 0; /* Seconds defaults to 0. */
193 if (strlen(p
+ 1) != 2)
196 t
->tm_sec
= ATOI2(p
);
200 switch(strlen(arg
)) {
201 case 12: /* CCYYMMDDhhmm */
202 t
->tm_year
= ATOI2(arg
);
206 case 10: /* YYMMDDhhmm */
208 yearset
= ATOI2(arg
);
209 t
->tm_year
+= yearset
;
211 yearset
= ATOI2(arg
);
213 t
->tm_year
= yearset
+ 2000;
215 t
->tm_year
= yearset
+ 1900;
217 t
->tm_year
-= 1900; /* Convert to UNIX time. */
219 case 8: /* MMDDhhmm */
220 t
->tm_mon
= ATOI2(arg
);
221 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
222 t
->tm_mday
= ATOI2(arg
);
223 t
->tm_hour
= ATOI2(arg
);
224 t
->tm_min
= ATOI2(arg
);
230 t
->tm_isdst
= -1; /* Figure out DST. */
231 tvp
[0].tv_sec
= tvp
[1].tv_sec
= mktime(t
);
232 if (tvp
[0].tv_sec
== -1)
235 tvp
[0].tv_usec
= tvp
[1].tv_usec
= 0;
239 errx(1, "out of range or illegal time "
240 "specification: [[CC]YY]MMDDhhmm[.SS]");
244 stime_arg2(const char *arg
, int year
, struct timeval
*tvp
)
248 /* Start with the current time. */
250 if ((t
= localtime(&now
)) == NULL
)
253 t
->tm_mon
= ATOI2(arg
); /* MMDDhhmm[yy] */
254 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
255 t
->tm_mday
= ATOI2(arg
);
256 t
->tm_hour
= ATOI2(arg
);
257 t
->tm_min
= ATOI2(arg
);
259 t
->tm_year
= ATOI2(arg
);
260 if (t
->tm_year
< 39) /* support 2000-2038 not 1902-1969 */
264 t
->tm_isdst
= -1; /* Figure out DST. */
265 tvp
[0].tv_sec
= tvp
[1].tv_sec
= mktime(t
);
266 if (tvp
[0].tv_sec
== -1)
267 errx(1, "out of range or illegal time "
268 "specification: MMDDhhmm[yy]");
270 tvp
[0].tv_usec
= tvp
[1].tv_usec
= 0;
274 stime_file(const char *fname
, struct timeval
*tvp
)
278 if (stat(fname
, &sb
))
280 TIMESPEC_TO_TIMEVAL(tvp
, &sb
.st_atimespec
);
281 TIMESPEC_TO_TIMEVAL(tvp
+ 1, &sb
.st_mtimespec
);
285 rw(const char *fname
, const struct stat
*sbp
, int force
)
287 int fd
, needed_chmod
;
288 const char *warn_msg
= NULL
;
291 /* Try regular files. */
292 if (!S_ISREG(sbp
->st_mode
)) {
293 warnx("%s: %s", fname
, strerror(EFTYPE
));
298 if ((fd
= open(fname
, O_RDWR
, 0)) == -1) {
299 if (!force
|| chmod(fname
, DEFFILEMODE
))
301 if ((fd
= open(fname
, O_RDWR
, 0)) == -1)
306 if (sbp
->st_size
!= 0) {
307 if (read(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
309 if (lseek(fd
, (off_t
)0, SEEK_SET
) == -1)
311 if (write(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
314 if (write(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
316 if (ftruncate(fd
, (off_t
)0)) {
317 warn_msg
= "%s: file modified";
324 if (needed_chmod
&& chmod(fname
, sbp
->st_mode
)) {
325 warn_msg
= "%s: permissions modified";
331 if (warn_msg
!= NULL
)
332 warnx(warn_msg
, fname
);
341 fprintf(stderr
, "usage: touch [-acfm] [-r file] "
342 "[-t [[CC]YY]MMDDhhmm[.SS]] file ...\n");