1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 1993-2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <string.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate #include <sys/param.h>
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #include "list.h"
35*0Sstevel@tonic-gate #include "proto_list.h"
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate /* LINTLIBRARY */
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate int max_list_depth;
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate void
init_list(elem_list * list,int hsize)42*0Sstevel@tonic-gate init_list(elem_list *list, int hsize)
43*0Sstevel@tonic-gate {
44*0Sstevel@tonic-gate int i;
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate list->type = 0;
47*0Sstevel@tonic-gate list->list = (elem**)malloc(sizeof (elem *) * hsize);
48*0Sstevel@tonic-gate list->num_of_buckets = hsize;
49*0Sstevel@tonic-gate for (i = 0; i < list->num_of_buckets; i++)
50*0Sstevel@tonic-gate list->list[i] = NULL;
51*0Sstevel@tonic-gate }
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate #ifdef DEBUG
54*0Sstevel@tonic-gate void
examine_list(elem_list * list)55*0Sstevel@tonic-gate examine_list(elem_list *list)
56*0Sstevel@tonic-gate {
57*0Sstevel@tonic-gate int i;
58*0Sstevel@tonic-gate elem *cur;
59*0Sstevel@tonic-gate int buck_count;
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate for (i = 0; i < list->num_of_buckets; i++) {
62*0Sstevel@tonic-gate buck_count = 0;
63*0Sstevel@tonic-gate for (cur = list->list[i]; cur; cur = cur->next)
64*0Sstevel@tonic-gate buck_count++;
65*0Sstevel@tonic-gate (void) printf("bucket[%4d] contains %5d entries\n",
66*0Sstevel@tonic-gate i, buck_count);
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate }
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate * print all elements of a list
73*0Sstevel@tonic-gate *
74*0Sstevel@tonic-gate * Debugging routine
75*0Sstevel@tonic-gate */
76*0Sstevel@tonic-gate void
print_list(elem_list * list)77*0Sstevel@tonic-gate print_list(elem_list *list)
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate int i;
80*0Sstevel@tonic-gate elem *cur;
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate for (i = 0; i < list->num_of_buckets; i++) {
83*0Sstevel@tonic-gate for (cur = list->list[i]; cur; cur = cur->next)
84*0Sstevel@tonic-gate print_elem(stdout, cur);
85*0Sstevel@tonic-gate }
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate
89*0Sstevel@tonic-gate /*
90*0Sstevel@tonic-gate * print all elements of a list of type 'file_type'
91*0Sstevel@tonic-gate *
92*0Sstevel@tonic-gate * Debugging routine
93*0Sstevel@tonic-gate */
94*0Sstevel@tonic-gate void
print_type_list(elem_list * list,char file_type)95*0Sstevel@tonic-gate print_type_list(elem_list *list, char file_type)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate int i;
98*0Sstevel@tonic-gate elem *cur;
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate for (i = 0; i < list->num_of_buckets; i++) {
101*0Sstevel@tonic-gate for (cur = list->list[i]; cur; cur = cur->next) {
102*0Sstevel@tonic-gate if (cur->file_type == file_type)
103*0Sstevel@tonic-gate print_elem(stdout, cur);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate #endif
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate unsigned int
hash(const char * str)110*0Sstevel@tonic-gate hash(const char *str)
111*0Sstevel@tonic-gate {
112*0Sstevel@tonic-gate unsigned int i;
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate for (i = 0; *str != '\0'; )
115*0Sstevel@tonic-gate i += *str++;
116*0Sstevel@tonic-gate return (i);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate static int
name_compare(elem * i,elem * j)121*0Sstevel@tonic-gate name_compare(elem *i, elem *j)
122*0Sstevel@tonic-gate {
123*0Sstevel@tonic-gate int n;
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate if ((n = strncmp(i->name, j->name, MAXNAME)) != 0)
126*0Sstevel@tonic-gate return (n);
127*0Sstevel@tonic-gate else
128*0Sstevel@tonic-gate return (j->arch - i->arch);
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate /*
133*0Sstevel@tonic-gate * find_elem:
134*0Sstevel@tonic-gate *
135*0Sstevel@tonic-gate * possible values for flag.
136*0Sstevel@tonic-gate * flag = FOLLOW_LINK
137*0Sstevel@tonic-gate * flag = NO_FOLLOW_LINK
138*0Sstevel@tonic-gate */
139*0Sstevel@tonic-gate elem *
find_elem(elem_list * list,elem * key,int flag)140*0Sstevel@tonic-gate find_elem(elem_list *list, elem *key, int flag)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate elem *e;
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate for (e = list->list[hash(key->name) % list->num_of_buckets]; e;
145*0Sstevel@tonic-gate e = e->next) {
146*0Sstevel@tonic-gate if (!name_compare(e, key))
147*0Sstevel@tonic-gate if (e->link_parent && flag == FOLLOW_LINK)
148*0Sstevel@tonic-gate return (e->link_parent);
149*0Sstevel@tonic-gate else
150*0Sstevel@tonic-gate return (e);
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate return (NULL);
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate * find_elem_isa:
159*0Sstevel@tonic-gate *
160*0Sstevel@tonic-gate * flags - same as find_elem()
161*0Sstevel@tonic-gate */
162*0Sstevel@tonic-gate elem *
find_elem_isa(elem_list * list,elem * key,int flag)163*0Sstevel@tonic-gate find_elem_isa(elem_list *list, elem *key, int flag)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate short orig_arch;
166*0Sstevel@tonic-gate elem *e;
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate orig_arch = key->arch;
169*0Sstevel@tonic-gate key->arch = P_ISA;
170*0Sstevel@tonic-gate e = find_elem(list, key, flag);
171*0Sstevel@tonic-gate key->arch = orig_arch;
172*0Sstevel@tonic-gate return (e);
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gate /*
176*0Sstevel@tonic-gate * find_elem_mach:
177*0Sstevel@tonic-gate *
178*0Sstevel@tonic-gate * flags - same as find_elem()
179*0Sstevel@tonic-gate */
180*0Sstevel@tonic-gate elem *
find_elem_mach(elem_list * list,elem * key,int flag)181*0Sstevel@tonic-gate find_elem_mach(elem_list *list, elem *key, int flag)
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate elem *e;
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate for (e = list->list[hash(key->name) % list->num_of_buckets]; e;
186*0Sstevel@tonic-gate e = e->next) {
187*0Sstevel@tonic-gate if ((e->arch != P_ISA) && (strcmp(key->name, e->name) == 0))
188*0Sstevel@tonic-gate if (e->link_parent && flag == FOLLOW_LINK)
189*0Sstevel@tonic-gate return (e->link_parent);
190*0Sstevel@tonic-gate else
191*0Sstevel@tonic-gate return (e);
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate return (NULL);
195*0Sstevel@tonic-gate }
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate pkg_list *
add_pkg(pkg_list * head,const char * pkgname)198*0Sstevel@tonic-gate add_pkg(pkg_list *head, const char *pkgname)
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate pkg_list *cur, *prev = NULL;
201*0Sstevel@tonic-gate static pkg_list *new = NULL;
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate if (!new)
204*0Sstevel@tonic-gate new = (pkg_list *)malloc(sizeof (pkg_list));
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate (void) strcpy(new->pkg_name, pkgname);
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gate for (cur = head; cur; cur = cur->next) {
209*0Sstevel@tonic-gate if (strcmp(cur->pkg_name, pkgname) >= 0)
210*0Sstevel@tonic-gate break;
211*0Sstevel@tonic-gate prev = cur;
212*0Sstevel@tonic-gate }
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate if (!head) {
215*0Sstevel@tonic-gate new->next = head;
216*0Sstevel@tonic-gate head = new;
217*0Sstevel@tonic-gate new = NULL;
218*0Sstevel@tonic-gate return (head);
219*0Sstevel@tonic-gate }
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate if (!cur) {
222*0Sstevel@tonic-gate prev->next = new;
223*0Sstevel@tonic-gate new->next = NULL;
224*0Sstevel@tonic-gate new = NULL;
225*0Sstevel@tonic-gate return (head);
226*0Sstevel@tonic-gate }
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate if (strcmp(cur->pkg_name, pkgname) == 0) /* a duplicate */
229*0Sstevel@tonic-gate return (NULL);
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gate if (!prev) {
232*0Sstevel@tonic-gate new->next = cur;
233*0Sstevel@tonic-gate cur = new;
234*0Sstevel@tonic-gate new = NULL;
235*0Sstevel@tonic-gate return (cur);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gate prev->next = new;
239*0Sstevel@tonic-gate new->next = cur;
240*0Sstevel@tonic-gate new = NULL;
241*0Sstevel@tonic-gate return (head);
242*0Sstevel@tonic-gate }
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate void
add_elem(elem_list * list,elem * e)245*0Sstevel@tonic-gate add_elem(elem_list *list, elem *e)
246*0Sstevel@tonic-gate {
247*0Sstevel@tonic-gate elem *last = NULL;
248*0Sstevel@tonic-gate elem *cur;
249*0Sstevel@tonic-gate int bucket;
250*0Sstevel@tonic-gate int depth = 0;
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate bucket = hash(e->name) % list->num_of_buckets;
253*0Sstevel@tonic-gate if (list->list[bucket]) {
254*0Sstevel@tonic-gate for (cur = list->list[bucket]; cur; cur = cur->next) {
255*0Sstevel@tonic-gate depth++;
256*0Sstevel@tonic-gate if (strcmp(cur->name, e->name) > 0)
257*0Sstevel@tonic-gate break;
258*0Sstevel@tonic-gate last = cur;
259*0Sstevel@tonic-gate }
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate if (last) {
262*0Sstevel@tonic-gate if (depth > max_list_depth)
263*0Sstevel@tonic-gate max_list_depth = depth;
264*0Sstevel@tonic-gate last->next = e;
265*0Sstevel@tonic-gate e->next = cur;
266*0Sstevel@tonic-gate return;
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate /*
271*0Sstevel@tonic-gate * insert at head of list
272*0Sstevel@tonic-gate */
273*0Sstevel@tonic-gate e->next = list->list[bucket];
274*0Sstevel@tonic-gate list->list[bucket] = e;
275*0Sstevel@tonic-gate if (depth > max_list_depth)
276*0Sstevel@tonic-gate max_list_depth = depth;
277*0Sstevel@tonic-gate }
278