Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.bin / touch / touch.c
1 /*
2  * Copyright (c) 1993
3  *      The Regents of the University of California.  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  * @(#) Copyright (c) 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)touch.c  8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/touch/touch.c,v 1.11.2.2 2002/07/28 06:52:15 eric Exp $
36  * $DragonFly: src/usr.bin/touch/touch.c,v 1.2 2003/06/17 04:29:33 dillon Exp $
37  */
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <time.h>
50 #include <unistd.h>
51
52 int     rw __P((char *, struct stat *, int));
53 void    stime_arg1 __P((char *, struct timeval *));
54 void    stime_arg2 __P((char *, int, struct timeval *));
55 void    stime_file __P((char *, struct timeval *));
56 void    usage __P((void));
57
58 int
59 main(argc, argv)
60         int argc;
61         char *argv[];
62 {
63         struct stat sb;
64         struct timeval tv[2];
65         int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
66         char *p;
67
68         aflag = cflag = fflag = mflag = timeset = 0;
69         if (gettimeofday(&tv[0], NULL))
70                 err(1, "gettimeofday");
71
72         while ((ch = getopt(argc, argv, "acfmr:t:")) != -1)
73                 switch(ch) {
74                 case 'a':
75                         aflag = 1;
76                         break;
77                 case 'c':
78                         cflag = 1;
79                         break;
80                 case 'f':
81                         fflag = 1;
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                 case '?':
95                 default:
96                         usage();
97                 }
98         argc -= optind;
99         argv += optind;
100
101         /* Default is both -a and -m. */
102         if (aflag == 0 && mflag == 0)
103                 aflag = mflag = 1;
104
105         /*
106          * If no -r or -t flag, at least two operands, the first of which
107          * is an 8 or 10 digit number, use the obsolete time specification.
108          */
109         if (!timeset && argc > 1) {
110                 (void)strtol(argv[0], &p, 10);
111                 len = p - argv[0];
112                 if (*p == '\0' && (len == 8 || len == 10)) {
113                         timeset = 1;
114                         stime_arg2(*argv++, len == 10, tv);
115                 }
116         }
117
118         /* Otherwise use the current time of day. */
119         if (!timeset)
120                 tv[1] = tv[0];
121
122         if (*argv == NULL)
123                 usage();
124
125         for (rval = 0; *argv; ++argv) {
126                 /* See if the file exists. */
127                 if (stat(*argv, &sb)) {
128                         if (!cflag) {
129                                 /* Create the file. */
130                                 fd = open(*argv,
131                                     O_WRONLY | O_CREAT, DEFFILEMODE);
132                                 if (fd == -1 || fstat(fd, &sb) || close(fd)) {
133                                         rval = 1;
134                                         warn("%s", *argv);
135                                         continue;
136                                 }
137
138                                 /* If using the current time, we're done. */
139                                 if (!timeset)
140                                         continue;
141                         } else
142                                 continue;
143                 }
144
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
150                 /* Try utimes(2). */
151                 if (!utimes(*argv, tv))
152                         continue;
153
154                 /* If the user specified a time, nothing else we can do. */
155                 if (timeset) {
156                         rval = 1;
157                         warn("%s", *argv);
158                 }
159
160                 /*
161                  * System V and POSIX 1003.1 require that a NULL argument
162                  * set the access/modification times to the current time.
163                  * The permission checks are different, too, in that the
164                  * ability to write the file is sufficient.  Take a shot.
165                  */
166                  if (!utimes(*argv, NULL))
167                         continue;
168
169                 /* Try reading/writing. */
170                 if (!S_ISLNK(sb.st_mode) && !S_ISDIR(sb.st_mode) &&
171                     rw(*argv, &sb, fflag))
172                         rval = 1;
173                 else
174                         warn("%s", *argv);
175         }
176         exit(rval);
177 }
178
179 #define ATOI2(ar)       ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
180
181 void
182 stime_arg1(arg, tvp)
183         char *arg;
184         struct timeval *tvp;
185 {
186         time_t now;
187         struct tm *t;
188         int yearset;
189         char *p;
190                                         /* Start with the current time. */
191         now = tvp[0].tv_sec;
192         if ((t = localtime(&now)) == NULL)
193                 err(1, "localtime");
194                                         /* [[CC]YY]MMDDhhmm[.SS] */
195         if ((p = strchr(arg, '.')) == NULL)
196                 t->tm_sec = 0;          /* Seconds defaults to 0. */
197         else {
198                 if (strlen(p + 1) != 2)
199                         goto terr;
200                 *p++ = '\0';
201                 t->tm_sec = ATOI2(p);
202         }
203
204         yearset = 0;
205         switch(strlen(arg)) {
206         case 12:                        /* CCYYMMDDhhmm */
207                 t->tm_year = ATOI2(arg);
208                 t->tm_year *= 100;
209                 yearset = 1;
210                 /* FALLTHOUGH */
211         case 10:                        /* YYMMDDhhmm */
212                 if (yearset) {
213                         yearset = ATOI2(arg);
214                         t->tm_year += yearset;
215                 } else {
216                         yearset = ATOI2(arg);
217                         if (yearset < 69)
218                                 t->tm_year = yearset + 2000;
219                         else
220                                 t->tm_year = yearset + 1900;
221                 }
222                 t->tm_year -= 1900;     /* Convert to UNIX time. */
223                 /* FALLTHROUGH */
224         case 8:                         /* MMDDhhmm */
225                 t->tm_mon = ATOI2(arg);
226                 --t->tm_mon;            /* Convert from 01-12 to 00-11 */
227                 t->tm_mday = ATOI2(arg);
228                 t->tm_hour = ATOI2(arg);
229                 t->tm_min = ATOI2(arg);
230                 break;
231         default:
232                 goto terr;
233         }
234
235         t->tm_isdst = -1;               /* Figure out DST. */
236         tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
237         if (tvp[0].tv_sec == -1)
238 terr:           errx(1,
239         "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
240
241         tvp[0].tv_usec = tvp[1].tv_usec = 0;
242 }
243
244 void
245 stime_arg2(arg, year, tvp)
246         char *arg;
247         int year;
248         struct timeval *tvp;
249 {
250         time_t now;
251         struct tm *t;
252                                         /* Start with the current time. */
253         now = tvp[0].tv_sec;
254         if ((t = localtime(&now)) == NULL)
255                 err(1, "localtime");
256
257         t->tm_mon = ATOI2(arg);         /* MMDDhhmm[yy] */
258         --t->tm_mon;                    /* Convert from 01-12 to 00-11 */
259         t->tm_mday = ATOI2(arg);
260         t->tm_hour = ATOI2(arg);
261         t->tm_min = ATOI2(arg);
262         if (year) {
263                 t->tm_year = ATOI2(arg);
264                 if (t->tm_year < 39)    /* support 2000-2038 not 1902-1969 */
265                         t->tm_year += 100;
266         }
267
268         t->tm_isdst = -1;               /* Figure out DST. */
269         tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
270         if (tvp[0].tv_sec == -1)
271                 errx(1,
272         "out of range or illegal time specification: MMDDhhmm[yy]");
273
274         tvp[0].tv_usec = tvp[1].tv_usec = 0;
275 }
276
277 void
278 stime_file(fname, tvp)
279         char *fname;
280         struct timeval *tvp;
281 {
282         struct stat sb;
283
284         if (stat(fname, &sb))
285                 err(1, "%s", fname);
286         TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
287         TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
288 }
289
290 int
291 rw(fname, sbp, force)
292         char *fname;
293         struct stat *sbp;
294         int force;
295 {
296         int fd, needed_chmod, rval;
297         u_char byte;
298
299         /* Try regular files. */
300         if (!S_ISREG(sbp->st_mode)) {
301                 warnx("%s: %s", fname, strerror(EFTYPE));
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;
311                 needed_chmod = 1;
312         }
313
314         if (sbp->st_size != 0) {
315                 if (read(fd, &byte, sizeof(byte)) != sizeof(byte))
316                         goto err;
317                 if (lseek(fd, (off_t)0, SEEK_SET) == -1)
318                         goto err;
319                 if (write(fd, &byte, sizeof(byte)) != sizeof(byte))
320                         goto err;
321         } else {
322                 if (write(fd, &byte, sizeof(byte)) != sizeof(byte)) {
323 err:                    rval = 1;
324                         warn("%s", fname);
325                 } else if (ftruncate(fd, (off_t)0)) {
326                         rval = 1;
327                         warn("%s: file modified", fname);
328                 }
329         }
330
331         if (close(fd) && rval != 1) {
332                 rval = 1;
333                 warn("%s", fname);
334         }
335         if (needed_chmod && chmod(fname, sbp->st_mode) && rval != 1) {
336                 rval = 1;
337                 warn("%s: permissions modified", fname);
338         }
339         return (rval);
340 }
341
342 void
343 usage()
344 {
345         (void)fprintf(stderr, "usage: touch [-acfm] [-r file] [-t [[CC]YY]MMDDhhmm[.SS]] file ...\n");
346         exit(1);
347 }