1236769Sobrien/* $NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $ */ 2236769Sobrien 3236769Sobrien/*- 4236769Sobrien * Copyright (c) 2008 - 2009 The NetBSD Foundation, Inc. 5236769Sobrien * All rights reserved. 6236769Sobrien * 7236769Sobrien * This code is derived from software contributed to The NetBSD Foundation 8236769Sobrien * by David Laight. 9236769Sobrien * 10236769Sobrien * Redistribution and use in source and binary forms, with or without 11236769Sobrien * modification, are permitted provided that the following conditions 12236769Sobrien * are met: 13236769Sobrien * 1. Redistributions of source code must retain the above copyright 14236769Sobrien * notice, this list of conditions and the following disclaimer. 15236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 16236769Sobrien * notice, this list of conditions and the following disclaimer in the 17236769Sobrien * documentation and/or other materials provided with the distribution. 18236769Sobrien * 3. Neither the name of The NetBSD Foundation nor the names of its 19236769Sobrien * contributors may be used to endorse or promote products derived 20236769Sobrien * from this software without specific prior written permission. 21236769Sobrien * 22236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23236769Sobrien * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24236769Sobrien * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25236769Sobrien * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26236769Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27236769Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28236769Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29236769Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30236769Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31236769Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32236769Sobrien * POSSIBILITY OF SUCH DAMAGE. 33236769Sobrien */ 34236769Sobrien 35236769Sobrien#ifndef MAKE_NATIVE 36236769Sobrienstatic char rcsid[] = "$NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $"; 37236769Sobrien#else 38236769Sobrien#include <sys/cdefs.h> 39236769Sobrien#ifndef lint 40236769Sobrien__RCSID("$NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $"); 41236769Sobrien#endif /* not lint */ 42236769Sobrien#endif 43236769Sobrien 44236769Sobrien#include <stddef.h> 45236769Sobrien#include <stdlib.h> 46236769Sobrien#include "strlist.h" 47236769Sobrien#include "make_malloc.h" 48236769Sobrien 49236769Sobrienvoid 50236769Sobrienstrlist_init(strlist_t *sl) 51236769Sobrien{ 52236769Sobrien sl->sl_num = 0; 53236769Sobrien sl->sl_max = 0; 54236769Sobrien sl->sl_items = NULL; 55236769Sobrien} 56236769Sobrien 57236769Sobrienvoid 58236769Sobrienstrlist_clean(strlist_t *sl) 59236769Sobrien{ 60236769Sobrien char *str; 61236769Sobrien int i; 62236769Sobrien 63236769Sobrien STRLIST_FOREACH(str, sl, i) 64236769Sobrien free(str); 65236769Sobrien free(sl->sl_items); 66236769Sobrien 67236769Sobrien sl->sl_num = 0; 68236769Sobrien sl->sl_max = 0; 69236769Sobrien sl->sl_items = NULL; 70236769Sobrien} 71236769Sobrien 72236769Sobrienvoid 73236769Sobrienstrlist_add_str(strlist_t *sl, char *str, unsigned int info) 74236769Sobrien{ 75236769Sobrien unsigned int n; 76236769Sobrien strlist_item_t *items; 77236769Sobrien 78236769Sobrien if (str == NULL) 79236769Sobrien return; 80236769Sobrien 81236769Sobrien n = sl->sl_num + 1; 82236769Sobrien sl->sl_num = n; 83236769Sobrien items = sl->sl_items; 84236769Sobrien if (n >= sl->sl_max) { 85236769Sobrien items = bmake_realloc(items, (n + 7) * sizeof *sl->sl_items); 86236769Sobrien sl->sl_items = items; 87236769Sobrien sl->sl_max = n + 6; 88236769Sobrien } 89236769Sobrien items += n - 1; 90236769Sobrien items->si_str = str; 91236769Sobrien items->si_info = info; 92236769Sobrien items[1].si_str = NULL; /* STRLIST_FOREACH() terminator */ 93236769Sobrien} 94