1221807Sstas/*
2221807Sstas * Copyright (c) 1998, 1999 Kungliga Tekniska H��gskolan
3221807Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4221807Sstas * All rights reserved.
5221807Sstas *
6221807Sstas * Redistribution and use in source and binary forms, with or without
7221807Sstas * modification, are permitted provided that the following conditions
8221807Sstas * are met:
9250870Strociny *
10221807Sstas * 1. Redistributions of source code must retain the above copyright
11221807Sstas *    notice, this list of conditions and the following disclaimer.
12221807Sstas *
13221807Sstas * 2. Redistributions in binary form must reproduce the above copyright
14221807Sstas *    notice, this list of conditions and the following disclaimer in the
15221807Sstas *    documentation and/or other materials provided with the distribution.
16221931Sstas *
17221931Sstas * 3. Neither the name of the Institute nor the names of its contributors
18221931Sstas *    may be used to endorse or promote products derived from this software
19223953Spluknet *    without specific prior written permission.
20221807Sstas *
21221807Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22223953Spluknet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23250870Strociny * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24250870Strociny * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25221807Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26223953Spluknet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27223953Spluknet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28221824Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29221824Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30221824Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31221824Sstas * SUCH DAMAGE.
32221824Sstas */
33221807Sstas
34221807Sstas#include <config.h>
35221824Sstas
36221807Sstas#include <stdarg.h>
37221807Sstas#include <stdlib.h>
38221807Sstas#include <string.h>
39221807Sstas#include <errno.h>
40221807Sstas#include "roken.h"
41221807Sstas
42221807Sstasenum { initial = 10, increment = 5 };
43221807Sstas
44221807Sstasstatic char **
45221807Sstassub (char **argv, int i, int argc, va_list *ap)
46221807Sstas{
47221807Sstas    do {
48221807Sstas	if(i == argc) {
49	    /* realloc argv */
50	    char **tmp = realloc(argv, (argc + increment) * sizeof(*argv));
51	    if(tmp == NULL) {
52		free(argv);
53		errno = ENOMEM;
54		return NULL;
55	    }
56	    argv  = tmp;
57	    argc += increment;
58	}
59	argv[i++] = va_arg(*ap, char*);
60    } while(argv[i - 1] != NULL);
61    return argv;
62}
63
64/*
65 * return a malloced vector of pointers to the strings in `ap'
66 * terminated by NULL.
67 */
68
69ROKEN_LIB_FUNCTION char ** ROKEN_LIB_CALL
70vstrcollect(va_list *ap)
71{
72    return sub (NULL, 0, 0, ap);
73}
74
75/*
76 *
77 */
78
79ROKEN_LIB_FUNCTION char ** ROKEN_LIB_CALL
80strcollect(char *first, ...)
81{
82    va_list ap;
83    char **ret = malloc (initial * sizeof(char *));
84
85    if (ret == NULL)
86	return ret;
87
88    ret[0] = first;
89    va_start(ap, first);
90    ret = sub (ret, 1, initial, &ap);
91    va_end(ap);
92    return ret;
93}
94