stringlist.c (165903) | stringlist.c (249801) |
---|---|
1/* 2 * Copyright (c) 1994 Christos Zoulas 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 16 unchanged lines hidden (view full) --- 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#if defined(LIBC_SCCS) && !defined(lint) 30static char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $"; 31#endif /* LIBC_SCCS and not lint */ 32#include <sys/cdefs.h> | 1/* 2 * Copyright (c) 1994 Christos Zoulas 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 16 unchanged lines hidden (view full) --- 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#if defined(LIBC_SCCS) && !defined(lint) 30static char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $"; 31#endif /* LIBC_SCCS and not lint */ 32#include <sys/cdefs.h> |
33__FBSDID("$FreeBSD: head/lib/libc/gen/stringlist.c 165903 2007-01-09 00:28:16Z imp $"); | 33__FBSDID("$FreeBSD: head/lib/libc/gen/stringlist.c 249801 2013-04-23 13:03:01Z eadler $"); |
34 35#include "namespace.h" 36#include <stdio.h> 37#include <string.h> 38#include <err.h> 39#include <stdlib.h> 40#include <stringlist.h> 41#include "un-namespace.h" 42 43#define _SL_CHUNKSIZE 20 44 45/* 46 * sl_init(): Initialize a string list 47 */ 48StringList * | 34 35#include "namespace.h" 36#include <stdio.h> 37#include <string.h> 38#include <err.h> 39#include <stdlib.h> 40#include <stringlist.h> 41#include "un-namespace.h" 42 43#define _SL_CHUNKSIZE 20 44 45/* 46 * sl_init(): Initialize a string list 47 */ 48StringList * |
49sl_init() | 49sl_init(void) |
50{ 51 StringList *sl; 52 53 sl = malloc(sizeof(StringList)); 54 if (sl == NULL) 55 _err(1, "stringlist: %m"); 56 57 sl->sl_cur = 0; --- 4 unchanged lines hidden (view full) --- 62 return sl; 63} 64 65 66/* 67 * sl_add(): Add an item to the string list 68 */ 69int | 50{ 51 StringList *sl; 52 53 sl = malloc(sizeof(StringList)); 54 if (sl == NULL) 55 _err(1, "stringlist: %m"); 56 57 sl->sl_cur = 0; --- 4 unchanged lines hidden (view full) --- 62 return sl; 63} 64 65 66/* 67 * sl_add(): Add an item to the string list 68 */ 69int |
70sl_add(sl, name) 71 StringList *sl; 72 char *name; | 70sl_add(StringList *sl, char *name) |
73{ 74 if (sl->sl_cur == sl->sl_max - 1) { 75 sl->sl_max += _SL_CHUNKSIZE; 76 sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); 77 if (sl->sl_str == NULL) 78 return (-1); 79 } 80 sl->sl_str[sl->sl_cur++] = name; 81 return (0); 82} 83 84 85/* 86 * sl_free(): Free a stringlist 87 */ 88void | 71{ 72 if (sl->sl_cur == sl->sl_max - 1) { 73 sl->sl_max += _SL_CHUNKSIZE; 74 sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); 75 if (sl->sl_str == NULL) 76 return (-1); 77 } 78 sl->sl_str[sl->sl_cur++] = name; 79 return (0); 80} 81 82 83/* 84 * sl_free(): Free a stringlist 85 */ 86void |
89sl_free(sl, all) 90 StringList *sl; 91 int all; | 87sl_free(StringList *sl, int all) |
92{ 93 size_t i; 94 95 if (sl == NULL) 96 return; 97 if (sl->sl_str) { 98 if (all) 99 for (i = 0; i < sl->sl_cur; i++) 100 free(sl->sl_str[i]); 101 free(sl->sl_str); 102 } 103 free(sl); 104} 105 106 107/* 108 * sl_find(): Find a name in the string list 109 */ 110char * | 88{ 89 size_t i; 90 91 if (sl == NULL) 92 return; 93 if (sl->sl_str) { 94 if (all) 95 for (i = 0; i < sl->sl_cur; i++) 96 free(sl->sl_str[i]); 97 free(sl->sl_str); 98 } 99 free(sl); 100} 101 102 103/* 104 * sl_find(): Find a name in the string list 105 */ 106char * |
111sl_find(sl, name) 112 StringList *sl; 113 char *name; | 107sl_find(StringList *sl, char *name) |
114{ 115 size_t i; 116 117 for (i = 0; i < sl->sl_cur; i++) 118 if (strcmp(sl->sl_str[i], name) == 0) 119 return sl->sl_str[i]; 120 121 return NULL; 122} | 108{ 109 size_t i; 110 111 for (i = 0; i < sl->sl_cur; i++) 112 if (strcmp(sl->sl_str[i], name) == 0) 113 return sl->sl_str[i]; 114 115 return NULL; 116} |