1 /* $NetBSD: touch.c,v 1.27 2008/07/21 14:19:27 lukem 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)touch.c 8.2 (Berkeley) 4/28/95";
42 __RCSID("$NetBSD: touch.c,v 1.27 2008/07/21 14:19:27 lukem Exp $");
45 #include <sys/types.h>
60 int main
__P((int, char **));
61 int rw
__P((char *, struct stat
*, int));
62 void stime_arg1
__P((char *, struct timeval
*));
63 void stime_arg2
__P((char *, int, struct timeval
*));
64 void stime_file
__P((char *, struct timeval
*));
65 void usage
__P((void));
74 int aflag
, cflag
, fflag
, hflag
, mflag
, ch
, fd
, len
, rval
, timeset
;
76 int (*change_file_times
) __P((const char *, const struct timeval
*));
77 int (*get_file_status
) __P((const char *, struct stat
*));
79 setlocale(LC_ALL
, "");
81 aflag
= cflag
= fflag
= hflag
= mflag
= timeset
= 0;
82 if (gettimeofday(&tv
[0], NULL
))
83 err(1, "gettimeofday");
85 while ((ch
= getopt(argc
, argv
, "acfhmr:t:")) != -1)
104 stime_file(optarg
, tv
);
108 stime_arg1(optarg
, tv
);
117 /* Default is both -a and -m. */
118 if (aflag
== 0 && mflag
== 0)
122 cflag
= 1; /* Don't create new file */
123 change_file_times
= lutimes
;
124 get_file_status
= lstat
;
126 change_file_times
= utimes
;
127 get_file_status
= stat
;
131 * If no -r or -t flag, at least two operands, the first of which
132 * is an 8 or 10 digit number, use the obsolete time specification.
134 if (!timeset
&& argc
> 1) {
135 (void)strtol(argv
[0], &p
, 10);
137 if (*p
== '\0' && (len
== 8 || len
== 10)) {
139 stime_arg2(*argv
++, len
== 10, tv
);
143 /* Otherwise use the current time of day. */
150 for (rval
= 0; *argv
; ++argv
) {
151 /* See if the file exists. */
152 if ((*get_file_status
)(*argv
, &sb
)) {
154 /* Create the file. */
156 O_WRONLY
| O_CREAT
, DEFFILEMODE
);
157 if (fd
== -1 || fstat(fd
, &sb
) || close(fd
)) {
163 /* If using the current time, we're done. */
170 TIMESPEC_TO_TIMEVAL(&tv
[0], &sb
.st_atimespec
);
172 TIMESPEC_TO_TIMEVAL(&tv
[1], &sb
.st_mtimespec
);
175 if (!(*change_file_times
)(*argv
, tv
))
178 /* If the user specified a time, nothing else we can do. */
185 * System V and POSIX 1003.1 require that a NULL argument
186 * set the access/modification times to the current time.
187 * The permission checks are different, too, in that the
188 * ability to write the file is sufficient. Take a shot.
190 if (!(*change_file_times
)(*argv
, NULL
))
199 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
210 /* Start with the current time. */
211 tmptime
= tvp
[0].tv_sec
;
212 if ((t
= localtime(&tmptime
)) == NULL
)
214 /* [[CC]YY]MMDDhhmm[.SS] */
215 if ((p
= strchr(arg
, '.')) == NULL
)
216 t
->tm_sec
= 0; /* Seconds defaults to 0. */
218 if (strlen(p
+ 1) != 2)
221 t
->tm_sec
= ATOI2(p
);
225 switch (strlen(arg
)) {
226 case 12: /* CCYYMMDDhhmm */
227 t
->tm_year
= ATOI2(arg
) * 100 - TM_YEAR_BASE
;
230 case 10: /* YYMMDDhhmm */
232 t
->tm_year
+= ATOI2(arg
);
234 yearset
= ATOI2(arg
);
236 t
->tm_year
= yearset
+ 2000 - TM_YEAR_BASE
;
238 t
->tm_year
= yearset
+ 1900 - TM_YEAR_BASE
;
241 case 8: /* MMDDhhmm */
242 t
->tm_mon
= ATOI2(arg
);
243 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
246 t
->tm_mday
= ATOI2(arg
);
249 t
->tm_hour
= ATOI2(arg
);
252 t
->tm_min
= ATOI2(arg
);
258 t
->tm_isdst
= -1; /* Figure out DST. */
259 tvp
[0].tv_sec
= tvp
[1].tv_sec
= mktime(t
);
260 if (tvp
[0].tv_sec
== -1)
262 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
264 tvp
[0].tv_usec
= tvp
[1].tv_usec
= 0;
268 stime_arg2(arg
, year
, tvp
)
275 /* Start with the current time. */
276 tmptime
= tvp
[0].tv_sec
;
277 if ((t
= localtime(&tmptime
)) == NULL
)
280 t
->tm_mon
= ATOI2(arg
); /* MMDDhhmm[yy] */
281 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
282 t
->tm_mday
= ATOI2(arg
);
283 t
->tm_hour
= ATOI2(arg
);
284 t
->tm_min
= ATOI2(arg
);
288 t
->tm_year
= year
+ 2000 - TM_YEAR_BASE
;
290 t
->tm_year
= year
+ 1900 - TM_YEAR_BASE
;
294 t
->tm_isdst
= -1; /* Figure out DST. */
295 tvp
[0].tv_sec
= tvp
[1].tv_sec
= mktime(t
);
296 if (tvp
[0].tv_sec
== -1)
298 "out of range or illegal time specification: MMDDhhmm[yy]");
300 tvp
[0].tv_usec
= tvp
[1].tv_usec
= 0;
304 stime_file(fname
, tvp
)
310 if (stat(fname
, &sb
))
312 TIMESPEC_TO_TIMEVAL(&tvp
[0], &sb
.st_atimespec
);
313 TIMESPEC_TO_TIMEVAL(&tvp
[1], &sb
.st_mtimespec
);
319 (void)fprintf(stderr
,
320 "usage: touch [-acfhm] [-r file] [-t time] file ...\n");