2000-01-25 19:24:40 +03:00
|
|
|
/* $NetBSD: stringlist.c,v 1.10 2000/01/25 16:24:40 enami Exp $ */
|
1997-01-17 10:26:18 +03:00
|
|
|
|
1999-11-27 09:19:32 +03:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.
|
1997-01-17 10:26:18 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
1999-11-27 09:19:32 +03:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Christos Zoulas.
|
|
|
|
*
|
1997-01-17 10:26:18 +03:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1999-11-27 09:19:32 +03:00
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
1997-01-17 10:26:18 +03:00
|
|
|
*
|
1999-11-27 09:19:32 +03:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
1997-01-17 10:26:18 +03:00
|
|
|
*/
|
|
|
|
|
1997-07-13 23:31:49 +04:00
|
|
|
#include <sys/cdefs.h>
|
1997-01-17 10:26:18 +03:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
2000-01-25 19:24:40 +03:00
|
|
|
__RCSID("$NetBSD: stringlist.c,v 1.10 2000/01/25 16:24:40 enami Exp $");
|
1997-01-17 10:26:18 +03:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
1997-07-13 23:31:49 +04:00
|
|
|
#include "namespace.h"
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
#include <assert.h>
|
1997-01-17 10:26:18 +03:00
|
|
|
#include <err.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <stdio.h>
|
1997-01-17 10:26:18 +03:00
|
|
|
#include <stdlib.h>
|
1999-09-16 15:44:54 +04:00
|
|
|
#include <string.h>
|
1997-01-17 10:26:18 +03:00
|
|
|
#include <stringlist.h>
|
|
|
|
|
1997-07-21 18:06:24 +04:00
|
|
|
#ifdef __weak_alias
|
2000-01-23 01:19:07 +03:00
|
|
|
__weak_alias(sl_add,_sl_add)
|
|
|
|
__weak_alias(sl_find,_sl_find)
|
|
|
|
__weak_alias(sl_free,_sl_free)
|
|
|
|
__weak_alias(sl_init,_sl_init)
|
1997-07-21 18:06:24 +04:00
|
|
|
#endif
|
|
|
|
|
1997-01-17 10:26:18 +03:00
|
|
|
#define _SL_CHUNKSIZE 20
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sl_init(): Initialize a string list
|
|
|
|
*/
|
|
|
|
StringList *
|
|
|
|
sl_init()
|
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
StringList *sl;
|
|
|
|
|
|
|
|
sl = malloc(sizeof(StringList));
|
1997-01-17 10:26:18 +03:00
|
|
|
if (sl == NULL)
|
1999-11-28 06:44:09 +03:00
|
|
|
return (NULL);
|
1997-01-17 10:26:18 +03:00
|
|
|
|
|
|
|
sl->sl_cur = 0;
|
|
|
|
sl->sl_max = _SL_CHUNKSIZE;
|
|
|
|
sl->sl_str = malloc(sl->sl_max * sizeof(char *));
|
1999-11-28 06:44:09 +03:00
|
|
|
if (sl->sl_str == NULL) {
|
|
|
|
free(sl);
|
|
|
|
sl = NULL;
|
|
|
|
}
|
|
|
|
return (sl);
|
1997-01-17 10:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sl_add(): Add an item to the string list
|
|
|
|
*/
|
1999-11-28 06:44:09 +03:00
|
|
|
int
|
1997-01-17 10:26:18 +03:00
|
|
|
sl_add(sl, name)
|
|
|
|
StringList *sl;
|
|
|
|
char *name;
|
|
|
|
{
|
1999-09-16 15:44:54 +04:00
|
|
|
|
|
|
|
_DIAGASSERT(sl != NULL);
|
|
|
|
|
1997-01-17 10:26:18 +03:00
|
|
|
if (sl->sl_cur == sl->sl_max - 1) {
|
1999-11-28 06:44:09 +03:00
|
|
|
char **new;
|
|
|
|
|
2000-01-25 19:24:40 +03:00
|
|
|
new = (char **)realloc(sl->sl_str,
|
|
|
|
(sl->sl_max + _SL_CHUNKSIZE) * sizeof(char *));
|
1999-11-28 06:44:09 +03:00
|
|
|
if (new == NULL)
|
|
|
|
return (-1);
|
2000-01-25 19:24:40 +03:00
|
|
|
sl->sl_max += _SL_CHUNKSIZE;
|
1999-11-28 06:44:09 +03:00
|
|
|
sl->sl_str = new;
|
1997-01-17 10:26:18 +03:00
|
|
|
}
|
|
|
|
sl->sl_str[sl->sl_cur++] = name;
|
1999-11-28 06:44:09 +03:00
|
|
|
return (0);
|
1997-01-17 10:26:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sl_free(): Free a stringlist
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sl_free(sl, all)
|
|
|
|
StringList *sl;
|
|
|
|
int all;
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (sl == NULL)
|
|
|
|
return;
|
|
|
|
if (sl->sl_str) {
|
|
|
|
if (all)
|
|
|
|
for (i = 0; i < sl->sl_cur; i++)
|
|
|
|
free(sl->sl_str[i]);
|
|
|
|
free(sl->sl_str);
|
|
|
|
}
|
|
|
|
free(sl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sl_find(): Find a name in the string list
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
sl_find(sl, name)
|
|
|
|
StringList *sl;
|
|
|
|
char *name;
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
1999-09-16 15:44:54 +04:00
|
|
|
_DIAGASSERT(sl != NULL);
|
|
|
|
|
1997-01-17 10:26:18 +03:00
|
|
|
for (i = 0; i < sl->sl_cur; i++)
|
1999-09-16 15:44:54 +04:00
|
|
|
/*
|
|
|
|
* XXX check sl->sl_str[i] != NULL?
|
|
|
|
*/
|
1997-01-17 10:26:18 +03:00
|
|
|
if (strcmp(sl->sl_str[i], name) == 0)
|
2000-01-25 19:24:40 +03:00
|
|
|
return (sl->sl_str[i]);
|
1997-01-17 10:26:18 +03:00
|
|
|
|
1999-11-28 06:44:09 +03:00
|
|
|
return (NULL);
|
1997-01-17 10:26:18 +03:00
|
|
|
}
|