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

1.47    ! ray         1: /*     $OpenBSD: rcsclean.c,v 1.46 2006/05/09 12:33:42 ray 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.31      ray        32: static void    rcsclean_file(char *, const char *);
1.28      xsa        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:        DIR *dirp;
                     46:        struct dirent *dp;
                     47:
1.30      ray        48:        rev_str = NULL;
1.1       joris      49:
1.42      ray        50:        while ((ch = rcs_getopt(argc, argv, "k:n::q::r::Tu::Vx::")) != -1) {
1.1       joris      51:                switch (ch) {
1.3       joris      52:                case 'k':
1.7       joris      53:                        kflag = rcs_kflag_get(rcs_optarg);
1.3       joris      54:                        if (RCS_KWEXP_INVAL(kflag)) {
1.40      xsa        55:                                warnx("invalid RCS keyword substitution mode");
1.3       joris      56:                                (usage)();
                     57:                                exit(1);
                     58:                        }
                     59:                        break;
                     60:                case 'n':
1.30      ray        61:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.3       joris      62:                        nflag = 1;
                     63:                        break;
1.1       joris      64:                case 'q':
1.30      ray        65:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.37      xsa        66:                        flags |= QUIET;
1.1       joris      67:                        break;
1.2       joris      68:                case 'r':
1.30      ray        69:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.16      xsa        70:                        break;
                     71:                case 'T':
                     72:                        flags |= PRESERVETIME;
1.2       joris      73:                        break;
1.11      xsa        74:                case 'u':
1.30      ray        75:                        rcs_setrevstr(&rev_str, rcs_optarg);
1.11      xsa        76:                        uflag = 1;
                     77:                        break;
1.1       joris      78:                case 'V':
                     79:                        printf("%s\n", rcs_version);
                     80:                        exit(0);
1.15      xsa        81:                case 'x':
1.27      ray        82:                        /* Use blank extension if none given. */
                     83:                        rcs_suffixes = rcs_optarg ? rcs_optarg : "";
1.15      xsa        84:                        break;
1.1       joris      85:                default:
1.35      ray        86:                        (usage)();
                     87:                        exit(1);
1.1       joris      88:                }
                     89:        }
                     90:
1.7       joris      91:        argc -= rcs_optind;
                     92:        argv += rcs_optind;
1.1       joris      93:
1.19      joris      94:        if ((locker = getlogin()) == NULL)
1.43      xsa        95:                err(1, "getlogin");
1.19      joris      96:
1.1       joris      97:        if (argc == 0) {
                     98:                if ((dirp = opendir(".")) == NULL) {
1.38      xsa        99:                        warn("opendir");
1.1       joris     100:                        (usage)();
                    101:                        exit(1);
                    102:                }
                    103:
                    104:                while ((dp = readdir(dirp)) != NULL) {
                    105:                        if (dp->d_type == DT_DIR)
                    106:                                continue;
1.31      ray       107:                        rcsclean_file(dp->d_name, rev_str);
1.1       joris     108:                }
                    109:
1.38      xsa       110:                (void)closedir(dirp);
1.30      ray       111:        } else
1.31      ray       112:                for (i = 0; i < argc; i++)
                    113:                        rcsclean_file(argv[i], rev_str);
1.1       joris     114:
                    115:        return (0);
                    116: }
                    117:
                    118: void
                    119: rcsclean_usage(void)
                    120: {
1.5       deraadt   121:        fprintf(stderr,
1.41      jmc       122:            "usage: rcsclean [-TV] [-kmode] [-n[rev]] [-q[rev]] [-r[rev]]\n"
                    123:            "                [-u[rev]] [-xsuffixes] [-ztz] [file] ...\n");
1.1       joris     124: }
                    125:
1.28      xsa       126: static void
1.31      ray       127: rcsclean_file(char *fname, const char *rev_str)
1.1       joris     128: {
1.45      joris     129:        int fd, match;
1.1       joris     130:        RCSFILE *file;
1.11      xsa       131:        char fpath[MAXPATHLEN], numb[64];
1.31      ray       132:        RCSNUM *rev;
1.1       joris     133:        BUF *b1, *b2;
1.17      xsa       134:        time_t rcs_mtime = -1;
1.1       joris     135:
1.34      ray       136:        b1 = b2 = NULL;
                    137:        file = NULL;
                    138:        rev = NULL;
1.3       joris     139:
1.47    ! ray       140:        if ((fd = rcs_choosefile(fname, fpath, sizeof(fpath))) < 0)
1.34      ray       141:                goto out;
1.1       joris     142:
1.45      joris     143:        if ((file = rcs_open(fpath, fd, RCS_RDWR)) == NULL)
1.34      ray       144:                goto out;
1.1       joris     145:
1.17      xsa       146:        if (flags & PRESERVETIME)
1.45      joris     147:                rcs_mtime = rcs_get_mtime(file);
1.17      xsa       148:
1.29      xsa       149:        rcs_kwexp_set(file, kflag);
1.3       joris     150:
1.31      ray       151:        if (rev_str == NULL)
                    152:                rev = file->rf_head;
                    153:        else if ((rev = rcs_getrevnum(rev_str, file)) == NULL) {
1.38      xsa       154:                warnx("%s: Symbolic name `%s' is undefined.", fpath, rev_str);
1.34      ray       155:                goto out;
1.31      ray       156:        }
1.1       joris     157:
1.31      ray       158:        if ((b1 = rcs_getrev(file, rev)) == NULL) {
1.38      xsa       159:                warnx("failed to get needed revision");
1.34      ray       160:                goto out;
1.1       joris     161:        }
1.44      joris     162:        if ((b2 = rcs_buf_load(fname, 0)) == NULL) {
1.38      xsa       163:                warnx("failed to load `%s'", fname);
1.34      ray       164:                goto out;
1.1       joris     165:        }
                    166:
1.34      ray       167:        /* If buffer lengths are the same, compare contents as well. */
1.44      joris     168:        if (rcs_buf_len(b1) != rcs_buf_len(b2))
1.34      ray       169:                match = 0;
                    170:        else {
                    171:                size_t len, n;
1.1       joris     172:
1.44      joris     173:                len = rcs_buf_len(b1);
1.1       joris     174:
1.34      ray       175:                match = 1;
                    176:                for (n = 0; n < len; ++n)
1.44      joris     177:                        if (rcs_buf_getc(b1, n) != rcs_buf_getc(b2, n)) {
1.34      ray       178:                                match = 0;
                    179:                                break;
                    180:                        }
                    181:        }
1.1       joris     182:
1.9       xsa       183:        if (match == 1) {
1.33      deraadt   184:                if (uflag == 1 && !TAILQ_EMPTY(&(file->rf_locks))) {
1.37      xsa       185:                        if (!(flags & QUIET) && nflag == 0) {
1.11      xsa       186:                                printf("rcs -u%s %s\n",
1.31      ray       187:                                    rcsnum_tostr(rev, numb, sizeof(numb)),
1.11      xsa       188:                                    fpath);
                    189:                        }
1.31      ray       190:                        (void)rcs_lock_remove(file, locker, rev);
1.11      xsa       191:                }
                    192:
1.14      xsa       193:                if (TAILQ_EMPTY(&(file->rf_locks))) {
1.37      xsa       194:                        if (!(flags & QUIET))
1.14      xsa       195:                                printf("rm -f %s\n", fname);
1.11      xsa       196:
1.14      xsa       197:                        if (nflag == 0)
                    198:                                (void)unlink(fname);
                    199:                }
1.1       joris     200:        }
                    201:
1.45      joris     202:        rcs_write(file);
1.17      xsa       203:        if (flags & PRESERVETIME)
1.45      joris     204:                rcs_set_mtime(file, rcs_mtime);
1.34      ray       205:
                    206: out:
                    207:        if (b1 != NULL)
1.44      joris     208:                rcs_buf_free(b1);
1.34      ray       209:        if (b2 != NULL)
1.44      joris     210:                rcs_buf_free(b2);
1.34      ray       211:        if (file != NULL)
                    212:                rcs_close(file);
1.1       joris     213: }