1*95c635efSGarrett D'Amore /*
2*95c635efSGarrett D'Amore * Copyright (c) 1994 Christos Zoulas
3*95c635efSGarrett D'Amore * All rights reserved.
4*95c635efSGarrett D'Amore *
5*95c635efSGarrett D'Amore * Redistribution and use in source and binary forms, with or without
6*95c635efSGarrett D'Amore * modification, are permitted provided that the following conditions
7*95c635efSGarrett D'Amore * are met:
8*95c635efSGarrett D'Amore * 1. Redistributions of source code must retain the above copyright
9*95c635efSGarrett D'Amore * notice, this list of conditions and the following disclaimer.
10*95c635efSGarrett D'Amore * 2. Redistributions in binary form must reproduce the above copyright
11*95c635efSGarrett D'Amore * notice, this list of conditions and the following disclaimer in the
12*95c635efSGarrett D'Amore * documentation and/or other materials provided with the distribution.
13*95c635efSGarrett D'Amore * 4. The name of the author may not be used to endorse or promote products
14*95c635efSGarrett D'Amore * derived from this software without specific prior written permission.
15*95c635efSGarrett D'Amore *
16*95c635efSGarrett D'Amore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17*95c635efSGarrett D'Amore * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18*95c635efSGarrett D'Amore * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*95c635efSGarrett D'Amore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20*95c635efSGarrett D'Amore * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*95c635efSGarrett D'Amore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*95c635efSGarrett D'Amore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*95c635efSGarrett D'Amore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*95c635efSGarrett D'Amore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*95c635efSGarrett D'Amore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*95c635efSGarrett D'Amore * SUCH DAMAGE.
27*95c635efSGarrett D'Amore */
28*95c635efSGarrett D'Amore
29*95c635efSGarrett D'Amore /*
30*95c635efSGarrett D'Amore * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
31*95c635efSGarrett D'Amore */
32*95c635efSGarrett D'Amore
33*95c635efSGarrett D'Amore #include <err.h>
34*95c635efSGarrett D'Amore #include <stdio.h>
35*95c635efSGarrett D'Amore #include <stdlib.h>
36*95c635efSGarrett D'Amore #include <string.h>
37*95c635efSGarrett D'Amore
38*95c635efSGarrett D'Amore #include "stringlist.h"
39*95c635efSGarrett D'Amore
40*95c635efSGarrett D'Amore #define _SL_CHUNKSIZE 20
41*95c635efSGarrett D'Amore
42*95c635efSGarrett D'Amore stringlist *
sl_init(void)43*95c635efSGarrett D'Amore sl_init(void)
44*95c635efSGarrett D'Amore {
45*95c635efSGarrett D'Amore stringlist *sl;
46*95c635efSGarrett D'Amore
47*95c635efSGarrett D'Amore if ((sl = malloc(sizeof (stringlist))) == NULL)
48*95c635efSGarrett D'Amore err(1, "malloc");
49*95c635efSGarrett D'Amore
50*95c635efSGarrett D'Amore sl->sl_cur = 0;
51*95c635efSGarrett D'Amore sl->sl_max = _SL_CHUNKSIZE;
52*95c635efSGarrett D'Amore sl->sl_str = malloc(sl->sl_max * sizeof (char *));
53*95c635efSGarrett D'Amore if (sl->sl_str == NULL)
54*95c635efSGarrett D'Amore err(1, "malloc");
55*95c635efSGarrett D'Amore
56*95c635efSGarrett D'Amore return (sl);
57*95c635efSGarrett D'Amore }
58*95c635efSGarrett D'Amore
59*95c635efSGarrett D'Amore int
sl_add(stringlist * sl,char * name)60*95c635efSGarrett D'Amore sl_add(stringlist *sl, char *name)
61*95c635efSGarrett D'Amore {
62*95c635efSGarrett D'Amore
63*95c635efSGarrett D'Amore if (sl->sl_cur == sl->sl_max - 1) {
64*95c635efSGarrett D'Amore sl->sl_max += _SL_CHUNKSIZE;
65*95c635efSGarrett D'Amore sl->sl_str = realloc(sl->sl_str, sl->sl_max * sizeof (char *));
66*95c635efSGarrett D'Amore if (sl->sl_str == NULL)
67*95c635efSGarrett D'Amore return (-1);
68*95c635efSGarrett D'Amore }
69*95c635efSGarrett D'Amore sl->sl_str[sl->sl_cur++] = name;
70*95c635efSGarrett D'Amore
71*95c635efSGarrett D'Amore return (0);
72*95c635efSGarrett D'Amore }
73*95c635efSGarrett D'Amore
74*95c635efSGarrett D'Amore
75*95c635efSGarrett D'Amore void
sl_free(stringlist * sl,int all)76*95c635efSGarrett D'Amore sl_free(stringlist *sl, int all)
77*95c635efSGarrett D'Amore {
78*95c635efSGarrett D'Amore size_t i;
79*95c635efSGarrett D'Amore
80*95c635efSGarrett D'Amore if (sl == NULL)
81*95c635efSGarrett D'Amore return;
82*95c635efSGarrett D'Amore if (sl->sl_str) {
83*95c635efSGarrett D'Amore if (all)
84*95c635efSGarrett D'Amore for (i = 0; i < sl->sl_cur; i++)
85*95c635efSGarrett D'Amore free(sl->sl_str[i]);
86*95c635efSGarrett D'Amore free(sl->sl_str);
87*95c635efSGarrett D'Amore }
88*95c635efSGarrett D'Amore free(sl);
89*95c635efSGarrett D'Amore }
90*95c635efSGarrett D'Amore
91*95c635efSGarrett D'Amore
92*95c635efSGarrett D'Amore char *
sl_find(stringlist * sl,char * name)93*95c635efSGarrett D'Amore sl_find(stringlist *sl, char *name)
94*95c635efSGarrett D'Amore {
95*95c635efSGarrett D'Amore size_t i;
96*95c635efSGarrett D'Amore
97*95c635efSGarrett D'Amore for (i = 0; i < sl->sl_cur; i++)
98*95c635efSGarrett D'Amore if (strcmp(sl->sl_str[i], name) == 0)
99*95c635efSGarrett D'Amore return (sl->sl_str[i]);
100*95c635efSGarrett D'Amore
101*95c635efSGarrett D'Amore return (NULL);
102*95c635efSGarrett D'Amore }
103