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

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