/************************************************************************ * * * Copyright Digital Equipment Corporation 1998. All rights reserved. * * * * Restricted Rights: Use, duplication, or disclosure by the U.S. * * Government is subject to restrictions as set forth in subparagraph * * (c) (1) (ii) of DFARS 252.227-7013, or in FAR 52.227-19, or in FAR * * 52.227-14 Alt. III, as applicable. * * * * This software is proprietary to and embodies the confidential * * technology of Digital Equipment Corporation. Possession, use, or * * copying of this software and media is authorized only pursuant to a * * valid written license from Digital or an authorized sublicensor. * * * ************************************************************************/ /* chap_3_stdarg.c */ /* This routine accepts a variable number of string arguments, */ /* preceded by a count of the number of such strings. It */ /* allocates enough space in which to concatenate all of the */ /* strings, concatenates them together, and returns the address */ /* of the new string. It returns NULL if there are no string */ /* arguments, or if they are all null strings. */ #include /* Include appropriate header files. */ #include #include #include /* For the "example" call in main */ /* NSTRINGS is the maximum number of string arguments accepted */ /* (arbitrary). */ #define NSTRINGS 10 char *concatenate(int n,...) { va_list ap; /* Declare the argument pointer. */ char *list[NSTRINGS], *string; int index = 0, size = 0; /* Check that the number of arguments is within range. */ if (n <= 0) return NULL; if (n > NSTRINGS) n = NSTRINGS; va_start(ap, n); /* Initialize the argument pointer. */ do { /* Extract the next argument and save it. */ list[index] = va_arg(ap, char *); size += strlen(list[index]); } while (++index < n); va_end(ap); /* Terminate use of ap. */ if (size == 0) return NULL; string = malloc(size + 1); string[0] = '\0'; /* Append each argument to the end of the growing result */ /* string. */ for (index = 0; index < n; ++index) strcat(string, list[index]); return string; } /* An example of calling this routine is */ main() { char *ret_string ; ret_string = concatenate(7, "This ", "message ", "is ", "built with ", "a", " variable arg", " list.") ; puts(ret_string) ; }