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

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