Annotation of src/usr.bin/rcs/rcsclean.c, Revision 1.49
1.49 ! xsa 1: /* $OpenBSD: rcsclean.c,v 1.48 2007/02/27 07:59:13 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.48 xsa 27: #include <sys/types.h>
28:
29: #include <dirent.h>
30: #include <err.h>
31: #include <stdio.h>
32: #include <stdlib.h>
33: #include <unistd.h>
1.1 joris 34:
1.23 xsa 35: #include "rcsprog.h"
1.4 niallo 36: #include "diff.h"
1.1 joris 37:
1.31 ray 38: static void rcsclean_file(char *, const char *);
1.28 xsa 39:
1.3 joris 40: static int nflag = 0;
41: static int kflag = RCS_KWEXP_ERR;
1.11 xsa 42: static int uflag = 0;
1.17 xsa 43: static int flags = 0;
1.19 joris 44: static char *locker = NULL;
1.1 joris 45:
46: int
47: rcsclean_main(int argc, char **argv)
48: {
1.17 xsa 49: int i, ch;
1.30 ray 50: char *rev_str;
1.1 joris 51: DIR *dirp;
52: struct dirent *dp;
53:
1.30 ray 54: rev_str = NULL;
1.1 joris 55:
1.42 ray 56: while ((ch = rcs_getopt(argc, argv, "k:n::q::r::Tu::Vx::")) != -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)) {
1.40 xsa 61: warnx("invalid RCS keyword substitution mode");
1.3 joris 62: (usage)();
63: exit(1);
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)();
93: exit(1);
1.1 joris 94: }
95: }
96:
1.7 joris 97: argc -= rcs_optind;
98: argv += rcs_optind;
1.1 joris 99:
1.19 joris 100: if ((locker = getlogin()) == NULL)
1.43 xsa 101: err(1, "getlogin");
1.19 joris 102:
1.1 joris 103: if (argc == 0) {
104: if ((dirp = opendir(".")) == NULL) {
1.38 xsa 105: warn("opendir");
1.1 joris 106: (usage)();
107: exit(1);
108: }
109:
110: while ((dp = readdir(dirp)) != NULL) {
111: if (dp->d_type == DT_DIR)
112: continue;
1.31 ray 113: rcsclean_file(dp->d_name, rev_str);
1.1 joris 114: }
115:
1.38 xsa 116: (void)closedir(dirp);
1.30 ray 117: } else
1.31 ray 118: for (i = 0; i < argc; i++)
119: rcsclean_file(argv[i], rev_str);
1.1 joris 120:
121: return (0);
122: }
123:
124: void
125: rcsclean_usage(void)
126: {
1.5 deraadt 127: fprintf(stderr,
1.41 jmc 128: "usage: rcsclean [-TV] [-kmode] [-n[rev]] [-q[rev]] [-r[rev]]\n"
1.49 ! xsa 129: " [-u[rev]] [-xsuffixes] [-ztz] [file ...]\n");
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.11 xsa 137: char fpath[MAXPATHLEN], numb[64];
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.44 joris 168: if ((b2 = rcs_buf_load(fname, 0)) == 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.44 joris 174: if (rcs_buf_len(b1) != rcs_buf_len(b2))
1.34 ray 175: match = 0;
176: else {
177: size_t len, n;
1.1 joris 178:
1.44 joris 179: len = rcs_buf_len(b1);
1.1 joris 180:
1.34 ray 181: match = 1;
182: for (n = 0; n < len; ++n)
1.44 joris 183: if (rcs_buf_getc(b1, n) != rcs_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:
213: if (b1 != NULL)
1.44 joris 214: rcs_buf_free(b1);
1.34 ray 215: if (b2 != NULL)
1.44 joris 216: rcs_buf_free(b2);
1.34 ray 217: if (file != NULL)
218: rcs_close(file);
1.1 joris 219: }