[BACK]Return to touch.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / touch

Annotation of src/usr.bin/touch/touch.c, Revision 1.23

1.23    ! millert     1: /*     $OpenBSD: touch.c,v 1.22 2015/03/15 00:41:28 millert Exp $      */
1.1       deraadt     2: /*     $NetBSD: touch.c,v 1.11 1995/08/31 22:10:06 jtc Exp $   */
                      3:
                      4: /*
                      5:  * Copyright (c) 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      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.
1.9       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     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.
                     31:  */
                     32:
                     33: #include <sys/types.h>
                     34: #include <sys/stat.h>
                     35: #include <sys/time.h>
                     36:
1.15      millert    37: #include <ctype.h>
1.1       deraadt    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 <unistd.h>
                     47:
1.18      guenther   48: void           stime_arg1(char *, struct timespec *);
                     49: void           stime_arg2(char *, int, struct timespec *);
                     50: void           stime_argd(char *, struct timespec *);
                     51: void           stime_file(char *, struct timespec *);
1.13      henning    52: __dead void    usage(void);
1.1       deraadt    53:
                     54: int
1.10      deraadt    55: main(int argc, char *argv[])
1.1       deraadt    56: {
1.18      guenther   57:        struct timespec  ts[2];
1.14      henning    58:        int              aflag, cflag, mflag, ch, fd, len, rval, timeset;
                     59:        char            *p;
1.1       deraadt    60:
1.13      henning    61:        (void)setlocale(LC_ALL, "");
1.1       deraadt    62:
1.11      otto       63:        aflag = cflag = mflag = timeset = 0;
1.18      guenther   64:        while ((ch = getopt(argc, argv, "acd:fmr:t:")) != -1)
1.12      deraadt    65:                switch (ch) {
1.1       deraadt    66:                case 'a':
                     67:                        aflag = 1;
                     68:                        break;
                     69:                case 'c':
                     70:                        cflag = 1;
                     71:                        break;
1.18      guenther   72:                case 'd':
                     73:                        timeset = 1;
                     74:                        stime_argd(optarg, ts);
                     75:                        break;
1.1       deraadt    76:                case 'f':
                     77:                        break;
                     78:                case 'm':
                     79:                        mflag = 1;
                     80:                        break;
                     81:                case 'r':
                     82:                        timeset = 1;
1.18      guenther   83:                        stime_file(optarg, ts);
1.1       deraadt    84:                        break;
                     85:                case 't':
                     86:                        timeset = 1;
1.18      guenther   87:                        stime_arg1(optarg, ts);
1.1       deraadt    88:                        break;
                     89:                default:
                     90:                        usage();
                     91:                }
                     92:        argc -= optind;
                     93:        argv += optind;
                     94:
                     95:        /* Default is both -a and -m. */
                     96:        if (aflag == 0 && mflag == 0)
                     97:                aflag = mflag = 1;
                     98:
                     99:        /*
                    100:         * If no -r or -t flag, at least two operands, the first of which
                    101:         * is an 8 or 10 digit number, use the obsolete time specification.
                    102:         */
                    103:        if (!timeset && argc > 1) {
                    104:                (void)strtol(argv[0], &p, 10);
                    105:                len = p - argv[0];
                    106:                if (*p == '\0' && (len == 8 || len == 10)) {
                    107:                        timeset = 1;
1.18      guenther  108:                        stime_arg2(*argv++, len == 10, ts);
1.1       deraadt   109:                }
                    110:        }
                    111:
                    112:        /* Otherwise use the current time of day. */
                    113:        if (!timeset)
1.18      guenther  114:                ts[0].tv_nsec = ts[1].tv_nsec = UTIME_NOW;
                    115:
                    116:        if (!aflag)
                    117:                ts[0].tv_nsec = UTIME_OMIT;
                    118:        if (!mflag)
                    119:                ts[1].tv_nsec = UTIME_OMIT;
1.1       deraadt   120:
                    121:        if (*argv == NULL)
                    122:                usage();
                    123:
                    124:        for (rval = 0; *argv; ++argv) {
1.18      guenther  125:                /* Update the file's timestamp if it exists. */
                    126:                if (! utimensat(AT_FDCWD, *argv, ts, 0))
                    127:                        continue;
                    128:                if (errno != ENOENT) {
                    129:                        rval = 1;
                    130:                        warn("%s", *argv);
                    131:                        continue;
1.4       pjanzen   132:                }
1.1       deraadt   133:
1.18      guenther  134:                /* Didn't exist; should we create it? */
                    135:                if (cflag)
1.1       deraadt   136:                        continue;
                    137:
1.18      guenther  138:                /* Create the file. */
                    139:                fd = open(*argv, O_WRONLY | O_CREAT, DEFFILEMODE);
                    140:                if (fd == -1 || futimens(fd, ts) || close(fd)) {
1.1       deraadt   141:                        rval = 1;
                    142:                        warn("%s", *argv);
                    143:                }
                    144:        }
                    145:        exit(rval);
                    146: }
                    147:
1.6       pjanzen   148: #define        ATOI2(s)        ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
1.1       deraadt   149:
                    150: void
1.18      guenther  151: stime_arg1(char *arg, struct timespec *tsp)
1.1       deraadt   152: {
1.15      millert   153:        struct tm       *lt;
1.13      henning   154:        time_t           tmptime;
                    155:        int              yearset;
1.15      millert   156:        char            *dot, *p;
1.1       deraadt   157:                                        /* Start with the current time. */
1.19      guenther  158:        tmptime = time(NULL);
1.15      millert   159:        if ((lt = localtime(&tmptime)) == NULL)
1.1       deraadt   160:                err(1, "localtime");
                    161:                                        /* [[CC]YY]MMDDhhmm[.SS] */
1.15      millert   162:        for (p = arg, dot = NULL; *p != '\0'; p++) {
1.16      millert   163:                if (*p == '.' && dot == NULL)
1.15      millert   164:                        dot = p;
                    165:                else if (!isdigit((unsigned char)*p))
                    166:                        goto terr;
                    167:        }
                    168:        if (dot == NULL)
                    169:                lt->tm_sec = 0;         /* Seconds defaults to 0. */
1.1       deraadt   170:        else {
1.15      millert   171:                *dot++ = '\0';
                    172:                if (strlen(dot) != 2)
                    173:                        goto terr;
                    174:                lt->tm_sec = ATOI2(dot);
                    175:                if (lt->tm_sec > 61)    /* Could be leap second. */
1.1       deraadt   176:                        goto terr;
                    177:        }
1.12      deraadt   178:
1.1       deraadt   179:        yearset = 0;
1.12      deraadt   180:        switch (strlen(arg)) {
1.1       deraadt   181:        case 12:                        /* CCYYMMDDhhmm */
1.23    ! millert   182:                lt->tm_year = (ATOI2(arg) * 100) - 1900;
1.1       deraadt   183:                yearset = 1;
1.8       henning   184:                /* FALLTHROUGH */
1.1       deraadt   185:        case 10:                        /* YYMMDDhhmm */
                    186:                if (yearset) {
                    187:                        yearset = ATOI2(arg);
1.15      millert   188:                        lt->tm_year += yearset;
1.1       deraadt   189:                } else {
                    190:                        yearset = ATOI2(arg);
1.20      guenther  191:                        /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */
1.23    ! millert   192:                        lt->tm_year = yearset;
1.20      guenther  193:                        if (yearset < 69)
                    194:                                lt->tm_year += 100;
1.1       deraadt   195:                }
                    196:                /* FALLTHROUGH */
                    197:        case 8:                         /* MMDDhhmm */
1.15      millert   198:                lt->tm_mon = ATOI2(arg);
                    199:                if (lt->tm_mon > 12 || lt->tm_mon == 0)
                    200:                        goto terr;
                    201:                --lt->tm_mon;           /* Convert from 01-12 to 00-11 */
                    202:                lt->tm_mday = ATOI2(arg);
                    203:                if (lt->tm_mday > 31 || lt->tm_mday == 0)
                    204:                        goto terr;
                    205:                lt->tm_hour = ATOI2(arg);
                    206:                if (lt->tm_hour > 23)
                    207:                        goto terr;
                    208:                lt->tm_min = ATOI2(arg);
                    209:                if (lt->tm_min > 59)
                    210:                        goto terr;
1.1       deraadt   211:                break;
                    212:        default:
                    213:                goto terr;
                    214:        }
                    215:
1.15      millert   216:        lt->tm_isdst = -1;              /* Figure out DST. */
1.18      guenther  217:        tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt);
                    218:        if (tsp[0].tv_sec == -1)
1.1       deraadt   219: terr:          errx(1,
                    220:        "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
                    221:
1.18      guenther  222:        tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
1.1       deraadt   223: }
                    224:
                    225: void
1.18      guenther  226: stime_arg2(char *arg, int year, struct timespec *tsp)
1.1       deraadt   227: {
1.15      millert   228:        struct tm       *lt;
1.13      henning   229:        time_t           tmptime;
1.1       deraadt   230:                                        /* Start with the current time. */
1.19      guenther  231:        tmptime = time(NULL);
1.15      millert   232:        if ((lt = localtime(&tmptime)) == NULL)
1.1       deraadt   233:                err(1, "localtime");
                    234:
1.15      millert   235:        lt->tm_mon = ATOI2(arg);        /* MMDDhhmm[YY] */
                    236:        if (lt->tm_mon > 12 || lt->tm_mon == 0)
                    237:                goto terr;
                    238:        --lt->tm_mon;                   /* Convert from 01-12 to 00-11 */
                    239:        lt->tm_mday = ATOI2(arg);
                    240:        if (lt->tm_mday > 31 || lt->tm_mday == 0)
                    241:                goto terr;
                    242:        lt->tm_hour = ATOI2(arg);
                    243:        if (lt->tm_hour > 23)
                    244:                goto terr;
                    245:        lt->tm_min = ATOI2(arg);
                    246:        if (lt->tm_min > 59)
                    247:                goto terr;
1.4       pjanzen   248:        if (year) {
1.20      guenther  249:                year = ATOI2(arg);
                    250:                /* POSIX logic: [00,68]=>20xx, [69,99]=>19xx */
1.23    ! millert   251:                lt->tm_year = year;
1.20      guenther  252:                if (year < 69)
                    253:                        lt->tm_year += 100;
1.4       pjanzen   254:        }
1.15      millert   255:        lt->tm_sec = 0;
1.1       deraadt   256:
1.15      millert   257:        lt->tm_isdst = -1;              /* Figure out DST. */
1.18      guenther  258:        tsp[0].tv_sec = tsp[1].tv_sec = mktime(lt);
                    259:        if (tsp[0].tv_sec == -1)
1.15      millert   260: terr:          errx(1,
1.4       pjanzen   261:        "out of range or illegal time specification: MMDDhhmm[YY]");
1.1       deraadt   262:
1.18      guenther  263:        tsp[0].tv_nsec = tsp[1].tv_nsec = 0;
1.1       deraadt   264: }
                    265:
                    266: void
1.18      guenther  267: stime_file(char *fname, struct timespec *tsp)
1.1       deraadt   268: {
1.13      henning   269:        struct stat     sb;
1.1       deraadt   270:
                    271:        if (stat(fname, &sb))
                    272:                err(1, "%s", fname);
1.18      guenther  273:        tsp[0] = sb.st_atim;
                    274:        tsp[1] = sb.st_mtim;
                    275: }
                    276:
                    277: void
                    278: stime_argd(char *arg, struct timespec *tsp)
                    279: {
                    280:        struct tm       tm;
                    281:        char            *frac, *p;
                    282:        int             utc = 0;
                    283:
                    284:        /* accept YYYY-MM-DD(T| )hh:mm:ss[(.|,)frac][Z] */
                    285:        memset(&tm, 0, sizeof(tm));
                    286:        p = strptime(arg, "%F", &tm);
                    287:        if (p == NULL || (*p != 'T' && *p != ' '))
                    288:                goto terr;
                    289:        p = strptime(p + 1, "%T", &tm);
                    290:        if (p == NULL)
                    291:                goto terr;
                    292:        tsp[0].tv_nsec = 0;
                    293:        if (*p == '.' || *p == ',') {
                    294:                frac = ++p;
                    295:                while (isdigit((unsigned char)*p)) {
                    296:                        if (p - frac < 9) {
                    297:                                tsp[0].tv_nsec = tsp[0].tv_nsec * 10 +
                    298:                                    *p - '0';
                    299:                        }
                    300:                        p++;
                    301:                }
                    302:                if (p == frac)
                    303:                        goto terr;
                    304:
                    305:                /* fill in the trailing zeros */
                    306:                while (p - frac-- < 9)
                    307:                        tsp[0].tv_nsec *= 10;
                    308:        }
                    309:        if (*p == 'Z') {
                    310:                utc = 1;
                    311:                p++;
                    312:        }
                    313:        if (*p != '\0')
                    314:                goto terr;
                    315:
                    316:        tm.tm_isdst = -1;
                    317:        tsp[0].tv_sec = utc ? timegm(&tm) : mktime(&tm);
                    318:        if (tsp[0].tv_sec == -1)
                    319: terr:          errx(1,
                    320:   "out of range or illegal time specification: YYYY-MM-DDThh:mm:ss[.frac][Z]");
                    321:        tsp[1] = tsp[0];
1.1       deraadt   322: }
                    323:
                    324: __dead void
1.10      deraadt   325: usage(void)
1.1       deraadt   326: {
                    327:        (void)fprintf(stderr,
1.21      jmc       328: "usage: touch [-acm] [-d ccyy-mm-ddTHH:MM:SS[.frac][Z]] [-r file]\n"
                    329: "             [-t [[cc]yy]mmddHHMM[.SS]] file ...\n");
1.1       deraadt   330:        exit(1);
                    331: }