summaryrefslogtreecommitdiff
path: root/usr.bin/rcs/rcsclean.c
blob: e03c930cd6c8478a948959bd933ef26e58f55b16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*	$OpenBSD: rcsclean.c,v 1.18 2005/11/28 14:43:59 xsa Exp $	*/
/*
 * Copyright (c) 2005 Joris Vink <joris@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/stat.h>

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "log.h"
#include "rcs.h"
#include "diff.h"
#include "rcsprog.h"

static int rcsclean_file(char *, RCSNUM *);
static int nflag = 0;
static int kflag = RCS_KWEXP_ERR;
static int uflag = 0;
static int flags = 0;

int
rcsclean_main(int argc, char **argv)
{
	int i, ch;
	RCSNUM *rev;
	DIR *dirp;
	struct dirent *dp;

	rev = RCS_HEAD_REV;

	while ((ch = rcs_getopt(argc, argv, "k:n::q::r:Tu::Vx:")) != -1) {
		switch (ch) {
		case 'k':
			kflag = rcs_kflag_get(rcs_optarg);
			if (RCS_KWEXP_INVAL(kflag)) {
				cvs_log(LP_ERR,
				    "invalid RCS keyword expansion mode");
				(usage)();
				exit(1);
			}
			break;
		case 'n':
			rcs_set_rev(rcs_optarg, &rev);
			nflag = 1;
			break;
		case 'q':
			rcs_set_rev(rcs_optarg, &rev);
			verbose = 0;
			break;
		case 'r':
			rcs_set_rev(rcs_optarg, &rev);
			break;
		case 'T':
			flags |= PRESERVETIME;
			break;
		case 'u':
			rcs_set_rev(rcs_optarg, &rev);
			uflag = 1;
			break;
		case 'V':
			printf("%s\n", rcs_version);
			exit(0);
		case 'x':
			rcs_suffixes = rcs_optarg;
			break;
		default:
			break;
		}
	}

	argc -= rcs_optind;
	argv += rcs_optind;

	if (argc == 0) {
		if ((dirp = opendir(".")) == NULL) {
			cvs_log(LP_ERRNO, "failed to open directory '.'");
			(usage)();
			exit(1);
		}

		while ((dp = readdir(dirp)) != NULL) {
			if (dp->d_type == DT_DIR)
				continue;
			rcsclean_file(dp->d_name, rev);
		}

		closedir(dirp);
	} else {
		for (i = 0; i < argc; i++)
			rcsclean_file(argv[i], rev);
	}

	return (0);
}

void
rcsclean_usage(void)
{
	fprintf(stderr,
	    "usage: rcsclean [-V] [-kmode] [-n[rev]] [-q[rev]]\n"
	    "                [-rrev] [-u[rev]] [-xsuffixes] [file] ...\n");
}

static int
rcsclean_file(char *fname, RCSNUM *rev)
{
	int match;
	RCSFILE *file;
	char fpath[MAXPATHLEN], numb[64];
	RCSNUM *frev;
	BUF *b1, *b2;
	char *s1, *s2, *c1, *c2;
	struct stat st;
	time_t rcs_mtime = -1;

	match = 1;

	if (stat(fname, &st) == -1)
		return (-1);

	if (rcs_statfile(fname, fpath, sizeof(fpath)) < 0)
		return (-1);

	if ((file = rcs_open(fpath, RCS_RDWR)) == NULL)
		return (-1);

	if (flags & PRESERVETIME)
		rcs_mtime = rcs_get_mtime(file->rf_path);

	if (!RCS_KWEXP_INVAL(kflag))
		rcs_kwexp_set(file, kflag);

	if (rev == RCS_HEAD_REV)
		frev = file->rf_head;
	else
		frev = rev;

	if ((b1 = rcs_getrev(file, frev)) == NULL) {
		cvs_log(LP_ERR, "failed to get needed revision");
		rcs_close(file);
		return (-1);
	}

	if ((b2 = cvs_buf_load(fname, BUF_AUTOEXT)) == NULL) {
		cvs_log(LP_ERRNO, "failed to load '%s'", fname);
		rcs_close(file);
		return (-1);
	}

	cvs_buf_putc(b1, '\0');
	cvs_buf_putc(b2, '\0');

	c1 = cvs_buf_release(b1);
	c2 = cvs_buf_release(b2);

	for (s1 = c1, s2 = c2; *s1 && *s2; *s1++, *s2++) {
		if (*s1 != *s2) {
			match = 0;
			break;
		}
	}

	free(c1);
	free(c2);

	if (match == 1) {
		if ((uflag == 1) && (!TAILQ_EMPTY(&(file->rf_locks)))) {
			if ((verbose == 1) && (nflag == 0)) {
				printf("rcs -u%s %s\n",
				    rcsnum_tostr(frev, numb, sizeof(numb)),
				    fpath);
			}
			(void)rcs_lock_remove(file, frev);
		}

		if (TAILQ_EMPTY(&(file->rf_locks))) {
			if (verbose == 1)
				printf("rm -f %s\n", fname);

			if (nflag == 0)
				(void)unlink(fname);
		}
	}

	rcs_close(file);

	if (flags & PRESERVETIME)
		rcs_set_mtime(fpath, rcs_mtime);

	return (0);
}