4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * String list maintenance and binary search routines
38 #define ALLOCCHUNK 128
47 #include "binsearch.h"
52 itemlist x
= malloc(sizeof (struct itemlist
));
54 x
->nallocated
= x
->nused
= 0;
62 item_add(itemlist l
, char *s
)
64 if (l
->nallocated
< 0) {
66 l
->nallocated
= l
->nused
+ ALLOCCHUNK
;
67 new = malloc(sizeof (char *) * l
->nused
);
68 memcpy(new, l
->items
, l
->nused
* sizeof (char *));
70 } else if (l
->nallocated
== l
->nused
) {
74 l
->nallocated
= ALLOCCHUNK
;
75 l
->items
= reallocarray(l
->items
, l
->nallocated
,
78 l
->items
[l
->nused
++] = s
;
79 l
->sorted
= l
->nused
<= 1;
83 item_add_list(itemlist l
, char **s
, int n
, int alloc
)
85 if (l
->nallocated
== 0) {
87 l
->nallocated
= alloc
? n
: -1;
93 for (i
= 0; i
< n
; i
++)
102 item_addfile(itemlist l
, const char *fname
)
104 FILE *f
= fopen(fname
, "r");
110 while (fgets(buf
, sizeof (buf
), f
) != NULL
) {
111 if (buf
[0] == '#' || buf
[0] == '\n')
114 buf
[strlen(buf
)-1] = '\0';
115 item_add(l
, strdup(buf
));
123 xcmp(const void *s1
, const void *s2
)
125 return (strcmp(*(char **)s1
, *(char **)s2
));
129 item_search(itemlist l
, const char *s
)
132 int hi
= l
->nused
- 1;
135 qsort(l
->items
, l
->nused
, sizeof (char *), xcmp
);
140 int mid
= (lo
+ hi
) / 2;
141 int res
= strcmp(s
, l
->items
[mid
]);
154 *item_get(itemlist l
, int i
)
156 if (i
>= l
->nused
|| i
< 0)
159 return (l
->items
[i
]);