summaryrefslogtreecommitdiffstats
path: root/usr.bin/sup/src/stree.c
blob: 0e2287d9e79033ed944d2b857fb4c1d0494f6c5d (plain) (blame)
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*	$OpenBSD: stree.c,v 1.3 1997/01/17 07:13:21 millert Exp $	*/

/*
 * Copyright (c) 1992 Carnegie Mellon University
 * All Rights Reserved.
 * 
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
/*
 * stree.c -- SUP Tree Routines
 *
 **********************************************************************
 * HISTORY
 * $Log: stree.c,v $
 * Revision 1.3  1997/01/17 07:13:21  millert
 * r?index -> strr?chr
 *
 * Revision 1.2  1996/06/26 05:39:48  deraadt
 * rcsid
 *
 * Revision 1.1  1995/12/16 11:46:53  deraadt
 * add sup to the tree
 *
 * Revision 1.1.1.1  1993/05/21 14:52:17  cgd
 * initial import of CMU's SUP to NetBSD
 *
 * Revision 1.4  92/08/11  12:06:32  mrt
 * 	Added copyright. Delinted
 * 	[92/08/10            mrt]
 * 
 * 
 * Revision 1.3  89/08/15  15:30:57  bww
 * 	Changed code in Tlookup to Tsearch for each subpart of path.
 * 	Added indent formatting code to Tprint.
 * 	From "[89/06/24            gm0w]" at CMU.
 * 	[89/08/15            bww]
 * 
 * 20-May-87  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Added code to please lint.
 *
 * 29-Dec-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Added code to initialize new fields.  Added Tfree routine.
 *
 * 27-Sep-85  Glenn Marcy (gm0w) at Carnegie-Mellon University
 *	Created.
 *
 **********************************************************************
 */

#include <libc.h>
#include <c.h>
#include <sys/param.h>
#include "sup.h"

#define Static	/* static		/* comment for debugging */

/*************************************************************
 ***    T R E E   P R O C E S S I N G   R O U T I N E S    ***
 *************************************************************/

Tfree (t)
register TREE **t;
{
	if (*t == NULL)  return;
	Tfree (&((*t)->Tlink));
	Tfree (&((*t)->Texec));
	Tfree (&((*t)->Tlo));
	Tfree (&((*t)->Thi));
	if ((*t)->Tname)  free ((*t)->Tname);
	if ((*t)->Tuser)  free ((*t)->Tuser);
	if ((*t)->Tgroup)  free ((*t)->Tgroup);
	free (*(char **)t);
	*t = NULL;
}

Static
TREE *Tmake (p)
char *p;
{
	register TREE *t;
	t = (TREE *) malloc (sizeof (TREE));
	t->Tname = (p == NULL) ? NULL : salloc (p);
	t->Tflags = 0;
	t->Tuid = 0;
	t->Tgid = 0;
	t->Tuser = NULL;
	t->Tgroup = NULL;
	t->Tmode = 0;
	t->Tctime = 0;
	t->Tmtime = 0;
	t->Tlink = NULL;
	t->Texec = NULL;
	t->Tbf = 0;
	t->Tlo = NULL;
	t->Thi = NULL;
	return (t);
}

Static
TREE *Trotll (tp,tl)
register TREE *tp,*tl;
{
    tp->Tlo = tl->Thi;
    tl->Thi = tp;
    tp->Tbf = tl->Tbf = 0;
    return(tl);
}

Static
TREE *Trotlh (tp,tl)
register TREE *tp,*tl;
{
    register TREE *th;

    th = tl->Thi;
    tp->Tlo = th->Thi;
    tl->Thi = th->Tlo;
    th->Thi = tp;
    th->Tlo = tl;
    tp->Tbf = tl->Tbf = 0;
    if (th->Tbf == 1)
	tp->Tbf = -1;
    else if (th->Tbf == -1)
	tl->Tbf = 1;
    th->Tbf = 0;
    return(th);
}

Static
TREE *Trothl (tp,th)
register TREE *tp,*th;
{
    register TREE *tl;

    tl = th->Tlo;
    tp->Thi = tl->Tlo;
    th->Tlo = tl->Thi;
    tl->Tlo = tp;
    tl->Thi = th;
    tp->Tbf = th->Tbf = 0;
    if (tl->Tbf == -1)
	tp->Tbf = 1;
    else if (tl->Tbf == 1)
	th->Tbf = -1;
    tl->Tbf = 0;
    return(tl);
}

Static
TREE *Trothh (tp,th)
register TREE *tp,*th;
{
    tp->Thi = th->Tlo;
    th->Tlo = tp;
    tp->Tbf = th->Tbf = 0;
    return(th);
}

Static
Tbalance (t)
TREE **t;
{
    if ((*t)->Tbf < 2 && (*t)->Tbf > -2)
	return;
    if ((*t)->Tbf > 0) {
	if ((*t)->Tlo->Tbf > 0)
	    *t = Trotll(*t, (*t)->Tlo);
	else
	    *t = Trotlh(*t, (*t)->Tlo);
    } else {
	if ((*t)->Thi->Tbf > 0)
	    *t = Trothl(*t, (*t)->Thi);
	else
	    *t = Trothh(*t, (*t)->Thi);
    }
}

Static
TREE *Tinsertavl (t,p,find,dh)
TREE **t;
char *p;
int find;
int *dh;
{
	register TREE *newt;
	register int cmp;
	int deltah;

	if (*t == NULL) {
	    *t = Tmake (p);
	    *dh = 1;
	    return (*t);
	}
	if ((cmp = strcmp(p, (*t)->Tname)) == 0) {
	    if (!find)  return (NULL);	/* node already exists */
	    *dh = 0;
	    return (*t);
	} else if (cmp < 0) {
	    if ((newt = Tinsertavl (&((*t)->Tlo),p,find,&deltah)) == NULL)
		return (NULL);
	    (*t)->Tbf += deltah;
	} else {
	    if ((newt = Tinsertavl (&((*t)->Thi),p,find,&deltah)) == NULL)
		return (NULL);
	    (*t)->Tbf -= deltah;
	}
	Tbalance(t);
	if ((*t)->Tbf == 0) deltah = 0;
	*dh = deltah;
	return (newt);
}

TREE *Tinsert (t,p,find)
TREE **t;
register char *p;
int find;
{
	int deltah;

	if (p != NULL && p[0] == '.' && p[1] == '/') {
		p += 2;
		while (*p == '/') p++;
		if (*p == 0) p = ".";
	}
	return (Tinsertavl (t,p,find,&deltah));
}

TREE *Tsearch (t,p)
TREE *t;
char *p;
{
	register TREE *x;
	register int cmp;

	x = t;
	while (x) {
		cmp = strcmp (p,x->Tname);
		if (cmp == 0)  return (x);
		if (cmp < 0)	x = x->Tlo;
		else		x = x->Thi;
	}
	return (NULL);
}

TREE *Tlookup (t,p)
TREE *t;
char *p;
{
	register TREE *x;
	char buf[MAXPATHLEN+1];

	if (p == NULL)
		return (NULL);
	if (p[0] == '.' && p[1] == '/') {
		p += 2;
		while (*p == '/') p++;
		if (*p == 0) p = ".";
	}
	if ((x = Tsearch (t,p)) != NULL)
		return (x);
	if (*p != '/' && (x = Tsearch (t,".")) != NULL)
		return (x);
	(void) strncpy(buf, p, sizeof(buf)-1);
	buf[MAXPATHLEN] = '\0';
	while ((p = strrchr(buf, '/')) != NULL) {
		while (p >= buf && *(p-1) == '/')
			p--;
		if (p == buf)
			*(p+1) = '\0';
		else
			*p = '\0';
		if ((x = Tsearch (t,buf)) != NULL)
			return (x);
		if (p == buf)
			break;
	}
	return (NULL);
}

Static int process_level;

Static
int Tsubprocess (t,reverse,f,argp)
TREE *t;
int reverse;
int (*f)();
int *argp;
{
	register int x = SCMOK;
	process_level++;
	if (reverse?t->Thi:t->Tlo)
		x = Tsubprocess (reverse?t->Thi:t->Tlo,
				 reverse,f,argp);
	if (x == SCMOK) {
		x = (*f) (t,argp);
		if (x == SCMOK) {
			if (reverse?t->Tlo:t->Thi)
				x = Tsubprocess (reverse?t->Tlo:t->Thi,
						 reverse,f,argp);
		}
	}
	process_level--;
	return (x);
}

/* VARARGS2 */
int Trprocess (t,f,args)
TREE *t;
int (*f)();
int args;
{
	if (t == NULL)  return (SCMOK);
	process_level = 0;
	return (Tsubprocess (t,TRUE,f,&args));
}

/* VARARGS2 */
int Tprocess (t,f,args)
TREE *t;
int (*f)();
int args;
{
	if (t == NULL)  return (SCMOK);
	process_level = 0;
	return (Tsubprocess (t,FALSE,f,&args));
}

Static
int Tprintone (t)
TREE *t;
{
	int i;
	for (i = 0; i < (process_level*2); i++)
		(void) putchar(' ');
	printf ("Node at %X name '%s' flags %o hi %X lo %X\n",t,t->Tname,t->Tflags,t->Thi,t->Tlo);
	return (SCMOK);
}

Tprint (t,p)		/* print tree -- for debugging */
TREE *t;
char *p;
{
	printf ("%s\n",p);
	(void) Tprocess (t,Tprintone);
	printf ("End of tree\n");
	(void) fflush (stdout);
}