Annotation of src/usr.bin/rcs/rcsclean.c, Revision 1.25
1.25 ! deraadt 1: /* $OpenBSD: rcsclean.c,v 1.24 2006/03/06 14:59:06 jmc 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:
32: static int rcsclean_file(char *, RCSNUM *);
1.3 joris 33: static int nflag = 0;
34: static int kflag = RCS_KWEXP_ERR;
1.11 xsa 35: static int uflag = 0;
1.17 xsa 36: static int flags = 0;
1.19 joris 37: static char *locker = NULL;
1.1 joris 38:
39: int
40: rcsclean_main(int argc, char **argv)
41: {
1.17 xsa 42: int i, ch;
1.1 joris 43: RCSNUM *rev;
44: DIR *dirp;
45: struct dirent *dp;
46:
47: rev = RCS_HEAD_REV;
48:
1.16 xsa 49: while ((ch = rcs_getopt(argc, argv, "k:n::q::r:Tu::Vx:")) != -1) {
1.1 joris 50: switch (ch) {
1.3 joris 51: case 'k':
1.7 joris 52: kflag = rcs_kflag_get(rcs_optarg);
1.3 joris 53: if (RCS_KWEXP_INVAL(kflag)) {
54: cvs_log(LP_ERR,
55: "invalid RCS keyword expansion mode");
56: (usage)();
57: exit(1);
58: }
59: break;
60: case 'n':
1.12 xsa 61: rcs_set_rev(rcs_optarg, &rev);
1.3 joris 62: nflag = 1;
63: break;
1.1 joris 64: case 'q':
1.12 xsa 65: rcs_set_rev(rcs_optarg, &rev);
1.1 joris 66: verbose = 0;
67: break;
1.2 joris 68: case 'r':
1.8 joris 69: rcs_set_rev(rcs_optarg, &rev);
1.16 xsa 70: break;
71: case 'T':
72: flags |= PRESERVETIME;
1.2 joris 73: break;
1.11 xsa 74: case 'u':
1.12 xsa 75: rcs_set_rev(rcs_optarg, &rev);
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':
82: rcs_suffixes = rcs_optarg;
83: break;
1.1 joris 84: default:
85: break;
86: }
87: }
88:
1.7 joris 89: argc -= rcs_optind;
90: argv += rcs_optind;
1.1 joris 91:
1.19 joris 92: if ((locker = getlogin()) == NULL)
1.21 xsa 93: fatal("getlogin failed");
1.19 joris 94:
1.1 joris 95: if (argc == 0) {
96: if ((dirp = opendir(".")) == NULL) {
97: cvs_log(LP_ERRNO, "failed to open directory '.'");
98: (usage)();
99: exit(1);
100: }
101:
102: while ((dp = readdir(dirp)) != NULL) {
103: if (dp->d_type == DT_DIR)
104: continue;
105: rcsclean_file(dp->d_name, rev);
106: }
107:
108: closedir(dirp);
109: } else {
110: for (i = 0; i < argc; i++)
111: rcsclean_file(argv[i], rev);
112: }
113:
114: return (0);
115: }
116:
117: void
118: rcsclean_usage(void)
119: {
1.5 deraadt 120: fprintf(stderr,
1.24 jmc 121: "usage: rcsclean [-TV] [-kmode] [-n[rev]] [-q[rev]]\n"
122: " [-rrev] [-u[rev]] [-xsuffixes] [-ztz] [file] ...\n");
1.1 joris 123: }
124:
125: static int
126: rcsclean_file(char *fname, RCSNUM *rev)
127: {
128: int match;
129: RCSFILE *file;
1.11 xsa 130: char fpath[MAXPATHLEN], numb[64];
1.1 joris 131: RCSNUM *frev;
132: BUF *b1, *b2;
133: char *s1, *s2, *c1, *c2;
1.3 joris 134: struct stat st;
1.17 xsa 135: time_t rcs_mtime = -1;
1.1 joris 136:
137: match = 1;
1.3 joris 138:
139: if (stat(fname, &st) == -1)
140: return (-1);
141:
1.1 joris 142: if (rcs_statfile(fname, fpath, sizeof(fpath)) < 0)
143: return (-1);
144:
145: if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
146: return (-1);
147:
1.17 xsa 148: if (flags & PRESERVETIME)
149: rcs_mtime = rcs_get_mtime(file->rf_path);
150:
1.3 joris 151: if (!RCS_KWEXP_INVAL(kflag))
152: rcs_kwexp_set(file, kflag);
153:
1.1 joris 154: if (rev == RCS_HEAD_REV)
155: frev = file->rf_head;
156: else
157: frev = rev;
158:
159: if ((b1 = rcs_getrev(file, frev)) == NULL) {
160: cvs_log(LP_ERR, "failed to get needed revision");
161: rcs_close(file);
162: return (-1);
163: }
164:
165: if ((b2 = cvs_buf_load(fname, BUF_AUTOEXT)) == NULL) {
166: cvs_log(LP_ERRNO, "failed to load '%s'", fname);
167: rcs_close(file);
168: return (-1);
169: }
170:
171: cvs_buf_putc(b1, '\0');
172: cvs_buf_putc(b2, '\0');
173:
174: c1 = cvs_buf_release(b1);
175: c2 = cvs_buf_release(b2);
176:
1.25 ! deraadt 177: for (s1 = c1, s2 = c2; *s1 && *s2; s1++, s2++) {
1.1 joris 178: if (*s1 != *s2) {
179: match = 0;
180: break;
181: }
182: }
183:
1.20 joris 184: xfree(c1);
185: xfree(c2);
1.1 joris 186:
1.9 xsa 187: if (match == 1) {
1.13 xsa 188: if ((uflag == 1) && (!TAILQ_EMPTY(&(file->rf_locks)))) {
1.11 xsa 189: if ((verbose == 1) && (nflag == 0)) {
190: printf("rcs -u%s %s\n",
191: rcsnum_tostr(frev, numb, sizeof(numb)),
192: fpath);
193: }
1.19 joris 194: (void)rcs_lock_remove(file, locker, frev);
1.11 xsa 195: }
196:
1.14 xsa 197: if (TAILQ_EMPTY(&(file->rf_locks))) {
198: if (verbose == 1)
199: printf("rm -f %s\n", fname);
1.11 xsa 200:
1.14 xsa 201: if (nflag == 0)
202: (void)unlink(fname);
203: }
1.1 joris 204: }
205:
206: rcs_close(file);
1.17 xsa 207:
208: if (flags & PRESERVETIME)
209: rcs_set_mtime(fpath, rcs_mtime);
210:
1.1 joris 211: return (0);
212: }