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

1.30    ! ray         1: /*     $OpenBSD: rcsclean.c,v 1.29 2006/04/10 08:08:00 xsa 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.22      xsa        27: #include "includes.h"
1.1       joris      28:
1.23      xsa        29: #include "rcsprog.h"
1.4       niallo     30: #include "diff.h"
1.1       joris      31:
1.28      xsa        32: static void    rcsclean_file(char *, RCSNUM *);
                     33:
1.3       joris      34: static int nflag = 0;
                     35: static int kflag = RCS_KWEXP_ERR;
1.11      xsa        36: static int uflag = 0;
1.17      xsa        37: static int flags = 0;
1.19      joris      38: static char *locker = NULL;
1.1       joris      39:
                     40: int
                     41: rcsclean_main(int argc, char **argv)
                     42: {
1.17      xsa        43:        int i, ch;
1.30    ! ray        44:        char *rev_str;
1.1       joris      45:        RCSNUM *rev;
                     46:        DIR *dirp;
                     47:        struct dirent *dp;
                     48:
                     49:        rev = RCS_HEAD_REV;
1.30    ! ray        50:        rev_str = NULL;
1.1       joris      51:
1.27      ray        52:        while ((ch = rcs_getopt(argc, argv, "k:n::q::r:Tu::Vx::")) != -1) {
1.1       joris      53:                switch (ch) {
1.3       joris      54:                case 'k':
1.7       joris      55:                        kflag = rcs_kflag_get(rcs_optarg);
1.3       joris      56:                        if (RCS_KWEXP_INVAL(kflag)) {
                     57:                                cvs_log(LP_ERR,
                     58:                                    "invalid RCS keyword expansion mode");
                     59:                                (usage)();
                     60:                                exit(1);
                     61:                        }
                     62:                        break;
                     63:                case 'n':
1.30    ! ray        64:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.3       joris      65:                        nflag = 1;
                     66:                        break;
1.1       joris      67:                case 'q':
1.30    ! ray        68:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.1       joris      69:                        verbose = 0;
                     70:                        break;
1.2       joris      71:                case 'r':
1.30    ! ray        72:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.16      xsa        73:                        break;
                     74:                case 'T':
                     75:                        flags |= PRESERVETIME;
1.2       joris      76:                        break;
1.11      xsa        77:                case 'u':
1.30    ! ray        78:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.11      xsa        79:                        uflag = 1;
                     80:                        break;
1.1       joris      81:                case 'V':
                     82:                        printf("%s\n", rcs_version);
                     83:                        exit(0);
1.26      ray        84:                        /* NOTREACHED */
1.15      xsa        85:                case 'x':
1.27      ray        86:                        /* Use blank extension if none given. */
                     87:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.15      xsa        88:                        break;
1.1       joris      89:                default:
                     90:                        break;
                     91:                }
                     92:        }
                     93:
1.7       joris      94:        argc -= rcs_optind;
                     95:        argv += rcs_optind;
1.1       joris      96:
1.19      joris      97:        if ((locker = getlogin()) == NULL)
1.21      xsa        98:                fatal("getlogin failed");
1.19      joris      99:
1.1       joris     100:        if (argc == 0) {
                    101:                if ((dirp = opendir(".")) == NULL) {
                    102:                        cvs_log(LP_ERRNO, "failed to open directory '.'");
                    103:                        (usage)();
                    104:                        exit(1);
                    105:                }
                    106:
                    107:                while ((dp = readdir(dirp)) != NULL) {
                    108:                        if (dp->d_type == DT_DIR)
                    109:                                continue;
1.30    ! ray       110:                        rcs_set_rev(rev_str, &rev);
1.1       joris     111:                        rcsclean_file(dp->d_name, rev);
1.30    ! ray       112:                        rcsnum_free(rev);
1.1       joris     113:                }
                    114:
                    115:                closedir(dirp);
1.30    ! ray       116:        } else
        !           117:                for (i = 0; i < argc; i++) {
        !           118:                        rcs_set_rev(rev_str, &rev);
1.1       joris     119:                        rcsclean_file(argv[i], rev);
1.30    ! ray       120:                        rcsnum_free(rev);
        !           121:                }
1.1       joris     122:
                    123:        return (0);
                    124: }
                    125:
                    126: void
                    127: rcsclean_usage(void)
                    128: {
1.5       deraadt   129:        fprintf(stderr,
1.24      jmc       130:            "usage: rcsclean [-TV] [-kmode] [-n[rev]] [-q[rev]]\n"
                    131:            "                [-rrev] [-u[rev]] [-xsuffixes] [-ztz] [file] ...\n");
1.1       joris     132: }
                    133:
1.28      xsa       134: static void
1.1       joris     135: rcsclean_file(char *fname, RCSNUM *rev)
                    136: {
                    137:        int match;
                    138:        RCSFILE *file;
1.11      xsa       139:        char fpath[MAXPATHLEN], numb[64];
1.1       joris     140:        RCSNUM *frev;
                    141:        BUF *b1, *b2;
                    142:        char *s1, *s2, *c1, *c2;
1.3       joris     143:        struct stat st;
1.17      xsa       144:        time_t rcs_mtime = -1;
1.1       joris     145:
                    146:        match = 1;
1.3       joris     147:
                    148:        if (stat(fname, &st) == -1)
1.28      xsa       149:                return;
1.3       joris     150:
1.1       joris     151:        if (rcs_statfile(fname, fpath, sizeof(fpath)) < 0)
1.28      xsa       152:                return;
1.1       joris     153:
                    154:        if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
1.28      xsa       155:                return;
1.1       joris     156:
1.17      xsa       157:        if (flags & PRESERVETIME)
                    158:                rcs_mtime = rcs_get_mtime(file->rf_path);
                    159:
1.29      xsa       160:        rcs_kwexp_set(file, kflag);
1.3       joris     161:
1.1       joris     162:        if (rev == RCS_HEAD_REV)
                    163:                frev = file->rf_head;
                    164:        else
                    165:                frev = rev;
                    166:
                    167:        if ((b1 = rcs_getrev(file, frev)) == NULL) {
                    168:                cvs_log(LP_ERR, "failed to get needed revision");
                    169:                rcs_close(file);
1.28      xsa       170:                return;
1.1       joris     171:        }
                    172:
                    173:        if ((b2 = cvs_buf_load(fname, BUF_AUTOEXT)) == NULL) {
                    174:                cvs_log(LP_ERRNO, "failed to load '%s'", fname);
                    175:                rcs_close(file);
1.28      xsa       176:                return;
1.1       joris     177:        }
                    178:
                    179:        cvs_buf_putc(b1, '\0');
                    180:        cvs_buf_putc(b2, '\0');
                    181:
                    182:        c1 = cvs_buf_release(b1);
                    183:        c2 = cvs_buf_release(b2);
                    184:
1.25      deraadt   185:        for (s1 = c1, s2 = c2; *s1 && *s2; s1++, s2++) {
1.1       joris     186:                if (*s1 != *s2) {
                    187:                        match = 0;
                    188:                        break;
                    189:                }
                    190:        }
                    191:
1.20      joris     192:        xfree(c1);
                    193:        xfree(c2);
1.1       joris     194:
1.9       xsa       195:        if (match == 1) {
1.13      xsa       196:                if ((uflag == 1) && (!TAILQ_EMPTY(&(file->rf_locks)))) {
1.11      xsa       197:                        if ((verbose == 1) && (nflag == 0)) {
                    198:                                printf("rcs -u%s %s\n",
                    199:                                    rcsnum_tostr(frev, numb, sizeof(numb)),
                    200:                                    fpath);
                    201:                        }
1.19      joris     202:                        (void)rcs_lock_remove(file, locker, frev);
1.11      xsa       203:                }
                    204:
1.14      xsa       205:                if (TAILQ_EMPTY(&(file->rf_locks))) {
                    206:                        if (verbose == 1)
                    207:                                printf("rm -f %s\n", fname);
1.11      xsa       208:
1.14      xsa       209:                        if (nflag == 0)
                    210:                                (void)unlink(fname);
                    211:                }
1.1       joris     212:        }
                    213:
                    214:        rcs_close(file);
1.17      xsa       215:
                    216:        if (flags & PRESERVETIME)
                    217:                rcs_set_mtime(fpath, rcs_mtime);
1.1       joris     218: }