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

Annotation of src/usr.bin/rcs/rcsclean.c, Revision 1.55

1.55    ! nicm        1: /*     $OpenBSD: rcsclean.c,v 1.54 2015/01/16 06:40:11 deraadt Exp $   */
1.1       joris       2: /*
                      3:  * Copyright (c) 2005 Joris Vink <joris@openbsd.org>
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  *
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. The name of the author may not be used to endorse or promote products
                     13:  *    derived from this software without specific prior written permission.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     16:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     17:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
                     18:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     19:  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     20:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     21:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     22:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     23:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     24:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     25:  */
                     26:
1.48      xsa        27: #include <sys/types.h>
                     28:
                     29: #include <dirent.h>
                     30: #include <err.h>
                     31: #include <stdio.h>
                     32: #include <stdlib.h>
                     33: #include <unistd.h>
1.1       joris      34:
1.23      xsa        35: #include "rcsprog.h"
1.4       niallo     36: #include "diff.h"
1.1       joris      37:
1.31      ray        38: static void    rcsclean_file(char *, const char *);
1.28      xsa        39:
1.3       joris      40: static int nflag = 0;
                     41: static int kflag = RCS_KWEXP_ERR;
1.11      xsa        42: static int uflag = 0;
1.17      xsa        43: static int flags = 0;
1.19      joris      44: static char *locker = NULL;
1.1       joris      45:
                     46: int
                     47: rcsclean_main(int argc, char **argv)
                     48: {
1.17      xsa        49:        int i, ch;
1.30      ray        50:        char *rev_str;
1.1       joris      51:        DIR *dirp;
                     52:        struct dirent *dp;
                     53:
1.30      ray        54:        rev_str = NULL;
1.1       joris      55:
1.42      ray        56:        while ((ch = rcs_getopt(argc, argv, "k:n::q::r::Tu::Vx::")) != -1) {
1.1       joris      57:                switch (ch) {
1.3       joris      58:                case 'k':
1.7       joris      59:                        kflag = rcs_kflag_get(rcs_optarg);
1.3       joris      60:                        if (RCS_KWEXP_INVAL(kflag)) {
1.40      xsa        61:                                warnx("invalid RCS keyword substitution mode");
1.3       joris      62:                                (usage)();
                     63:                        }
                     64:                        break;
                     65:                case 'n':
1.30      ray        66:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.3       joris      67:                        nflag = 1;
                     68:                        break;
1.1       joris      69:                case 'q':
1.30      ray        70:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.37      xsa        71:                        flags |= QUIET;
1.1       joris      72:                        break;
1.2       joris      73:                case 'r':
1.30      ray        74:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.16      xsa        75:                        break;
                     76:                case 'T':
                     77:                        flags |= PRESERVETIME;
1.2       joris      78:                        break;
1.11      xsa        79:                case 'u':
1.30      ray        80:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.11      xsa        81:                        uflag = 1;
                     82:                        break;
1.1       joris      83:                case 'V':
                     84:                        printf("%s\n", rcs_version);
                     85:                        exit(0);
1.15      xsa        86:                case 'x':
1.27      ray        87:                        /* Use blank extension if none given. */
                     88:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.15      xsa        89:                        break;
1.1       joris      90:                default:
1.35      ray        91:                        (usage)();
1.1       joris      92:                }
                     93:        }
                     94:
1.7       joris      95:        argc -= rcs_optind;
                     96:        argv += rcs_optind;
1.1       joris      97:
1.19      joris      98:        if ((locker = getlogin()) == NULL)
1.43      xsa        99:                err(1, "getlogin");
1.19      joris     100:
1.1       joris     101:        if (argc == 0) {
                    102:                if ((dirp = opendir(".")) == NULL) {
1.38      xsa       103:                        warn("opendir");
1.1       joris     104:                        (usage)();
                    105:                }
                    106:
                    107:                while ((dp = readdir(dirp)) != NULL) {
                    108:                        if (dp->d_type == DT_DIR)
                    109:                                continue;
1.31      ray       110:                        rcsclean_file(dp->d_name, rev_str);
1.1       joris     111:                }
                    112:
1.38      xsa       113:                (void)closedir(dirp);
1.30      ray       114:        } else
1.31      ray       115:                for (i = 0; i < argc; i++)
                    116:                        rcsclean_file(argv[i], rev_str);
1.1       joris     117:
                    118:        return (0);
                    119: }
                    120:
1.53      otto      121: __dead void
1.1       joris     122: rcsclean_usage(void)
                    123: {
1.5       deraadt   124:        fprintf(stderr,
1.41      jmc       125:            "usage: rcsclean [-TV] [-kmode] [-n[rev]] [-q[rev]] [-r[rev]]\n"
1.49      xsa       126:            "                [-u[rev]] [-xsuffixes] [-ztz] [file ...]\n");
1.53      otto      127:
                    128:        exit(1);
1.1       joris     129: }
                    130:
1.28      xsa       131: static void
1.31      ray       132: rcsclean_file(char *fname, const char *rev_str)
1.1       joris     133: {
1.45      joris     134:        int fd, match;
1.1       joris     135:        RCSFILE *file;
1.54      deraadt   136:        char fpath[PATH_MAX], numb[RCS_REV_BUFSZ];
1.31      ray       137:        RCSNUM *rev;
1.1       joris     138:        BUF *b1, *b2;
1.17      xsa       139:        time_t rcs_mtime = -1;
1.1       joris     140:
1.34      ray       141:        b1 = b2 = NULL;
                    142:        file = NULL;
                    143:        rev = NULL;
1.3       joris     144:
1.47      ray       145:        if ((fd = rcs_choosefile(fname, fpath, sizeof(fpath))) < 0)
1.34      ray       146:                goto out;
1.1       joris     147:
1.45      joris     148:        if ((file = rcs_open(fpath, fd, RCS_RDWR)) == NULL)
1.34      ray       149:                goto out;
1.1       joris     150:
1.17      xsa       151:        if (flags & PRESERVETIME)
1.45      joris     152:                rcs_mtime = rcs_get_mtime(file);
1.17      xsa       153:
1.29      xsa       154:        rcs_kwexp_set(file, kflag);
1.3       joris     155:
1.31      ray       156:        if (rev_str == NULL)
                    157:                rev = file->rf_head;
                    158:        else if ((rev = rcs_getrevnum(rev_str, file)) == NULL) {
1.38      xsa       159:                warnx("%s: Symbolic name `%s' is undefined.", fpath, rev_str);
1.34      ray       160:                goto out;
1.31      ray       161:        }
1.1       joris     162:
1.31      ray       163:        if ((b1 = rcs_getrev(file, rev)) == NULL) {
1.38      xsa       164:                warnx("failed to get needed revision");
1.34      ray       165:                goto out;
1.1       joris     166:        }
1.52      ray       167:        if ((b2 = buf_load(fname)) == NULL) {
1.38      xsa       168:                warnx("failed to load `%s'", fname);
1.34      ray       169:                goto out;
1.1       joris     170:        }
                    171:
1.34      ray       172:        /* If buffer lengths are the same, compare contents as well. */
1.51      ray       173:        if (buf_len(b1) != buf_len(b2))
1.34      ray       174:                match = 0;
                    175:        else {
                    176:                size_t len, n;
1.1       joris     177:
1.51      ray       178:                len = buf_len(b1);
1.1       joris     179:
1.34      ray       180:                match = 1;
                    181:                for (n = 0; n < len; ++n)
1.51      ray       182:                        if (buf_getc(b1, n) != buf_getc(b2, n)) {
1.34      ray       183:                                match = 0;
                    184:                                break;
                    185:                        }
                    186:        }
1.1       joris     187:
1.9       xsa       188:        if (match == 1) {
1.33      deraadt   189:                if (uflag == 1 && !TAILQ_EMPTY(&(file->rf_locks))) {
1.37      xsa       190:                        if (!(flags & QUIET) && nflag == 0) {
1.11      xsa       191:                                printf("rcs -u%s %s\n",
1.31      ray       192:                                    rcsnum_tostr(rev, numb, sizeof(numb)),
1.11      xsa       193:                                    fpath);
                    194:                        }
1.31      ray       195:                        (void)rcs_lock_remove(file, locker, rev);
1.11      xsa       196:                }
                    197:
1.14      xsa       198:                if (TAILQ_EMPTY(&(file->rf_locks))) {
1.37      xsa       199:                        if (!(flags & QUIET))
1.14      xsa       200:                                printf("rm -f %s\n", fname);
1.11      xsa       201:
1.14      xsa       202:                        if (nflag == 0)
                    203:                                (void)unlink(fname);
                    204:                }
1.1       joris     205:        }
                    206:
1.45      joris     207:        rcs_write(file);
1.17      xsa       208:        if (flags & PRESERVETIME)
1.45      joris     209:                rcs_set_mtime(file, rcs_mtime);
1.34      ray       210:
                    211: out:
1.55    ! nicm      212:        buf_free(b1);
        !           213:        buf_free(b2);
1.34      ray       214:        if (file != NULL)
                    215:                rcs_close(file);
1.1       joris     216: }