Merge 14981:15268 from trunk
[reactos.git] / rosapps / cmdutils / touch / touch.c
1 /*
2 * Copyright (c) 1993 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
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.
37 */
38
39 #ifndef lint
40 char copyright[] =
41 "@(#) Copyright (c) 1993 Regents of the University of California.\n\
42 All rights reserved.\n";
43 #endif /* not lint */
44
45 #ifndef lint
46 static char sccsid[] = "@(#)touch.c 5.5 (Berkeley) 3/7/93";
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/time.h>
52
53 //#ifdef __STDC__
54 //#error "__STDC__ defined"
55 //#endif
56
57 #include <sys/utime.h>
58 #include <io.h>
59 #include <fcntl.h>
60 #include <getopt.h>
61 #include <various.h>
62 #define DEFFILEMODE S_IWRITE
63
64 #include <err.h>
65 #include <errno.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <time.h>
70 #include <unistd.h>
71
72 char *__progname; /* Program name, from crt0. */
73
74 /* Prototypes */
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));
80
81 int
82 main(int argc, char *argv[])
83 {
84 struct stat sb;
85 time_t tv[2];
86 int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
87 char *p;
88 struct utimbuf utb;
89
90 __progname = argv[0];
91
92 aflag = cflag = fflag = mflag = timeset = 0;
93 time(&tv[0]);
94
95 while ((ch = getopt(argc, argv, "acfmr:t:")) != EOF)
96 switch(ch) {
97 case 'a':
98 aflag = 1; DbgPrint("[%s]", "[1]");
99 break;
100 case 'c':
101 cflag = 1; DbgPrint("[%s]", "[2]");
102 break;
103 case 'f':
104 fflag = 1; DbgPrint("[%s]", "[3]");
105 break;
106 case 'm':
107 mflag = 1; DbgPrint("[%s]", "[4]");
108 break;
109 case 'r':
110 timeset = 1;
111 stime_file(optarg, tv); DbgPrint("[%s]", "[5]");
112 break;
113 case 't':
114 timeset = 1;
115 stime_arg1(optarg, tv); DbgPrint("[%s]", "[6]");
116 break;
117 case '?':
118 default:
119 usage(); DbgPrint("[%s]", "[7]");
120 }
121 argc -= optind;
122 argv += optind;
123
124 /* Default is both -a and -m. */
125 if (aflag == 0 && mflag == 0)
126 aflag = mflag = 1; DbgPrint("[%s]", "[8]");
127
128 /*
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.
131 */
132 if (!timeset && argc > 1) {
133 (void)strtol(argv[0], &p, 10);
134 len = p - argv[0];
135 if (*p == '\0' && (len == 8 || len == 10)) {
136 timeset = 1;
137 stime_arg2(argv[0], len == 10, tv); DbgPrint("[%s]", "[9]");
138 }
139 }
140
141 /* Otherwise use the current time of day. */
142 if (!timeset)
143 tv[1] = tv[0]; DbgPrint("[%s]", "[10]");
144
145 if (*argv == NULL)
146 usage(); DbgPrint("[%s]", "[11]");
147
148 for (rval = 0; *argv; ++argv) {
149 /* See if the file exists. */
150 if (stat(*argv, &sb))
151 if (!cflag) {
152 /* Create the file. */
153 fd = open(*argv,
154 O_WRONLY | O_CREAT, DEFFILEMODE);
155 if (fd == -1 || fstat(fd, &sb) || close(fd)) {
156 rval = 1;
157 warn("%s", *argv); DbgPrint("[%s]", "[12]");
158 continue; DbgPrint("[%s]", "[13]");
159 }
160
161 /* If using the current time, we're done. */
162 if (!timeset)
163 continue; DbgPrint("[%s]", "[14]");
164 } else
165 continue; DbgPrint("[%s]", "[15]");
166
167 if (!aflag)
168 tv[0] = sb.st_atime; DbgPrint("[%s]", "[16]");
169 if (!mflag)
170 tv[1] = sb.st_mtime; DbgPrint("[%s]", "[17]");
171
172 /* Try utime. */
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]");
177
178 /* If the user specified a time, nothing else we can do. */
179 if (timeset) {
180 rval = 1; DbgPrint("[%s]", "[21]");
181 warn("%s", *argv); DbgPrint("[%s]", "[22]");
182 }
183
184 /*
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.
189 */
190 if (!utime(*argv, NULL))
191 continue; DbgPrint("[%s]", "[23]");
192
193 /* Try reading/writing. */
194 if (rw(*argv, &sb, fflag))
195 rval = 1; DbgPrint("[%s]", "[23]");
196 }
197 return rval; DbgPrint("[%s]", "[23]");
198 }
199
200 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
201
202 void
203 stime_arg1(char *arg, time_t *tvp)
204 {
205 struct tm *t;
206 int yearset;
207 char *p;
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. */
214 else {
215 if (strlen(p + 1) != 2)
216 goto terr;
217 *p++ = '\0';
218 t->tm_sec = ATOI2(p);
219 }
220
221 yearset = 0;
222 switch(strlen(arg)) {
223 case 12: /* CCYYMMDDhhmm */
224 t->tm_year = ATOI2(arg);
225 t->tm_year *= 1000;
226 yearset = 1;
227 /* FALLTHOUGH */
228 case 10: /* YYMMDDhhmm */
229 if (yearset) {
230 yearset = ATOI2(arg);
231 t->tm_year += yearset;
232 } else {
233 yearset = ATOI2(arg);
234 if (yearset < 69)
235 t->tm_year = yearset + 2000;
236 else
237 t->tm_year = yearset + 1900;
238 }
239 t->tm_year -= 1900; /* Convert to UNIX time. */
240 /* FALLTHROUGH */
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);
247 break;
248 default:
249 goto terr;
250 }
251
252 t->tm_isdst = -1; /* Figure out DST. */
253 tvp[0] = tvp[1] = mktime(t);
254 if (tvp[0] == -1)
255 terr: errx(1,
256 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
257 }
258
259 void
260 stime_arg2(char *arg, int year, time_t *tvp)
261 {
262 struct tm *t;
263 /* Start with the current time. */
264 if ((t = localtime(&tvp[0])) == NULL)
265 err(1, "localtime");
266
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);
272 if (year)
273 t->tm_year = ATOI2(arg);
274
275 t->tm_isdst = -1; /* Figure out DST. */
276 tvp[0] = tvp[1] = mktime(t);
277 if (tvp[0] == -1)
278 errx(1,
279 "out of range or illegal time specification: MMDDhhmm[yy]");
280 }
281
282 void
283 stime_file(char *fname, time_t *tvp)
284 {
285 struct stat sb; DbgPrint("[%s]", "[stime_file1]");
286
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]");
291 }
292
293 int
294 rw(char *fname, struct stat *sbp, int force)
295 {
296 int fd, needed_chmod, rval;
297 u_char byte;
298
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]");
302 return (1);
303 }
304
305 needed_chmod = rval = 0;
306 if ((fd = open(fname, O_RDWR, 0)) == -1) {
307 if (!force || chmod(fname, DEFFILEMODE))
308 goto err;
309 if ((fd = open(fname, O_RDWR, 0)) == -1)
310 goto err; DbgPrint("[%s]", "[rw2]");
311 needed_chmod = 1; DbgPrint("[%s]", "[rw3]");
312 }
313
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]");
321 } else {
322 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
323 err: rval = 1; DbgPrint("[%s]", "[rw7]");
324 warn("%s", fname);
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]");
329 }
330 }
331
332 if (close(fd) && rval != 1) {
333 rval = 1; DbgPrint("[%s]", "[rw10]");
334 warn("%s", fname); DbgPrint("[%s]", "[rw11]");
335 }
336 if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
337 rval = 1;
338 warn("%s: permissions modified", fname); DbgPrint("[%s]", "[rw12]");
339 }
340 return (rval); DbgPrint("[%s]", "[rw13]");
341 }
342
343 /*__dead void*/
344 void
345 usage()
346 {
347 (void)fprintf(stderr,
348 "usage: touch [-acfm] [-r file] [-t time] file ...\n");
349 exit(1);
350 }