[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.14

1.14    ! henning     1: /*     $OpenBSD: touch.c,v 1.13 2006/03/07 11:49:40 henning 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:
                     37: #include <err.h>
                     38: #include <errno.h>
                     39: #include <fcntl.h>
                     40: #include <stdio.h>
                     41: #include <stdlib.h>
                     42: #include <string.h>
                     43: #include <locale.h>
                     44: #include <time.h>
1.5       pjanzen    45: #include <tzfile.h>
1.1       deraadt    46: #include <unistd.h>
                     47:
1.13      henning    48: void           stime_arg1(char *, struct timeval *);
                     49: void           stime_arg2(char *, int, struct timeval *);
                     50: void           stime_file(char *, struct timeval *);
                     51: __dead void    usage(void);
1.1       deraadt    52:
                     53: int
1.10      deraadt    54: main(int argc, char *argv[])
1.1       deraadt    55: {
1.14    ! henning    56:        struct stat      sb;
        !            57:        struct timeval   tv[2];
        !            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.1       deraadt    64:        if (gettimeofday(&tv[0], NULL))
                     65:                err(1, "gettimeofday");
                     66:
1.3       millert    67:        while ((ch = getopt(argc, argv, "acfmr:t:")) != -1)
1.12      deraadt    68:                switch (ch) {
1.1       deraadt    69:                case 'a':
                     70:                        aflag = 1;
                     71:                        break;
                     72:                case 'c':
                     73:                        cflag = 1;
                     74:                        break;
                     75:                case 'f':
                     76:                        break;
                     77:                case 'm':
                     78:                        mflag = 1;
                     79:                        break;
                     80:                case 'r':
                     81:                        timeset = 1;
                     82:                        stime_file(optarg, tv);
                     83:                        break;
                     84:                case 't':
                     85:                        timeset = 1;
                     86:                        stime_arg1(optarg, tv);
                     87:                        break;
                     88:                default:
                     89:                        usage();
                     90:                }
                     91:        argc -= optind;
                     92:        argv += optind;
                     93:
                     94:        /* Default is both -a and -m. */
                     95:        if (aflag == 0 && mflag == 0)
                     96:                aflag = mflag = 1;
                     97:
                     98:        /*
                     99:         * If no -r or -t flag, at least two operands, the first of which
                    100:         * is an 8 or 10 digit number, use the obsolete time specification.
                    101:         */
                    102:        if (!timeset && argc > 1) {
                    103:                (void)strtol(argv[0], &p, 10);
                    104:                len = p - argv[0];
                    105:                if (*p == '\0' && (len == 8 || len == 10)) {
                    106:                        timeset = 1;
                    107:                        stime_arg2(*argv++, len == 10, tv);
                    108:                }
                    109:        }
                    110:
                    111:        /* Otherwise use the current time of day. */
                    112:        if (!timeset)
                    113:                tv[1] = tv[0];
                    114:
                    115:        if (*argv == NULL)
                    116:                usage();
                    117:
                    118:        for (rval = 0; *argv; ++argv) {
                    119:                /* See if the file exists. */
1.4       pjanzen   120:                if (stat(*argv, &sb)) {
1.1       deraadt   121:                        if (!cflag) {
                    122:                                /* Create the file. */
                    123:                                fd = open(*argv,
                    124:                                    O_WRONLY | O_CREAT, DEFFILEMODE);
                    125:                                if (fd == -1 || fstat(fd, &sb) || close(fd)) {
                    126:                                        rval = 1;
                    127:                                        warn("%s", *argv);
                    128:                                        continue;
                    129:                                }
                    130:
                    131:                                /* If using the current time, we're done. */
                    132:                                if (!timeset)
                    133:                                        continue;
                    134:                        } else
                    135:                                continue;
1.4       pjanzen   136:                }
1.1       deraadt   137:
                    138:                if (!aflag)
                    139:                        TIMESPEC_TO_TIMEVAL(&tv[0], &sb.st_atimespec);
                    140:                if (!mflag)
                    141:                        TIMESPEC_TO_TIMEVAL(&tv[1], &sb.st_mtimespec);
                    142:
                    143:                /* Try utimes(2). */
                    144:                if (!utimes(*argv, tv))
                    145:                        continue;
                    146:
                    147:                /* If the user specified a time, nothing else we can do. */
                    148:                if (timeset) {
                    149:                        rval = 1;
                    150:                        warn("%s", *argv);
                    151:                }
                    152:
                    153:                /*
                    154:                 * System V and POSIX 1003.1 require that a NULL argument
                    155:                 * set the access/modification times to the current time.
                    156:                 * The permission checks are different, too, in that the
                    157:                 * ability to write the file is sufficient.  Take a shot.
                    158:                 */
                    159:                 if (!utimes(*argv, NULL))
                    160:                        continue;
                    161:
1.11      otto      162:                rval = 1;
                    163:                warn("%s", *argv);
1.1       deraadt   164:        }
                    165:        exit(rval);
                    166: }
                    167:
1.6       pjanzen   168: #define        ATOI2(s)        ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
1.1       deraadt   169:
                    170: void
1.10      deraadt   171: stime_arg1(char *arg, struct timeval *tvp)
1.1       deraadt   172: {
1.13      henning   173:        struct tm       *t;
                    174:        time_t           tmptime;
                    175:        int              yearset;
                    176:        char            *p;
1.1       deraadt   177:                                        /* Start with the current time. */
                    178:        tmptime = tvp[0].tv_sec;
                    179:        if ((t = localtime(&tmptime)) == NULL)
                    180:                err(1, "localtime");
                    181:                                        /* [[CC]YY]MMDDhhmm[.SS] */
                    182:        if ((p = strchr(arg, '.')) == NULL)
                    183:                t->tm_sec = 0;          /* Seconds defaults to 0. */
                    184:        else {
                    185:                if (strlen(p + 1) != 2)
                    186:                        goto terr;
                    187:                *p++ = '\0';
                    188:                t->tm_sec = ATOI2(p);
                    189:        }
1.12      deraadt   190:
1.1       deraadt   191:        yearset = 0;
1.12      deraadt   192:        switch (strlen(arg)) {
1.1       deraadt   193:        case 12:                        /* CCYYMMDDhhmm */
1.6       pjanzen   194:                t->tm_year = ATOI2(arg) * 100 - TM_YEAR_BASE;
1.1       deraadt   195:                yearset = 1;
1.8       henning   196:                /* FALLTHROUGH */
1.1       deraadt   197:        case 10:                        /* YYMMDDhhmm */
                    198:                if (yearset) {
                    199:                        yearset = ATOI2(arg);
                    200:                        t->tm_year += yearset;
                    201:                } else {
                    202:                        yearset = ATOI2(arg);
                    203:                        if (yearset < 69)
1.5       pjanzen   204:                                t->tm_year = yearset + 2000 - TM_YEAR_BASE;
1.1       deraadt   205:                        else
1.5       pjanzen   206:                                t->tm_year = yearset + 1900 - TM_YEAR_BASE;
1.1       deraadt   207:                }
                    208:                /* FALLTHROUGH */
                    209:        case 8:                         /* MMDDhhmm */
                    210:                t->tm_mon = ATOI2(arg);
                    211:                --t->tm_mon;            /* Convert from 01-12 to 00-11 */
                    212:                t->tm_mday = ATOI2(arg);
                    213:                t->tm_hour = ATOI2(arg);
                    214:                t->tm_min = ATOI2(arg);
                    215:                break;
                    216:        default:
                    217:                goto terr;
                    218:        }
                    219:
                    220:        t->tm_isdst = -1;               /* Figure out DST. */
                    221:        tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
                    222:        if (tvp[0].tv_sec == -1)
                    223: terr:          errx(1,
                    224:        "out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]");
                    225:
                    226:        tvp[0].tv_usec = tvp[1].tv_usec = 0;
                    227: }
                    228:
                    229: void
1.10      deraadt   230: stime_arg2(char *arg, int year, struct timeval *tvp)
1.1       deraadt   231: {
1.13      henning   232:        struct tm       *t;
                    233:        time_t           tmptime;
1.1       deraadt   234:                                        /* Start with the current time. */
                    235:        tmptime = tvp[0].tv_sec;
                    236:        if ((t = localtime(&tmptime)) == NULL)
                    237:                err(1, "localtime");
                    238:
1.4       pjanzen   239:        t->tm_mon = ATOI2(arg);         /* MMDDhhmm[YY] */
1.1       deraadt   240:        --t->tm_mon;                    /* Convert from 01-12 to 00-11 */
                    241:        t->tm_mday = ATOI2(arg);
                    242:        t->tm_hour = ATOI2(arg);
                    243:        t->tm_min = ATOI2(arg);
1.4       pjanzen   244:        if (year) {
1.5       pjanzen   245:                year = ATOI2(arg);
                    246:                if (year < 69)
                    247:                        t->tm_year = year + 2000 - TM_YEAR_BASE;
                    248:                else
                    249:                        t->tm_year = year + 1900 - TM_YEAR_BASE;
1.4       pjanzen   250:        }
1.5       pjanzen   251:        t->tm_sec = 0;
1.1       deraadt   252:
                    253:        t->tm_isdst = -1;               /* Figure out DST. */
                    254:        tvp[0].tv_sec = tvp[1].tv_sec = mktime(t);
                    255:        if (tvp[0].tv_sec == -1)
                    256:                errx(1,
1.4       pjanzen   257:        "out of range or illegal time specification: MMDDhhmm[YY]");
1.1       deraadt   258:
                    259:        tvp[0].tv_usec = tvp[1].tv_usec = 0;
                    260: }
                    261:
                    262: void
1.10      deraadt   263: stime_file(char *fname, struct timeval *tvp)
1.1       deraadt   264: {
1.13      henning   265:        struct stat     sb;
1.1       deraadt   266:
                    267:        if (stat(fname, &sb))
                    268:                err(1, "%s", fname);
                    269:        TIMESPEC_TO_TIMEVAL(tvp, &sb.st_atimespec);
                    270:        TIMESPEC_TO_TIMEVAL(tvp + 1, &sb.st_mtimespec);
                    271: }
                    272:
                    273: __dead void
1.10      deraadt   274: usage(void)
1.1       deraadt   275: {
1.13      henning   276:        extern char     *__progname;
                    277:
1.1       deraadt   278:        (void)fprintf(stderr,
1.13      henning   279:            "usage: %s [-acm] [-r file] [-t time] file ...\n", __progname);
1.1       deraadt   280:        exit(1);
                    281: }