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

1.14    ! xsa         1: /*     $OpenBSD: rcsclean.c,v 1.13 2005/11/16 11:42:03 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:
                     27: #include <sys/param.h>
                     28: #include <sys/stat.h>
                     29:
                     30: #include <dirent.h>
                     31: #include <stdio.h>
                     32: #include <stdlib.h>
                     33: #include <string.h>
                     34: #include <unistd.h>
                     35:
                     36: #include "log.h"
                     37: #include "rcs.h"
1.4       niallo     38: #include "diff.h"
1.1       joris      39: #include "rcsprog.h"
                     40:
                     41: static int rcsclean_file(char *, RCSNUM *);
1.3       joris      42: static int nflag = 0;
                     43: static int kflag = RCS_KWEXP_ERR;
1.11      xsa        44: static int uflag = 0;
1.1       joris      45:
                     46: int
                     47: rcsclean_main(int argc, char **argv)
                     48: {
                     49:        int i, ch;
                     50:        RCSNUM *rev;
                     51:        DIR *dirp;
                     52:        struct dirent *dp;
                     53:
                     54:        rev = RCS_HEAD_REV;
                     55:
1.12      xsa        56:        while ((ch = rcs_getopt(argc, argv, "k:n::q::r:u::V")) != -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)) {
                     61:                                cvs_log(LP_ERR,
                     62:                                    "invalid RCS keyword expansion mode");
                     63:                                (usage)();
                     64:                                exit(1);
                     65:                        }
                     66:                        break;
                     67:                case 'n':
1.12      xsa        68:                        rcs_set_rev(rcs_optarg, &rev);
1.3       joris      69:                        nflag = 1;
                     70:                        break;
1.1       joris      71:                case 'q':
1.12      xsa        72:                        rcs_set_rev(rcs_optarg, &rev);
1.1       joris      73:                        verbose = 0;
                     74:                        break;
1.2       joris      75:                case 'r':
1.8       joris      76:                        rcs_set_rev(rcs_optarg, &rev);
1.2       joris      77:                        break;
1.11      xsa        78:                case 'u':
1.12      xsa        79:                        rcs_set_rev(rcs_optarg, &rev);
1.11      xsa        80:                        uflag = 1;
                     81:                        break;
1.1       joris      82:                case 'V':
                     83:                        printf("%s\n", rcs_version);
                     84:                        exit(0);
                     85:                default:
                     86:                        break;
                     87:                }
                     88:        }
                     89:
1.7       joris      90:        argc -= rcs_optind;
                     91:        argv += rcs_optind;
1.1       joris      92:
                     93:        if (argc == 0) {
                     94:                if ((dirp = opendir(".")) == NULL) {
                     95:                        cvs_log(LP_ERRNO, "failed to open directory '.'");
                     96:                        (usage)();
                     97:                        exit(1);
                     98:                }
                     99:
                    100:                while ((dp = readdir(dirp)) != NULL) {
                    101:                        if (dp->d_type == DT_DIR)
                    102:                                continue;
                    103:                        rcsclean_file(dp->d_name, rev);
                    104:                }
                    105:
                    106:                closedir(dirp);
                    107:        } else {
                    108:                for (i = 0; i < argc; i++)
                    109:                        rcsclean_file(argv[i], rev);
                    110:        }
                    111:
                    112:        return (0);
                    113: }
                    114:
                    115: void
                    116: rcsclean_usage(void)
                    117: {
1.5       deraadt   118:        fprintf(stderr,
1.12      xsa       119:            "usage: rcsclean [-V] [-kmode] [-n[rev]] [-q[rev]]\n"
                    120:            "                [-rrev] [-u[rev]] [file] ...\n");
1.1       joris     121: }
                    122:
                    123: static int
                    124: rcsclean_file(char *fname, RCSNUM *rev)
                    125: {
                    126:        int match;
                    127:        RCSFILE *file;
1.11      xsa       128:        char fpath[MAXPATHLEN], numb[64];
1.1       joris     129:        RCSNUM *frev;
                    130:        BUF *b1, *b2;
                    131:        char *s1, *s2, *c1, *c2;
1.3       joris     132:        struct stat st;
1.1       joris     133:
                    134:        match = 1;
1.3       joris     135:
                    136:        if (stat(fname, &st) == -1)
                    137:                return (-1);
                    138:
1.1       joris     139:        if (rcs_statfile(fname, fpath, sizeof(fpath)) < 0)
                    140:                return (-1);
                    141:
                    142:        if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
                    143:                return (-1);
                    144:
1.3       joris     145:        if (!RCS_KWEXP_INVAL(kflag))
                    146:                rcs_kwexp_set(file, kflag);
                    147:
1.1       joris     148:        if (rev == RCS_HEAD_REV)
                    149:                frev = file->rf_head;
                    150:        else
                    151:                frev = rev;
                    152:
                    153:        if ((b1 = rcs_getrev(file, frev)) == NULL) {
                    154:                cvs_log(LP_ERR, "failed to get needed revision");
                    155:                rcs_close(file);
                    156:                return (-1);
                    157:        }
                    158:
                    159:        if ((b2 = cvs_buf_load(fname, BUF_AUTOEXT)) == NULL) {
                    160:                cvs_log(LP_ERRNO, "failed to load '%s'", fname);
                    161:                rcs_close(file);
                    162:                return (-1);
                    163:        }
                    164:
                    165:        cvs_buf_putc(b1, '\0');
                    166:        cvs_buf_putc(b2, '\0');
                    167:
                    168:        c1 = cvs_buf_release(b1);
                    169:        c2 = cvs_buf_release(b2);
                    170:
                    171:        for (s1 = c1, s2 = c2; *s1 && *s2; *s1++, *s2++) {
                    172:                if (*s1 != *s2) {
                    173:                        match = 0;
                    174:                        break;
                    175:                }
                    176:        }
                    177:
                    178:        free(c1);
                    179:        free(c2);
                    180:
1.9       xsa       181:        if (match == 1) {
1.13      xsa       182:                if ((uflag == 1) && (!TAILQ_EMPTY(&(file->rf_locks)))) {
1.11      xsa       183:                        if ((verbose == 1) && (nflag == 0)) {
                    184:                                printf("rcs -u%s %s\n",
                    185:                                    rcsnum_tostr(frev, numb, sizeof(numb)),
                    186:                                    fpath);
                    187:                        }
                    188:                        (void)rcs_lock_remove(file, frev);
                    189:                }
                    190:
1.14    ! xsa       191:                if (TAILQ_EMPTY(&(file->rf_locks))) {
        !           192:                        if (verbose == 1)
        !           193:                                printf("rm -f %s\n", fname);
1.11      xsa       194:
1.14    ! xsa       195:                        if (nflag == 0)
        !           196:                                (void)unlink(fname);
        !           197:                }
1.1       joris     198:        }
                    199:
                    200:        rcs_close(file);
                    201:        return (0);
                    202: }