1 /* Clean up working files. */
3 /* Copyright 1991, 1992, 1993, 1994, 1995 Paul Eggert
4 Distributed under license by the Free Software Foundation, Inc.
6 This file is part of RCS.
8 RCS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 RCS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with RCS; see the file COPYING.
20 If not, write to the Free Software Foundation,
21 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 Report problems and direct all questions to:
25 rcs-bugs@cs.purdue.edu
29 * $FreeBSD: src/gnu/usr.bin/rcs/rcsclean/rcsclean.c,v 1.5 1999/08/27 23:36:54 peter Exp $
30 * $DragonFly: src/gnu/usr.bin/rcs/rcsclean/rcsclean.c,v 1.2 2003/06/17 04:25:48 dillon Exp $
36 static int get_directory P((char const*,char***));
39 static int unlock P((struct hshentry *));
40 static void cleanup P((void));
43 static int exitstatus;
45 mainProg(rcscleanId, "rcsclean", "$DragonFly: src/gnu/usr.bin/rcs/rcsclean/rcsclean.c,v 1.2 2003/06/17 04:25:48 dillon Exp $")
47 static char const usage[] =
48 "\nrcsclean: usage: rcsclean -ksubst -{nqru}[rev] -T -Vn -xsuff -zzone file ...";
50 static struct buf revision;
54 int dounlock, expmode, perform, unlocked, unlockflag, waslocked;
56 struct hshentries *deltas;
57 struct hshentry *delta;
69 argc = getRCSINIT(argc, argv, &newargv);
74 argc = get_directory(".", &newargv);
78 faterror("no pathnames specified");
82 if (!*a || *a++ != '-')
88 if ((expmode = str2expmode(a)) < 0)
103 warn("redefinition of revision number");
116 goto handle_revision;
119 setRCSversion(*argv);
132 error("unknown option: %s%s", *argv, usage);
136 dounlock = perform & unlockflag;
141 for (; 0 < argc; cleanup(), ++argv, --argc) {
148 dounlock ? rcswriteopen : rcsreadopen,
151 (workptr = Iopen(workname, FOPEN_R_WORK, &workstat))
155 if (same_file(RCSstat, workstat, 0)) {
156 rcserror("RCS file is the same as working file %s.",
166 if (!fexpandsym(rev, &revision, workptr))
170 switch (unlockflag ? findlock(false,&delta) : 0) {
174 p = Dbranch ? Dbranch : "";
181 deltas = 0; /* Keep lint happy. */
182 if (p && !(delta = genrevs(p,(char*)0,(char*)0,(char*)0,&deltas)))
185 waslocked = delta && delta->lockedby;
186 locker_expansion = unlock(delta);
187 unlocked = locker_expansion & unlockflag;
188 if (unlocked<waslocked && workstat.st_mode&(S_IWUSR|S_IWGRP|S_IWOTH))
191 if (unlocked && !checkaccesslist())
194 if (dorewrite(dounlock, unlocked) != 0)
201 Expand == KEYVAL_EXPAND &&
202 WORKMODE(RCSstat.st_mode,true) == workstat.st_mode
204 Expand = KEYVALLOCK_EXPAND;
209 !delta ? workstat.st_size!=0 :
212 buildrevision(deltas, delta, (FILE*)0, false),
218 if (quietflag < unlocked)
219 aprintf(stdout, "rcs -u%s %s\n", delta->num, RCSname);
221 if (perform & unlocked) {
222 if_advise_access(deltas->first != delta, finptr, MADV_SEQUENTIAL);
223 if (donerewrite(true,
224 Ttimeflag ? RCSstat.st_mtime : (time_t)-1
230 aprintf(stdout, "rm -f %s\n", workname);
232 if (perform && un_link(workname) != 0)
240 exitmain(exitstatus);
246 if (nerror) exitstatus = EXIT_FAILURE;
255 # define exiterr rcscleanExit
268 struct hshentry *delta;
270 register struct rcslock **al, *l;
272 if (delta && delta->lockedby && strcmp(getcaller(),delta->lockedby)==0)
273 for (al = &Locks; (l = *al); al = &l->nextlock)
274 if (l->delta == delta) {
284 get_directory(dirname, aargv)
288 * Put a vector of all DIRNAME's directory entries names into *AARGV.
289 * Ignore names of RCS files.
290 * Yield the number of entries found. Terminate the vector with 0.
291 * Allocate the storage for the vector and entry names.
292 * Do not sort the names. Do not include '.' and '..'.
295 int i, entries = 0, entries_max = 64;
296 size_t chars = 0, chars_max = 1024;
297 size_t *offset = tnalloc(size_t, entries_max);
298 char *a = tnalloc(char, chars_max), **p;
302 if (!(d = opendir(dirname)))
304 while ((errno = 0, e = readdir(d))) {
305 char const *en = e->d_name;
306 size_t s = strlen(en) + 1;
307 if (en[0]=='.' && (!en[1] || (en[1]=='.' && !en[2])))
311 while (chars_max < s + chars)
312 a = trealloc(char, a, chars_max<<=1);
313 if (entries == entries_max)
314 offset = trealloc(size_t, offset, entries_max<<=1);
315 offset[entries++] = chars;
316 VOID strcpy(a+chars, en);
320 # define close_directory(d) (closedir(d), 0)
322 # define close_directory(d) closedir(d)
324 if (errno || close_directory(d) != 0)
327 a = trealloc(char, a, chars);
330 *aargv = p = tnalloc(char*, entries+1);
331 for (i=0; i<entries; i++)
332 *p++ = a + offset[i];