Annotation of mandoc/compat_stringlist.c, Revision 1.6
1.5 schwarze 1: #include "config.h"
2:
3: #if HAVE_STRINGLIST
4:
5: int dummy;
6:
7: #else
8:
1.6 ! schwarze 9: /* $Id: compat_stringlist.c,v 1.5 2015/05/21 00:13:43 schwarze Exp $ */
1.1 schwarze 10: /*
1.2 schwarze 11: * Copyright (c) 1994 Christos Zoulas <christos@netbsd.org>
1.1 schwarze 12: * All rights reserved.
13: *
14: * Redistribution and use in source and binary forms, with or without
15: * modification, are permitted provided that the following conditions
16: * are met:
17: * 1. Redistributions of source code must retain the above copyright
18: * notice, this list of conditions and the following disclaimer.
19: * 2. Redistributions in binary form must reproduce the above copyright
20: * notice, this list of conditions and the following disclaimer in the
21: * documentation and/or other materials provided with the distribution.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
1.6 ! schwarze 36: #if HAVE_ERR
1.1 schwarze 37: #include <err.h>
1.6 ! schwarze 38: #endif
1.1 schwarze 39: #include <stdlib.h>
1.2 schwarze 40: #include <string.h>
41: #include "compat_stringlist.h"
1.1 schwarze 42:
43: #define _SL_CHUNKSIZE 20
44:
45: /*
46: * sl_init(): Initialize a string list
47: */
48: StringList *
49: sl_init(void)
50: {
51: StringList *sl;
52:
53: sl = malloc(sizeof(StringList));
54: if (sl == NULL)
1.2 schwarze 55: err(1, "stringlist");
1.1 schwarze 56:
57: sl->sl_cur = 0;
58: sl->sl_max = _SL_CHUNKSIZE;
1.3 schwarze 59: sl->sl_str = reallocarray(NULL, sl->sl_max, sizeof(char *));
1.1 schwarze 60: if (sl->sl_str == NULL)
1.2 schwarze 61: err(1, "stringlist");
1.1 schwarze 62: return sl;
63: }
64:
65:
66: /*
67: * sl_add(): Add an item to the string list
68: */
69: int
70: sl_add(StringList *sl, char *name)
71: {
72: if (sl->sl_cur == sl->sl_max - 1) {
73: sl->sl_max += _SL_CHUNKSIZE;
1.3 schwarze 74: sl->sl_str = reallocarray(sl->sl_str,
75: sl->sl_max, sizeof(char *));
1.1 schwarze 76: if (sl->sl_str == NULL)
77: return (-1);
78: }
79: sl->sl_str[sl->sl_cur++] = name;
80: return (0);
81: }
82:
83:
84: /*
85: * sl_free(): Free a stringlist
86: */
87: void
88: sl_free(StringList *sl, int all)
89: {
90: size_t i;
91:
92: if (sl == NULL)
93: return;
94: if (sl->sl_str) {
95: if (all)
96: for (i = 0; i < sl->sl_cur; i++)
97: free(sl->sl_str[i]);
98: free(sl->sl_str);
99: }
100: free(sl);
101: }
102:
103:
104: /*
105: * sl_find(): Find a name in the string list
106: */
107: char *
108: sl_find(StringList *sl, const char *name)
109: {
110: size_t i;
111:
112: for (i = 0; i < sl->sl_cur; i++)
113: if (strcmp(sl->sl_str[i], name) == 0)
114: return sl->sl_str[i];
115:
116: return NULL;
117: }
1.5 schwarze 118:
119: #endif
CVSweb