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