1 /* $NetBSD: concatenate.c,v 1.1.1.1 2009/06/23 10:08:59 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 41 /* concatenate - concatenate null-terminated list of strings */ 42 43 char *concatenate(const char *arg0,...) 44 { 45 char *result; 46 va_list ap; 47 ssize_t len; 48 char *arg; 49 50 /* 51 * Compute the length of the resulting string. 52 */ 53 va_start(ap, arg0); 54 len = strlen(arg0); 55 while ((arg = va_arg(ap, char *)) != 0) 56 len += strlen(arg); 57 va_end(ap); 58 59 /* 60 * Build the resulting string. Don't care about wasting a CPU cycle. 61 */ 62 result = mymalloc(len + 1); 63 va_start(ap, arg0); 64 strcpy(result, arg0); 65 while ((arg = va_arg(ap, char *)) != 0) 66 strcat(result, arg); 67 va_end(ap); 68 return (result); 69 } 70