Annotation of src/usr.bin/rcs/rcsclean.c, Revision 1.7
1.7 ! joris 1: /* $OpenBSD: rcsclean.c,v 1.6 2005/10/12 17:43:18 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.1 joris 44:
45: int
46: rcsclean_main(int argc, char **argv)
47: {
48: int i, ch;
49: RCSNUM *rev;
50: DIR *dirp;
51: struct dirent *dp;
52:
53: rev = RCS_HEAD_REV;
54:
1.7 ! joris 55: while ((ch = rcs_getopt(argc, argv, "k:nqr:V")) != -1) {
1.1 joris 56: switch (ch) {
1.3 joris 57: case 'k':
1.7 ! joris 58: kflag = rcs_kflag_get(rcs_optarg);
1.3 joris 59: if (RCS_KWEXP_INVAL(kflag)) {
60: cvs_log(LP_ERR,
61: "invalid RCS keyword expansion mode");
62: (usage)();
63: exit(1);
64: }
65: break;
66: case 'n':
67: nflag = 1;
68: break;
1.1 joris 69: case 'q':
70: verbose = 0;
71: break;
1.2 joris 72: case 'r':
1.7 ! joris 73: if ((rev = rcsnum_parse(rcs_optarg)) == NULL) {
1.2 joris 74: cvs_log(LP_ERR, "bad revision number");
75: exit(1);
76: }
77: break;
1.1 joris 78: case 'V':
79: printf("%s\n", rcs_version);
80: exit(0);
81: default:
82: break;
83: }
84: }
85:
1.7 ! joris 86: argc -= rcs_optind;
! 87: argv += rcs_optind;
1.1 joris 88:
89: if (argc == 0) {
90: if ((dirp = opendir(".")) == NULL) {
91: cvs_log(LP_ERRNO, "failed to open directory '.'");
92: (usage)();
93: exit(1);
94: }
95:
96: while ((dp = readdir(dirp)) != NULL) {
97: if (dp->d_type == DT_DIR)
98: continue;
99: rcsclean_file(dp->d_name, rev);
100: }
101:
102: closedir(dirp);
103: } else {
104: for (i = 0; i < argc; i++)
105: rcsclean_file(argv[i], rev);
106: }
107:
108: return (0);
109: }
110:
111: void
112: rcsclean_usage(void)
113: {
1.5 deraadt 114: fprintf(stderr,
115: "usage: rcsclean [-qV] [-r rev] [file] ...\n");
1.1 joris 116: }
117:
118: static int
119: rcsclean_file(char *fname, RCSNUM *rev)
120: {
121: int match;
122: RCSFILE *file;
123: char fpath[MAXPATHLEN];
124: RCSNUM *frev;
125: BUF *b1, *b2;
126: char *s1, *s2, *c1, *c2;
1.3 joris 127: struct stat st;
1.1 joris 128:
129: match = 1;
1.3 joris 130:
131: if (stat(fname, &st) == -1)
132: return (-1);
133:
1.1 joris 134: if (rcs_statfile(fname, fpath, sizeof(fpath)) < 0)
135: return (-1);
136:
137: if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
138: return (-1);
139:
1.3 joris 140: if (!RCS_KWEXP_INVAL(kflag))
141: rcs_kwexp_set(file, kflag);
142:
1.1 joris 143: if (rev == RCS_HEAD_REV)
144: frev = file->rf_head;
145: else
146: frev = rev;
147:
148: if ((b1 = rcs_getrev(file, frev)) == NULL) {
149: cvs_log(LP_ERR, "failed to get needed revision");
150: rcs_close(file);
151: return (-1);
152: }
153:
154: if ((b2 = cvs_buf_load(fname, BUF_AUTOEXT)) == NULL) {
155: cvs_log(LP_ERRNO, "failed to load '%s'", fname);
156: rcs_close(file);
157: return (-1);
158: }
159:
160: cvs_buf_putc(b1, '\0');
161: cvs_buf_putc(b2, '\0');
162:
163: c1 = cvs_buf_release(b1);
164: c2 = cvs_buf_release(b2);
165:
166: for (s1 = c1, s2 = c2; *s1 && *s2; *s1++, *s2++) {
167: if (*s1 != *s2) {
168: match = 0;
169: break;
170: }
171: }
172:
173: free(c1);
174: free(c2);
175:
176: if (match) {
1.6 xsa 177: if (verbose == 1)
1.1 joris 178: printf("rm -f %s\n", fname);
1.3 joris 179: if (nflag == 0)
180: (void)unlink(fname);
1.1 joris 181: }
182:
183: rcs_close(file);
184: return (0);
185: }