]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/touch/touch.c
Include target names in diagnostic output.
[FreeBSD/FreeBSD.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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31
32 __FBSDID("$FreeBSD$");
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1993\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif
39
40 #ifndef lint
41 static const char sccsid[] = "@(#)touch.c       8.1 (Berkeley) 6/6/93";
42 #endif
43
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <libgen.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57
58 void    stime_arg1(char *, struct timeval *);
59 void    stime_arg2(char *, int, struct timeval *);
60 void    stime_file(char *, struct timeval *);
61 int     timeoffset(char *);
62 void    usage(char *);
63
64 int
65 main(int argc, char *argv[])
66 {
67         struct stat sb;
68         struct timeval tv[2];
69         int (*stat_f)(const char *, struct stat *);
70         int (*utimes_f)(const char *, const struct timeval *);
71         int Aflag, aflag, cflag, mflag, ch, fd, len, rval, timeset;
72         char *p;
73         char *myname;
74
75         myname = basename(argv[0]);
76         Aflag = aflag = cflag = mflag = timeset = 0;
77         stat_f = stat;
78         utimes_f = utimes;
79         if (gettimeofday(&tv[0], NULL))
80                 err(1, "gettimeofday");
81
82         while ((ch = getopt(argc, argv, "A:acfhmr:t:")) != -1)
83                 switch(ch) {
84                 case 'A':
85                         Aflag = timeoffset(optarg);
86                         break;
87                 case 'a':
88                         aflag = 1;
89                         break;
90                 case 'c':
91                         cflag = 1;
92                         break;
93                 case 'f':
94                         /* No-op for compatibility. */
95                         break;
96                 case 'h':
97                         cflag = 1;
98                         stat_f = lstat;
99                         utimes_f = lutimes;
100                         break;
101                 case 'm':
102                         mflag = 1;
103                         break;
104                 case 'r':
105                         timeset = 1;
106                         stime_file(optarg, tv);
107                         break;
108                 case 't':
109                         timeset = 1;
110                         stime_arg1(optarg, tv);
111                         break;
112                 case '?':
113                 default:
114                         usage(myname);
115                 }
116         argc -= optind;
117         argv += optind;
118
119         if (aflag == 0 && mflag == 0)
120                 aflag = mflag = 1;
121
122         if (timeset) {
123                 if (Aflag) {
124                         /*
125                          * We're setting the time to an offset from a specified
126                          * time.  God knows why, but it means that we can set
127                          * that time once and for all here.
128                          */
129                         if (aflag)
130                                 tv[0].tv_sec += Aflag;
131                         if (mflag)
132                                 tv[1].tv_sec += Aflag;
133                         Aflag = 0;              /* done our job */
134                 }
135         } else {
136                 /*
137                  * If no -r or -t flag, at least two operands, the first of
138                  * which is an 8 or 10 digit number, use the obsolete time
139                  * specification, otherwise use the current time.
140                  */
141                 if (argc > 1) {
142                         strtol(argv[0], &p, 10);
143                         len = p - argv[0];
144                         if (*p == '\0' && (len == 8 || len == 10)) {
145                                 timeset = 1;
146                                 stime_arg2(*argv++, len == 10, tv);
147                         }
148                 }
149                 /* Both times default to the same. */
150                 tv[1] = tv[0];
151         }
152
153         if (*argv == NULL)
154                 usage(myname);
155
156         if (Aflag)
157                 cflag = 1;
158
159         for (rval = 0; *argv; ++argv) {
160                 /* See if the file exists. */
161                 if (stat_f(*argv, &sb) != 0) {
162                         if (errno != ENOENT) {
163                                 rval = 1;
164                                 warn("%s", *argv);
165                                 continue;
166                         }
167                         if (!cflag) {
168                                 /* Create the file. */
169                                 fd = open(*argv,
170                                     O_WRONLY | O_CREAT, DEFFILEMODE);
171                                 if (fd == -1 || fstat(fd, &sb) || close(fd)) {
172                                         rval = 1;
173                                         warn("%s", *argv);
174                                         continue;
175                                 }
176
177                                 /* If using the current time, we're done. */
178                                 if (!timeset)
179                                         continue;
180                         } else
181                                 continue;
182                 }
183
184                 if (!aflag)
185                         TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atim);
186                 if (!mflag)
187                         TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtim);
188
189                 /*
190                  * We're adjusting the times based on the file times, not a
191                  * specified time (that gets handled above).
192                  */
193                 if (Aflag) {
194                         if (aflag) {
195                                 TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atim);
196                                 tv[0].tv_sec += Aflag;
197                         }
198                         if (mflag) {
199                                 TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtim);
200                                 tv[1].tv_sec += Aflag;
201                         }
202                 }
203
204                 /* Try utimes(2). */
205                 if (!utimes_f(*argv, tv))
206                         continue;
207
208                 /* If the user specified a time, nothing else we can do. */
209                 if (timeset || Aflag) {
210                         rval = 1;
211                         warn("%s", *argv);
212                         continue;
213                 }
214
215                 /*
216                  * System V and POSIX 1003.1 require that a NULL argument
217                  * set the access/modification times to the current time.
218                  * The permission checks are different, too, in that the
219                  * ability to write the file is sufficient.  Take a shot.
220                  */
221                  if (!utimes_f(*argv, NULL))
222                         continue;
223
224                 rval = 1;
225                 warn("%s", *argv);
226         }
227         exit(rval);
228 }
229
230 #define ATOI2(ar)       ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
231
232 void
233 stime_arg1(char *arg, struct timeval *tvp)
234 {
235         time_t now;
236         struct tm *t;
237         int yearset;
238         char *p;
239                                         /* Start with the current time. */
240         now = tvp[0].tv_sec;
241         if ((t = localtime(&now)) == NULL)
242                 err(1, "localtime");
243                                         /* [[CC]YY]MMDDhhmm[.SS] */
244         if ((p = strchr(arg, '.')) == NULL)
245                 t->tm_sec = 0;          /* Seconds defaults to 0. */
246         else {
247                 if (strlen(p + 1) != 2)
248                         goto terr;
249                 *p++ = '\0';
250                 t->tm_sec = ATOI2(p);
251         }
252
253         yearset = 0;
254         switch(strlen(arg)) {
255         case 12:                        /* CCYYMMDDhhmm */
256                 t->tm_year = ATOI2(arg);
257                 t->tm_year *= 100;
258                 yearset = 1;
259                 /* FALLTHROUGH */
260         case 10:                        /* YYMMDDhhmm */
261                 if (yearset) {
262                         yearset = ATOI2(arg);
263                         t->tm_year += yearset;
264                 } else {
265                         yearset = ATOI2(arg);
266                         if (yearset < 69)
267                                 t->tm_year = yearset + 2000;
268                         else
269                                 t->tm_year = yearset + 1900;
270                 }
271                 t->tm_year -= 1900;     /* Convert to UNIX time. */
272                 /* FALLTHROUGH */
273         case 8:                         /* MMDDhhmm */
274                 t->tm_mon = ATOI2(arg);
275                 --t->tm_mon;            /* Convert from 01-12 to 00-11 */
276                 t->tm_mday = ATOI2(arg);
277                 t->tm_hour = ATOI2(arg);
278                 t->tm_min = ATOI2(arg);
279                 break;
280         default:
281                 goto terr;
282         }
283
284         t->tm_isdst = -1;               /* Figure out DST. */
285         tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
286         if (tvp[0].tv_sec == -1)
287 terr:           errx(1,
288         "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
289
290         tvp[0].tv_usec = tvp[1].tv_usec = 0;
291 }
292
293 void
294 stime_arg2(char *arg, int year, struct timeval *tvp)
295 {
296         time_t now;
297         struct tm *t;
298                                         /* Start with the current time. */
299         now = tvp[0].tv_sec;
300         if ((t = localtime(&now)) == NULL)
301                 err(1, "localtime");
302
303         t->tm_mon = ATOI2(arg);         /* MMDDhhmm[yy] */
304         --t->tm_mon;                    /* Convert from 01-12 to 00-11 */
305         t->tm_mday = ATOI2(arg);
306         t->tm_hour = ATOI2(arg);
307         t->tm_min = ATOI2(arg);
308         if (year) {
309                 t->tm_year = ATOI2(arg);
310                 if (t->tm_year < 39)    /* support 2000-2038 not 1902-1969 */
311                         t->tm_year += 100;
312         }
313
314         t->tm_isdst = -1;               /* Figure out DST. */
315         tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
316         if (tvp[0].tv_sec == -1)
317                 errx(1,
318         "out of range or illegal time specification: MMDDhhmm[yy]");
319
320         tvp[0].tv_usec = tvp[1].tv_usec = 0;
321 }
322
323 /* Calculate a time offset in seconds, given an arg of the format [-]HHMMSS. */
324 int
325 timeoffset(char *arg)
326 {
327         int offset;
328         int isneg;
329
330         offset = 0;
331         isneg = *arg == '-';
332         if (isneg)
333                 arg++;
334         switch (strlen(arg)) {
335         default:                                /* invalid */
336                 errx(1, "Invalid offset spec, must be [-][[HH]MM]SS");
337
338         case 6:                                 /* HHMMSS */
339                 offset = ATOI2(arg);
340                 /* FALLTHROUGH */
341         case 4:                                 /* MMSS */
342                 offset = offset * 60 + ATOI2(arg);
343                 /* FALLTHROUGH */
344         case 2:                                 /* SS */
345                 offset = offset * 60 + ATOI2(arg);
346         }
347         if (isneg)
348                 return (-offset);
349         else
350                 return (offset);
351 }
352
353 void
354 stime_file(char *fname, struct timeval *tvp)
355 {
356         struct stat sb;
357
358         if (stat(fname, &sb))
359                 err(1, "%s", fname);
360         TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atim);
361         TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtim);
362 }
363
364 void
365 usage(char *myname)
366 {
367         fprintf(stderr, "usage:\n" "%s [-A [-][[hh]mm]SS] [-achm] [-r file] "
368                 "[-t [[CC]YY]MMDDhhmm[.SS]] file ...\n", myname);
369         exit(1);
370 }