113e3f4d6SMark Murray /* 2*ae771770SStanislav Sedov * Copyright (c) 1998, 1999 Kungliga Tekniska Högskolan 313e3f4d6SMark Murray * (Royal Institute of Technology, Stockholm, Sweden). 413e3f4d6SMark Murray * All rights reserved. 513e3f4d6SMark Murray * 613e3f4d6SMark Murray * Redistribution and use in source and binary forms, with or without 713e3f4d6SMark Murray * modification, are permitted provided that the following conditions 813e3f4d6SMark Murray * are met: 913e3f4d6SMark Murray * 1013e3f4d6SMark Murray * 1. Redistributions of source code must retain the above copyright 1113e3f4d6SMark Murray * notice, this list of conditions and the following disclaimer. 1213e3f4d6SMark Murray * 1313e3f4d6SMark Murray * 2. Redistributions in binary form must reproduce the above copyright 1413e3f4d6SMark Murray * notice, this list of conditions and the following disclaimer in the 1513e3f4d6SMark Murray * documentation and/or other materials provided with the distribution. 1613e3f4d6SMark Murray * 1713e3f4d6SMark Murray * 3. Neither the name of the Institute nor the names of its contributors 1813e3f4d6SMark Murray * may be used to endorse or promote products derived from this software 1913e3f4d6SMark Murray * without specific prior written permission. 2013e3f4d6SMark Murray * 2113e3f4d6SMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 2213e3f4d6SMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2313e3f4d6SMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2413e3f4d6SMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 2513e3f4d6SMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2613e3f4d6SMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2713e3f4d6SMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2813e3f4d6SMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2913e3f4d6SMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3013e3f4d6SMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3113e3f4d6SMark Murray * SUCH DAMAGE. 3213e3f4d6SMark Murray */ 3313e3f4d6SMark Murray 3413e3f4d6SMark Murray #include <config.h> 3513e3f4d6SMark Murray 3613e3f4d6SMark Murray #include <stdarg.h> 3713e3f4d6SMark Murray #include <stdlib.h> 3813e3f4d6SMark Murray #include <string.h> 3913e3f4d6SMark Murray #include <errno.h> 40c19800e8SDoug Rabson #include "roken.h" 4113e3f4d6SMark Murray 4213e3f4d6SMark Murray enum { initial = 10, increment = 5 }; 4313e3f4d6SMark Murray 4413e3f4d6SMark Murray static char ** 4513e3f4d6SMark Murray sub (char **argv, int i, int argc, va_list *ap) 4613e3f4d6SMark Murray { 4713e3f4d6SMark Murray do { 4813e3f4d6SMark Murray if(i == argc) { 4913e3f4d6SMark Murray /* realloc argv */ 5013e3f4d6SMark Murray char **tmp = realloc(argv, (argc + increment) * sizeof(*argv)); 5113e3f4d6SMark Murray if(tmp == NULL) { 5213e3f4d6SMark Murray free(argv); 5313e3f4d6SMark Murray errno = ENOMEM; 5413e3f4d6SMark Murray return NULL; 5513e3f4d6SMark Murray } 5613e3f4d6SMark Murray argv = tmp; 5713e3f4d6SMark Murray argc += increment; 5813e3f4d6SMark Murray } 5913e3f4d6SMark Murray argv[i++] = va_arg(*ap, char*); 6013e3f4d6SMark Murray } while(argv[i - 1] != NULL); 6113e3f4d6SMark Murray return argv; 6213e3f4d6SMark Murray } 6313e3f4d6SMark Murray 6413e3f4d6SMark Murray /* 6513e3f4d6SMark Murray * return a malloced vector of pointers to the strings in `ap' 6613e3f4d6SMark Murray * terminated by NULL. 6713e3f4d6SMark Murray */ 6813e3f4d6SMark Murray 69*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION char ** ROKEN_LIB_CALL 7013e3f4d6SMark Murray vstrcollect(va_list *ap) 7113e3f4d6SMark Murray { 7213e3f4d6SMark Murray return sub (NULL, 0, 0, ap); 7313e3f4d6SMark Murray } 7413e3f4d6SMark Murray 7513e3f4d6SMark Murray /* 7613e3f4d6SMark Murray * 7713e3f4d6SMark Murray */ 7813e3f4d6SMark Murray 79*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION char ** ROKEN_LIB_CALL 8013e3f4d6SMark Murray strcollect(char *first, ...) 8113e3f4d6SMark Murray { 8213e3f4d6SMark Murray va_list ap; 8313e3f4d6SMark Murray char **ret = malloc (initial * sizeof(char *)); 8413e3f4d6SMark Murray 8513e3f4d6SMark Murray if (ret == NULL) 8613e3f4d6SMark Murray return ret; 8713e3f4d6SMark Murray 8813e3f4d6SMark Murray ret[0] = first; 8913e3f4d6SMark Murray va_start(ap, first); 9013e3f4d6SMark Murray ret = sub (ret, 1, initial, &ap); 9113e3f4d6SMark Murray va_end(ap); 9213e3f4d6SMark Murray return ret; 9313e3f4d6SMark Murray } 94