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";
46 static char sccsid
[] = "@(#)touch.c 5.5 (Berkeley) 3/7/93";
49 #include <sys/types.h>
54 //#error "__STDC__ defined"
57 #include <sys/utime.h>
62 #define DEFFILEMODE S_IWRITE
72 char *__progname
; /* Program name, from crt0. */
75 int rw
__P((char *, struct stat
*, int));
76 void stime_arg1
__P((char *, time_t *));
77 void stime_arg2
__P((char *, int, time_t *));
78 void stime_file
__P((char *, time_t *));
79 void usage
__P((void));
82 main(int argc
, char *argv
[])
86 int aflag
, cflag
, fflag
, mflag
, ch
, fd
, len
, rval
, timeset
;
92 aflag
= cflag
= fflag
= mflag
= timeset
= 0;
95 while ((ch
= getopt(argc
, argv
, "acfmr:t:")) != EOF
)
98 aflag
= 1; DbgPrint("[%s]", "[1]");
101 cflag
= 1; DbgPrint("[%s]", "[2]");
104 fflag
= 1; DbgPrint("[%s]", "[3]");
107 mflag
= 1; DbgPrint("[%s]", "[4]");
111 stime_file(optarg
, tv
); DbgPrint("[%s]", "[5]");
115 stime_arg1(optarg
, tv
); DbgPrint("[%s]", "[6]");
119 usage(); DbgPrint("[%s]", "[7]");
124 /* Default is both -a and -m. */
125 if (aflag
== 0 && mflag
== 0)
126 aflag
= mflag
= 1; DbgPrint("[%s]", "[8]");
129 * If no -r or -t flag, at least two operands, the first of which
130 * is an 8 or 10 digit number, use the obsolete time specification.
132 if (!timeset
&& argc
> 1) {
133 (void)strtol(argv
[0], &p
, 10);
135 if (*p
== '\0' && (len
== 8 || len
== 10)) {
137 stime_arg2(argv
[0], len
== 10, tv
); DbgPrint("[%s]", "[9]");
141 /* Otherwise use the current time of day. */
143 tv
[1] = tv
[0]; DbgPrint("[%s]", "[10]");
146 usage(); DbgPrint("[%s]", "[11]");
148 for (rval
= 0; *argv
; ++argv
) {
149 /* See if the file exists. */
150 if (stat(*argv
, &sb
))
152 /* Create the file. */
154 O_WRONLY
| O_CREAT
, DEFFILEMODE
);
155 if (fd
== -1 || fstat(fd
, &sb
) || close(fd
)) {
157 warn("%s", *argv
); DbgPrint("[%s]", "[12]");
158 continue; DbgPrint("[%s]", "[13]");
161 /* If using the current time, we're done. */
163 continue; DbgPrint("[%s]", "[14]");
165 continue; DbgPrint("[%s]", "[15]");
168 tv
[0] = sb
.st_atime
; DbgPrint("[%s]", "[16]");
170 tv
[1] = sb
.st_mtime
; DbgPrint("[%s]", "[17]");
173 utb
.actime
= tv
[0]; DbgPrint("[%s]", "[18]");
174 utb
.modtime
= tv
[1]; DbgPrint("[%s]", "[19]");
175 if (!utime(*argv
, &utb
))
176 continue; DbgPrint("[%s]", "[20]");
178 /* If the user specified a time, nothing else we can do. */
180 rval
= 1; DbgPrint("[%s]", "[21]");
181 warn("%s", *argv
); DbgPrint("[%s]", "[22]");
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 (!utime(*argv
, NULL
))
191 continue; DbgPrint("[%s]", "[23]");
193 /* Try reading/writing. */
194 if (rw(*argv
, &sb
, fflag
))
195 rval
= 1; DbgPrint("[%s]", "[23]");
197 exit(rval
); DbgPrint("[%s]", "[23]");
200 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
203 stime_arg1(char *arg
, time_t *tvp
)
208 /* Start with the current time. */
209 if ((t
= localtime(&tvp
[0])) == NULL
)
210 err(1, "localtime"); DbgPrint("[%s]", "[23]");
211 /* [[CC]YY]MMDDhhmm[.SS] */
212 if ((p
= strchr(arg
, '.')) == NULL
)
213 t
->tm_sec
= 0; /* Seconds defaults to 0. */
215 if (strlen(p
+ 1) != 2)
218 t
->tm_sec
= ATOI2(p
);
222 switch(strlen(arg
)) {
223 case 12: /* CCYYMMDDhhmm */
224 t
->tm_year
= ATOI2(arg
);
228 case 10: /* YYMMDDhhmm */
230 yearset
= ATOI2(arg
);
231 t
->tm_year
+= yearset
;
233 yearset
= ATOI2(arg
);
235 t
->tm_year
= yearset
+ 2000;
237 t
->tm_year
= yearset
+ 1900;
239 t
->tm_year
-= 1900; /* Convert to UNIX time. */
241 case 8: /* MMDDhhmm */
242 t
->tm_mon
= ATOI2(arg
);
243 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
244 t
->tm_mday
= ATOI2(arg
);
245 t
->tm_hour
= ATOI2(arg
);
246 t
->tm_min
= ATOI2(arg
);
252 t
->tm_isdst
= -1; /* Figure out DST. */
253 tvp
[0] = tvp
[1] = mktime(t
);
256 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
260 stime_arg2(char *arg
, int year
, time_t *tvp
)
263 /* Start with the current time. */
264 if ((t
= localtime(&tvp
[0])) == NULL
)
267 t
->tm_mon
= ATOI2(arg
); /* MMDDhhmm[yy] */
268 --t
->tm_mon
; /* Convert from 01-12 to 00-11 */
269 t
->tm_mday
= ATOI2(arg
);
270 t
->tm_hour
= ATOI2(arg
);
271 t
->tm_min
= ATOI2(arg
);
273 t
->tm_year
= ATOI2(arg
);
275 t
->tm_isdst
= -1; /* Figure out DST. */
276 tvp
[0] = tvp
[1] = mktime(t
);
279 "out of range or illegal time specification: MMDDhhmm[yy]");
283 stime_file(char *fname
, time_t *tvp
)
285 struct stat sb
; DbgPrint("[%s]", "[stime_file1]");
287 if (stat(fname
, &sb
))
288 err(1, "%s", fname
); DbgPrint("[%s]", "[stime_file1]");
289 tvp
[0] = sb
.st_atime
; DbgPrint("[%s]", "[stime_file1]");
290 tvp
[1] = sb
.st_mtime
; DbgPrint("[%s]", "[stime_file1]");
294 rw(char *fname
, struct stat
*sbp
, int force
)
296 int fd
, needed_chmod
, rval
;
299 /* Try regular files and directories. */
300 if (!S_ISREG(sbp
->st_mode
) && !S_ISDIR(sbp
->st_mode
)) {
301 warnx("%s: %s", fname
, strerror(0)); DbgPrint("[%s]", "[rw1]");
305 needed_chmod
= rval
= 0;
306 if ((fd
= open(fname
, O_RDWR
, 0)) == -1) {
307 if (!force
|| chmod(fname
, DEFFILEMODE
))
309 if ((fd
= open(fname
, O_RDWR
, 0)) == -1)
310 goto err
; DbgPrint("[%s]", "[rw2]");
311 needed_chmod
= 1; DbgPrint("[%s]", "[rw3]");
314 if (sbp
->st_size
!= 0) {
315 if (read(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
316 goto err
; DbgPrint("[%s]", "[rw4]");
317 if (lseek(fd
, (off_t
)0, SEEK_SET
) == -1)
318 goto err
; DbgPrint("[%s]", "[rw5]");
319 if (write(fd
, &byte
, sizeof(byte
)) != sizeof(byte
))
320 goto err
; DbgPrint("[%s]", "[rw6]");
322 if (write(fd
, &byte
, sizeof(byte
)) != sizeof(byte
)) {
323 err
: rval
= 1; DbgPrint("[%s]", "[rw7]");
325 /* } else if (ftruncate(fd, (off_t)0)) {*/
326 } else if (chsize(fd
, (off_t
)0)) {
327 rval
= 1; DbgPrint("[%s]", "[rw8]");
328 warn("%s: file modified", fname
);DbgPrint("[%s]", "[rw9]");
332 if (close(fd
) && rval
!= 1) {
333 rval
= 1; DbgPrint("[%s]", "[rw10]");
334 warn("%s", fname
); DbgPrint("[%s]", "[rw11]");
336 if (needed_chmod
&& chmod(fname
, sbp
->st_mode
) && rval
!= 1) {
338 warn("%s: permissions modified", fname
); DbgPrint("[%s]", "[rw12]");
340 return (rval
); DbgPrint("[%s]", "[rw13]");
347 (void)fprintf(stderr
,
348 "usage: touch [-acfm] [-r file] [-t time] file ...\n");