/*1* Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>3435#include "roken.h"3637ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL38roken_concat (char *s, size_t len, ...)39{40int ret;41va_list args;4243va_start(args, len);44ret = roken_vconcat (s, len, args);45va_end(args);46return ret;47}4849ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL50roken_vconcat (char *s, size_t len, va_list args)51{52const char *a;5354while ((a = va_arg(args, const char*))) {55size_t n = strlen (a);5657if (n >= len)58return -1;59memcpy (s, a, n);60s += n;61len -= n;62}63*s = '\0';64return 0;65}6667ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL68roken_vmconcat (char **s, size_t max_len, va_list args)69{70const char *a;71char *p, *q;72size_t len = 0;73*s = NULL;74p = malloc(1);75if(p == NULL)76return 0;77len = 1;78while ((a = va_arg(args, const char*))) {79size_t n = strlen (a);8081if(max_len && len + n > max_len){82free(p);83return 0;84}85q = realloc(p, len + n);86if(q == NULL){87free(p);88return 0;89}90p = q;91memcpy (p + len - 1, a, n);92len += n;93}94p[len - 1] = '\0';95*s = p;96return len;97}9899ROKEN_LIB_FUNCTION size_t ROKEN_LIB_CALL100roken_mconcat (char **s, size_t max_len, ...)101{102size_t ret;103va_list args;104105va_start(args, max_len);106ret = roken_vmconcat (s, max_len, args);107va_end(args);108return ret;109}110111112