2 * Copyright (c) 1993 Regents of the University of California.
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 * 7/20/97 - Ted Felix <tfelix@fred.net>
34 * Ported to Win32 from bsd4.3-reno at wuarchive.
35 * Biggest problems were err() routines, utines, and
36 * gettimeofday(). All easily fixed.
41 "@(#) Copyright (c) 1993 Regents of the University of California.\n\
42 All rights reserved.\n";
45 #include <sys/types.h>
50 //#error "__STDC__ defined"
53 #include <sys/utime.h>
58 #define DEFFILEMODE S_IWRITE
68 char *__progname
; /* Program name, from crt0. */
71 int rw
__P((char *, struct stat
*, int));
72 void stime_arg1
__P((char *, time_t *));
73 void stime_arg2
__P((char *, int, time_t *));
74 void stime_file
__P((char *, time_t *));
75 void usage
__P((void));
78 main(int argc
, char *argv
[])
82 int aflag
, cflag
, fflag
, mflag
, ch
, fd
, len
, rval
, timeset
;
88 aflag
= cflag
= fflag
= mflag
= timeset
= 0;
91 while ((ch
= getopt(argc
, argv
, "acfmr:t:")) != EOF
)
107 stime_file(optarg
, tv
);
111 stime_arg1(optarg
, tv
);
120 /* Default is both -a and -m. */
121 if (aflag
== 0 && mflag
== 0)
125 * If no -r or -t flag, at least two operands, the first of which
126 * is an 8 or 10 digit number, use the obsolete time specification.
128 if (!timeset
&& argc
> 1) {
129 (void)strtol(argv
[0], &p
, 10);
131 if (*p
== '\0' && (len
== 8 || len
== 10)) {
133 stime_arg2(argv
[0], len
== 10, tv
);
137 /* Otherwise use the current time of day. */
144 for (rval
= 0; *argv
; ++argv
) {
145 /* See if the file exists. */
146 if (stat(*argv
, &sb
)) {
148 /* Create the file. */
150 O_WRONLY
| O_CREAT
, DEFFILEMODE
);
151 if (fd
== -1 || fstat(fd
, &sb
) || close(fd
)) {
157 /* If using the current time, we're done. */
172 if (!utime(*argv
, &utb
))
175 /* If the user specified a time, nothing else we can do. */
182 * System V and POSIX 1003.1 require that a NULL argument
183 * set the access/modification times to the current time.
184 * The permission checks are different, too, in that the
185 * ability to write the file is sufficient. Take a shot.
187 if (!utime(*argv
, NULL
))
190 /* Try reading/writing. */
191 if (rw(*argv
, &sb
, fflag
))
197 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
200 stime_arg1(char *arg
, time_t *tvp
)
205 /* Start with the current time. */
206 if ((t
= localtime(&tvp
[0])) == NULL
)
208 /* [[CC]YY]MMDDhhmm[.SS] */
209 if ((p
= strchr(arg
, '.')) == NULL
)
210 t
->tm_sec
= 0; /* Seconds defaults to 0. */
212 if (strlen(p
+ 1) != 2)
215 t
->tm_sec
= ATOI2(p
);
219 switch(strlen(arg
)) {
220 case 12: /* CCYYMMDDhhmm */
221 t
->tm_year
= ATOI2(arg
);
225 case 10: /* YYMMDDhhmm */
227 yearset
= ATOI2(arg
);
228 t
->tm_year
+= yearset
;
230 yearset
= ATOI2(arg
);
232 t
->tm_year
= yearset
+ 2000;
234 t
->tm_year
= yearset
+ 1900;
236 t
->tm_year
-= 1900; /* Convert to UNIX time. */
238 case 8: /* MMDDhhmm */
239 t
->tm_mon
= ATOI2(arg
);
240 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
241 t
->tm_mday
= ATOI2(arg
);
242 t
->tm_hour
= ATOI2(arg
);
243 t
->tm_min
= ATOI2(arg
);
249 t
->tm_isdst
= -1; /* Figure out DST. */
250 tvp
[0] = tvp
[1] = mktime(t
);
253 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
257 stime_arg2(char *arg
, int year
, time_t *tvp
)
260 /* Start with the current time. */
261 if ((t
= localtime(&tvp
[0])) == NULL
)
264 t
->tm_mon
= ATOI2(arg
); /* MMDDhhmm[yy] */
265 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
266 t
->tm_mday
= ATOI2(arg
);
267 t
->tm_hour
= ATOI2(arg
);
268 t
->tm_min
= ATOI2(arg
);
270 t
->tm_year
= ATOI2(arg
);
272 t
->tm_isdst
= -1; /* Figure out DST. */
273 tvp
[0] = tvp
[1] = mktime(t
);
276 "out of range or illegal time specification: MMDDhhmm[yy]");
280 stime_file(char *fname
, time_t *tvp
)
284 if (stat(fname
, &sb
))
286 tvp
[0] = sb
.st_atime
;
287 tvp
[1] = sb
.st_mtime
;
291 rw(char *fname
, struct stat
*sbp
, int force
)
293 int fd
, needed_chmod
, rval
;
296 /* Try regular files and directories. */
297 if (!S_ISREG(sbp
->st_mode
) && !S_ISDIR(sbp
->st_mode
)) {
298 warnx("%s: %s", fname
, strerror(0));
302 needed_chmod
= rval
= 0;
303 if ((fd
= open(fname
, O_RDWR
, 0)) == -1) {
304 if (!force
|| chmod(fname
, DEFFILEMODE
))
306 if ((fd
= open(fname
, O_RDWR
, 0)) == -1)
311 if (sbp
->st_size
!= 0) {
312 if (read(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
314 if (lseek(fd
, (off_t
)0, SEEK_SET
) == -1)
316 if (write(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
319 if (write(fd
, &byte
, sizeof(byte
)) != sizeof(byte
)) {
322 /* } else if (ftruncate(fd, (off_t)0)) {*/
323 } else if (chsize(fd
, (off_t
)0)) {
325 warn("%s: file modified", fname
);
329 if (close(fd
) && rval
!= 1) {
333 if (needed_chmod
&& chmod(fname
, sbp
->st_mode
) && rval
!= 1) {
335 warn("%s: permissions modified", fname
);
344 (void)fprintf(stderr
,
345 "usage: touch [-acfm] [-r file] [-t time] file ...\n");