Add sl_delete, KNF, ansi

This commit is contained in:
christos 2006-07-27 15:36:29 +00:00
parent add0e05fda
commit 6826db0f05
2 changed files with 51 additions and 26 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: stringlist.3,v 1.10 2003/04/16 13:34:39 wiz Exp $
.\" $NetBSD: stringlist.3,v 1.11 2006/07/27 15:36:29 christos Exp $
.\"
.\" Copyright (c) 1997, 1999 The NetBSD Foundation, Inc.
.\" All rights reserved.
@ -33,7 +33,7 @@
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd November 28, 1999
.Dd July 27, 2006
.Os
.Dt STRINGLIST 3
.Sh NAME
@ -41,7 +41,8 @@
.Nm sl_init ,
.Nm sl_add ,
.Nm sl_free ,
.Nm sl_find
.Nm sl_find ,
.Nm sl_delete
.Nd stringlist manipulation functions
.Sh LIBRARY
.Lb libc
@ -54,7 +55,9 @@
.Ft void
.Fn sl_free "StringList *sl" "int freeall"
.Ft char *
.Fn sl_find "StringList *sl" "char *item"
.Fn sl_find "StringList *sl" "const char *item"
.Ft int
.Fn sl_delete "StringList *sl" "const char *item" "int freeit"
.Sh DESCRIPTION
The
.Nm
@ -122,6 +125,19 @@ in
returning
.Dv NULL
if it's not found.
.It Fn sl_delete
Remove
.Ar item
from the list.
If
.Ar freeit
is non-zero, the strings is freed.
Returns
.Dv 0
if the name is found
and
.Dv \-1
if the name is not found.
.El
.Sh SEE ALSO
.Xr free 3 ,

View File

@ -1,4 +1,4 @@
/* $NetBSD: stringlist.c,v 1.10 2000/01/25 16:24:40 enami Exp $ */
/* $NetBSD: stringlist.c,v 1.11 2006/07/27 15:36:29 christos Exp $ */
/*-
* Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.
@ -38,7 +38,7 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: stringlist.c,v 1.10 2000/01/25 16:24:40 enami Exp $");
__RCSID("$NetBSD: stringlist.c,v 1.11 2006/07/27 15:36:29 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#include "namespace.h"
@ -63,13 +63,13 @@ __weak_alias(sl_init,_sl_init)
* sl_init(): Initialize a string list
*/
StringList *
sl_init()
sl_init(void)
{
StringList *sl;
sl = malloc(sizeof(StringList));
if (sl == NULL)
return (NULL);
return NULL;
sl->sl_cur = 0;
sl->sl_max = _SL_CHUNKSIZE;
@ -78,7 +78,7 @@ sl_init()
free(sl);
sl = NULL;
}
return (sl);
return sl;
}
@ -86,9 +86,7 @@ sl_init()
* sl_add(): Add an item to the string list
*/
int
sl_add(sl, name)
StringList *sl;
char *name;
sl_add(StringList *sl, char *name)
{
_DIAGASSERT(sl != NULL);
@ -96,15 +94,15 @@ sl_add(sl, name)
if (sl->sl_cur == sl->sl_max - 1) {
char **new;
new = (char **)realloc(sl->sl_str,
new = realloc(sl->sl_str,
(sl->sl_max + _SL_CHUNKSIZE) * sizeof(char *));
if (new == NULL)
return (-1);
return -1;
sl->sl_max += _SL_CHUNKSIZE;
sl->sl_str = new;
}
sl->sl_str[sl->sl_cur++] = name;
return (0);
return 0;
}
@ -112,9 +110,7 @@ sl_add(sl, name)
* sl_free(): Free a stringlist
*/
void
sl_free(sl, all)
StringList *sl;
int all;
sl_free(StringList *sl, int all)
{
size_t i;
@ -134,20 +130,33 @@ sl_free(sl, all)
* sl_find(): Find a name in the string list
*/
char *
sl_find(sl, name)
StringList *sl;
char *name;
sl_find(StringList *sl, const char *name)
{
size_t i;
_DIAGASSERT(sl != NULL);
for (i = 0; i < sl->sl_cur; i++)
/*
* XXX check sl->sl_str[i] != NULL?
*/
if (strcmp(sl->sl_str[i], name) == 0)
return (sl->sl_str[i]);
return sl->sl_str[i];
return (NULL);
return NULL;
}
int
sl_delete(StringList *sl, const char *name, int all)
{
size_t i, j;
for (i = 0; i < sl->sl_cur; i++)
if (strcmp(sl->sl_str[i], name) == 0) {
if (all)
free(sl->sl_str[i]);
for (j = i + 1; j < sl->sl_cur; j++)
sl->sl_str[j - 1] = sl->sl_str[j];
sl->sl_str[--sl->sl_cur] = NULL;
return 0;
}
return -1;
}