summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/rcs/rcsclean/rcsclean.c
blob: 3d8ecd3fe5d9a8ccef789b491a86197aa5656047 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* Clean up working files.  */

/* Copyright 1991, 1992, 1993, 1994, 1995 Paul Eggert
   Distributed under license by the Free Software Foundation, Inc.

This file is part of RCS.

RCS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

RCS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RCS; see the file COPYING.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

Report problems and direct all questions to:

	rcs-bugs@cs.purdue.edu

*/

#include "rcsbase.h"

#if has_dirent
	static int get_directory P((char const*,char***));
#endif

static int unlock P((struct hshentry *));
static void cleanup P((void));

static RILE *workptr;
static int exitstatus;

mainProg(rcscleanId, "rcsclean", "$Id: rcsclean.c,v 5.9 1995/06/16 06:19:24 eggert Exp $")
{
	static char const usage[] =
		"\nrcsclean: usage: rcsclean -ksubst -{nqru}[rev] -T -Vn -xsuff -zzone file ...";

	static struct buf revision;

	char *a, **newargv;
	char const *rev, *p;
	int dounlock, expmode, perform, unlocked, unlockflag, waslocked;
	int Ttimeflag;
	struct hshentries *deltas;
	struct hshentry *delta;
	struct stat workstat;

	setrid();

	expmode = -1;
	rev = 0;
	suffixes = X_DEFAULT;
	perform = true;
	unlockflag = false;
	Ttimeflag = false;

	argc = getRCSINIT(argc, argv, &newargv);
	argv = newargv;
	for (;;) {
		if (--argc < 1) {
#			if has_dirent
				argc = get_directory(".", &newargv);
				argv = newargv;
				break;
#			else
				faterror("no pathnames specified");
#			endif
		}
		a = *++argv;
		if (!*a  ||  *a++ != '-')
			break;
		switch (*a++) {
			case 'k':
				if (0 <= expmode)
					redefined('k');
				if ((expmode = str2expmode(a))  <  0)
					goto unknown;
				break;

			case 'n':
				perform = false;
				goto handle_revision;

			case 'q':
				quietflag = true;
				/* fall into */
			case 'r':
			handle_revision:
				if (*a) {
					if (rev)
						warn("redefinition of revision number");
					rev = a;
				}
				break;

			case 'T':
				if (*a)
					goto unknown;
				Ttimeflag = true;
				break;

			case 'u':
				unlockflag = true;
				goto handle_revision;

			case 'V':
				setRCSversion(*argv);
				break;

			case 'x':
				suffixes = a;
				break;

			case 'z':
				zone_set(a);
				break;

			default:
			unknown:
				error("unknown option: %s%s", *argv, usage);
		}
	}

	dounlock = perform & unlockflag;

	if (nerror)
	  cleanup();
	else
	  for (;  0 < argc;  cleanup(), ++argv, --argc) {

		ffree();

		if (!(
			0 < pairnames(
				argc, argv,
				dounlock ? rcswriteopen : rcsreadopen,
				true, true
			) &&
			(workptr = Iopen(workname, FOPEN_R_WORK, &workstat))
		))
			continue;

		if (same_file(RCSstat, workstat, 0)) {
			rcserror("RCS file is the same as working file %s.",
				workname
			);
			continue;
		}

		gettree();

		p = 0;
		if (rev) {
			if (!fexpandsym(rev, &revision, workptr))
				continue;
			p = revision.string;
		} else if (Head)
			switch (unlockflag ? findlock(false,&delta) : 0) {
				default:
					continue;
				case 0:
					p = Dbranch ? Dbranch : "";
					break;
				case 1:
					p = delta->num;
					break;
			}
		delta = 0;
		deltas = 0;  /* Keep lint happy.  */
		if (p  &&  !(delta = genrevs(p,(char*)0,(char*)0,(char*)0,&deltas)))
			continue;

		waslocked = delta && delta->lockedby;
		locker_expansion = unlock(delta);
		unlocked = locker_expansion & unlockflag;
		if (unlocked<waslocked  &&  workstat.st_mode&(S_IWUSR|S_IWGRP|S_IWOTH))
			continue;

		if (unlocked && !checkaccesslist())
			continue;

		if (dorewrite(dounlock, unlocked) != 0)
			continue;

		if (0 <= expmode)
			Expand = expmode;
		else if (
			waslocked  &&
			Expand == KEYVAL_EXPAND  &&
			WORKMODE(RCSstat.st_mode,true) == workstat.st_mode
		)
			Expand = KEYVALLOCK_EXPAND;

		getdesc(false);

		if (
			!delta ? workstat.st_size!=0 :
			0 < rcsfcmp(
				workptr, &workstat,
				buildrevision(deltas, delta, (FILE*)0, false),
				delta
			)
		)
			continue;

		if (quietflag < unlocked)
			aprintf(stdout, "rcs -u%s %s\n", delta->num, RCSname);

		if (perform & unlocked) {
			if_advise_access(deltas->first != delta, finptr, MADV_SEQUENTIAL);
			if (donerewrite(true,
				Ttimeflag ? RCSstat.st_mtime : (time_t)-1
			) != 0)
				continue;
		}

		if (!quietflag)
			aprintf(stdout, "rm -f %s\n", workname);
		Izclose(&workptr);
		if (perform  &&  un_link(workname) != 0)
			eerror(workname);

	  }

	tempunlink();
	if (!quietflag)
		Ofclose(stdout);
	exitmain(exitstatus);
}

	static void
cleanup()
{
	if (nerror) exitstatus = EXIT_FAILURE;
	Izclose(&finptr);
	Izclose(&workptr);
	Ozclose(&fcopy);
	ORCSclose();
	dirtempunlink();
}

#if RCS_lint
#	define exiterr rcscleanExit
#endif
	void
exiterr()
{
	ORCSerror();
	dirtempunlink();
	tempunlink();
	_exit(EXIT_FAILURE);
}

	static int
unlock(delta)
	struct hshentry *delta;
{
	register struct rcslock **al, *l;

	if (delta && delta->lockedby && strcmp(getcaller(),delta->lockedby)==0)
		for (al = &Locks;  (l = *al);  al = &l->nextlock)
			if (l->delta == delta) {
				*al = l->nextlock;
				delta->lockedby = 0;
				return true;
			}
	return false;
}

#if has_dirent
	static int
get_directory(dirname, aargv)
	char const *dirname;
	char ***aargv;
/*
 * Put a vector of all DIRNAME's directory entries names into *AARGV.
 * Ignore names of RCS files.
 * Yield the number of entries found.  Terminate the vector with 0.
 * Allocate the storage for the vector and entry names.
 * Do not sort the names.  Do not include '.' and '..'.
 */
{
	int i, entries = 0, entries_max = 64;
	size_t chars = 0, chars_max = 1024;
	size_t *offset = tnalloc(size_t, entries_max);
	char *a = tnalloc(char, chars_max), **p;
	DIR *d;
	struct dirent *e;

	if (!(d = opendir(dirname)))
		efaterror(dirname);
	while ((errno = 0,  e = readdir(d))) {
		char const *en = e->d_name;
		size_t s = strlen(en) + 1;
		if (en[0]=='.'   &&   (!en[1]  ||  (en[1]=='.' && !en[2])))
			continue;
		if (rcssuffix(en))
			continue;
		while (chars_max < s + chars)
			a = trealloc(char, a, chars_max<<=1);
		if (entries == entries_max)
			offset = trealloc(size_t, offset, entries_max<<=1);
		offset[entries++] = chars;
		VOID strcpy(a+chars, en);
		chars += s;
	}
#	if void_closedir
#		define close_directory(d) (closedir(d), 0)
#	else
#		define close_directory(d) closedir(d)
#	endif
	if (errno  ||  close_directory(d) != 0)
		efaterror(dirname);
	if (chars)
		a = trealloc(char, a, chars);
	else
		tfree(a);
	*aargv = p = tnalloc(char*, entries+1);
	for (i=0; i<entries; i++)
		*p++ = a + offset[i];
	*p = 0;
	tfree(offset);
	return entries;
}
#endif
OpenPOWER on IntegriCloud