mkfs, mkproto: minor improvements
[minix.git] / commands / touch / touch.c
blobefedae1e0b257c000302d6f02117cf0cd2dacb5b
1 /* $OpenBSD: touch.c,v 1.17 2007/08/06 19:16:06 sobrado Exp $ */
2 /* $NetBSD: touch.c,v 1.11 1995/08/31 22:10:06 jtc Exp $ */
4 /*
5 * Copyright (c) 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/time.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <locale.h>
45 #include <time.h>
46 #include <utime.h>
47 #include <tzfile.h>
48 #include <unistd.h>
50 void stime_arg1(char *, struct timeval *);
51 void stime_arg2(char *, int, struct timeval *);
52 void stime_file(char *, struct timeval *);
53 __dead void usage(void);
55 char * __progname;
57 int
58 main(int argc, char *argv[])
60 struct stat sb;
61 struct timeval tv[2];
62 struct utimbuf timbuf;
63 int aflag, cflag, mflag, ch, fd, len, rval, timeset;
64 char *p;
66 (void)setlocale(LC_ALL, "");
67 __progname = argv[0];
69 aflag = cflag = mflag = timeset = 0;
70 if (gettimeofday(&tv[0], NULL))
71 err(1, "gettimeofday");
73 while ((ch = getopt(argc, argv, "acfmr:t:")) != -1)
74 switch (ch) {
75 case 'a':
76 aflag = 1;
77 break;
78 case 'c':
79 cflag = 1;
80 break;
81 case 'f':
82 break;
83 case 'm':
84 mflag = 1;
85 break;
86 case 'r':
87 timeset = 1;
88 stime_file(optarg, tv);
89 break;
90 case 't':
91 timeset = 1;
92 stime_arg1(optarg, tv);
93 break;
94 default:
95 usage();
97 argc -= optind;
98 argv += optind;
100 /* Default is both -a and -m. */
101 if (aflag == 0 && mflag == 0)
102 aflag = mflag = 1;
105 * If no -r or -t flag, at least two operands, the first of which
106 * is an 8 or 10 digit number, use the obsolete time specification.
108 if (!timeset && argc > 1) {
109 (void)strtol(argv[0], &p, 10);
110 len = p - argv[0];
111 if (*p == '\0' && (len == 8 || len == 10)) {
112 timeset = 1;
113 stime_arg2(*argv++, len == 10, tv);
117 /* Otherwise use the current time of day. */
118 if (!timeset)
119 tv[1] = tv[0];
121 if (*argv == NULL)
122 usage();
124 for (rval = 0; *argv; ++argv) {
125 /* See if the file exists. */
126 if (stat(*argv, &sb)) {
127 if (!cflag) {
128 /* Create the file. */
129 fd = open(*argv,
130 O_WRONLY | O_CREAT, DEFFILEMODE);
131 if (fd == -1 || fstat(fd, &sb) || close(fd)) {
132 rval = 1;
133 warn("%s", *argv);
134 continue;
137 /* If using the current time, we're done. */
138 if (!timeset)
139 continue;
140 } else
141 continue;
144 #if 0
145 if (!aflag)
146 TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
147 if (!mflag)
148 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
149 #else
150 if (!aflag)
151 tv[0].tv_sec = sb.st_atime;
152 if (!mflag)
153 tv[1].tv_sec = sb.st_mtime;
154 #endif
156 /* Try utimes(2). */
157 #if 0
158 if (!utimes(*argv, tv))
159 continue;
160 #else
161 timbuf.actime = tv[0].tv_sec;
162 timbuf.modtime = tv[1].tv_sec;
163 if (!utime(*argv, &timbuf))
164 continue;
165 #endif
167 /* If the user specified a time, nothing else we can do. */
168 if (timeset) {
169 rval = 1;
170 warn("%s", *argv);
174 * System V and POSIX 1003.1 require that a NULL argument
175 * set the access/modification times to the current time.
176 * The permission checks are different, too, in that the
177 * ability to write the file is sufficient. Take a shot.
179 #if 0
180 if (!utimes(*argv, NULL))
181 continue;
182 #else
183 if (!utime(*argv, NULL))
184 continue;
185 #endif
187 rval = 1;
188 warn("%s", *argv);
190 exit(rval);
193 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
195 void
196 stime_arg1(char *arg, struct timeval *tvp)
198 struct tm *lt;
199 time_t tmptime;
200 int yearset;
201 char *dot, *p;
202 /* Start with the current time. */
203 tmptime = tvp[0].tv_sec;
204 if ((lt = localtime(&tmptime)) == NULL)
205 err(1, "localtime");
206 /* [[CC]YY]MMDDhhmm[.SS] */
207 for (p = arg, dot = NULL; *p != '\0'; p++) {
208 if (*p == '.' && dot == NULL)
209 dot = p;
210 else if (!isdigit((unsigned char)*p))
211 goto terr;
213 if (dot == NULL)
214 lt->tm_sec = 0; /* Seconds defaults to 0. */
215 else {
216 *dot++ = '\0';
217 if (strlen(dot) != 2)
218 goto terr;
219 lt->tm_sec = ATOI2(dot);
220 if (lt->tm_sec > 61) /* Could be leap second. */
221 goto terr;
224 yearset = 0;
225 switch (strlen(arg)) {
226 case 12: /* CCYYMMDDhhmm */
227 lt->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE;
228 yearset = 1;
229 /* FALLTHROUGH */
230 case 10: /* YYMMDDhhmm */
231 if (yearset) {
232 yearset = ATOI2(arg);
233 lt->tm_year += yearset;
234 } else {
235 yearset = ATOI2(arg);
236 /* Preserve current century. */
237 lt->tm_year = ((lt->tm_year / 100) * 100) + yearset;
239 /* FALLTHROUGH */
240 case 8: /* MMDDhhmm */
241 lt->tm_mon = ATOI2(arg);
242 if (lt->tm_mon > 12 || lt->tm_mon == 0)
243 goto terr;
244 --lt->tm_mon; /* Convert from 01-12 to 00-11 */
245 lt->tm_mday = ATOI2(arg);
246 if (lt->tm_mday > 31 || lt->tm_mday == 0)
247 goto terr;
248 lt->tm_hour = ATOI2(arg);
249 if (lt->tm_hour > 23)
250 goto terr;
251 lt->tm_min = ATOI2(arg);
252 if (lt->tm_min > 59)
253 goto terr;
254 break;
255 default:
256 goto terr;
259 lt->tm_isdst = -1; /* Figure out DST. */
260 tvp[0].tv_sec = tvp[1].tv_sec = mktime(lt);
261 if (tvp[0].tv_sec == -1)
262 terr: errx(1,
263 "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
265 tvp[0].tv_usec = tvp[1].tv_usec = 0;
268 void
269 stime_arg2(char *arg, int year, struct timeval *tvp)
271 struct tm *lt;
272 time_t tmptime;
273 /* Start with the current time. */
274 tmptime = tvp[0].tv_sec;
275 if ((lt = localtime(&tmptime)) == NULL)
276 err(1, "localtime");
278 lt->tm_mon = ATOI2(arg); /* MMDDhhmm[YY] */
279 if (lt->tm_mon > 12 || lt->tm_mon == 0)
280 goto terr;
281 --lt->tm_mon; /* Convert from 01-12 to 00-11 */
282 lt->tm_mday = ATOI2(arg);
283 if (lt->tm_mday > 31 || lt->tm_mday == 0)
284 goto terr;
285 lt->tm_hour = ATOI2(arg);
286 if (lt->tm_hour > 23)
287 goto terr;
288 lt->tm_min = ATOI2(arg);
289 if (lt->tm_min > 59)
290 goto terr;
291 if (year) {
292 year = ATOI2(arg); /* Preserve current century. */
293 lt->tm_year = ((lt->tm_year / 100) * 100) + year;
295 lt->tm_sec = 0;
297 lt->tm_isdst = -1; /* Figure out DST. */
298 tvp[0].tv_sec = tvp[1].tv_sec = mktime(lt);
299 if (tvp[0].tv_sec == -1)
300 terr: errx(1,
301 "out of range or illegal time specification: MMDDhhmm[YY]");
303 tvp[0].tv_usec = tvp[1].tv_usec = 0;
306 void
307 stime_file(char *fname, struct timeval *tvp)
309 struct stat sb;
311 if (stat(fname, &sb))
312 err(1, "%s", fname);
313 #if 0
314 TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
315 TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
316 #else
317 (tvp + 0)->tv_sec = sb.st_atime;
318 (tvp + 1)->tv_sec = sb.st_mtime;
319 #endif
322 __dead void
323 usage(void)
325 extern char *__progname;
327 (void)fprintf(stderr,
328 "usage: %s [-acm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n",
329 __progname);
330 exit(1);