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