1 /* $NetBSD: concatenate.c,v 1.1.1.2 2014/07/06 19:27:57 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* concatenate 3 6 /* SUMMARY 7 /* concatenate strings 8 /* SYNOPSIS 9 /* #include <stringops.h> 10 /* 11 /* char *concatenate(str, ...) 12 /* const char *str; 13 /* DESCRIPTION 14 /* The \fBconcatenate\fR routine concatenates a null-terminated 15 /* list of pointers to null-terminated character strings. 16 /* The result is dynamically allocated and should be passed to myfree() 17 /* when no longer needed. 18 /* LICENSE 19 /* .ad 20 /* .fi 21 /* The Secure Mailer license must be distributed with this software. 22 /* AUTHOR(S) 23 /* Wietse Venema 24 /* IBM T.J. Watson Research 25 /* P.O. Box 704 26 /* Yorktown Heights, NY 10598, USA 27 /*--*/ 28 29 /* System library. */ 30 31 #include <sys_defs.h> 32 #include <stdlib.h> /* 44BSD stdarg.h uses abort() */ 33 #include <stdarg.h> 34 #include <string.h> 35 36 /* Utility library. */ 37 38 #include "mymalloc.h" 39 #include "stringops.h" 40 #include "compat_va_copy.h" 41 42 /* concatenate - concatenate null-terminated list of strings */ 43 44 char *concatenate(const char *arg0,...) 45 { 46 char *result; 47 va_list ap; 48 va_list ap2; 49 ssize_t len; 50 char *arg; 51 52 /* 53 * Initialize argument lists. 54 */ 55 va_start(ap, arg0); 56 VA_COPY(ap2, ap); 57 58 /* 59 * Compute the length of the resulting string. 60 */ 61 len = strlen(arg0); 62 while ((arg = va_arg(ap, char *)) != 0) 63 len += strlen(arg); 64 va_end(ap); 65 66 /* 67 * Build the resulting string. Don't care about wasting a CPU cycle. 68 */ 69 result = mymalloc(len + 1); 70 strcpy(result, arg0); 71 while ((arg = va_arg(ap2, char *)) != 0) 72 strcat(result, arg); 73 va_end(ap2); 74 return (result); 75 } 76