diff options
author | behdad <behdad> | 2004-05-03 05:17:48 +0000 |
---|---|---|
committer | behdad <behdad> | 2004-05-03 05:17:48 +0000 |
commit | 577ed4095383ef5284225d45709e6b5f0598a064 (patch) | |
tree | 6c7d0ce55124a688b4d7050e684d9d7a1e3aa71d /strconcat.c | |
download | c2man-master.tar.gz |
Diffstat (limited to 'strconcat.c')
-rw-r--r-- | strconcat.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/strconcat.c b/strconcat.c new file mode 100644 index 0000000..c9caff6 --- /dev/null +++ b/strconcat.c @@ -0,0 +1,74 @@ +/* $Id: strconcat.c,v 1.1 2004-05-03 05:17:48 behdad Exp $ + * concatenate a list of strings, storing them in a malloc'ed region + */ +#include "c2man.h" +#include "strconcat.h" + +#ifdef I_STDARG +#include <stdarg.h> +#endif +#ifdef I_VARARGS +#include <varargs.h> +#endif + +extern void outmem(); + +#ifdef I_STDARG +char *strconcat(const char *first, ...) +#else +char *strconcat(va_alist) + va_dcl +#endif +{ + size_t totallen; + va_list argp; + char *s, *retstring; +#ifndef I_STDARG + char *first; +#endif + /* add up the total length */ +#ifdef I_STDARG + va_start(argp,first); +#else + va_start(argp); + first = va_arg(argp, char *); +#endif +#ifdef DEBUG + fprintf(stderr,"strconcat: \"%s\"",first); +#endif + totallen = strlen(first); + while ((s = va_arg(argp,char *)) != NULL) + { + totallen += strlen(s); +#ifdef DEBUG + fprintf(stderr,",\"%s\"",s); +#endif + } +#ifdef DEBUG + fprintf(stderr,"\nstrlen = %ld\n",(long)totallen); +#endif + va_end(argp); + + /* malloc the memory */ + if ((retstring = malloc(totallen + 1)) == 0) + outmem(); + +#ifdef I_STDARG + va_start(argp,first); +#else + va_start(argp); + first = va_arg(argp, char *); +#endif + /* copy the stuff in */ + strcpy(retstring,first); + + while ((s = va_arg(argp,char *)) != NULL) + strcat(retstring,s); + + va_end(argp); + +#ifdef DEBUG + fprintf(stderr,"strconcat returns \"%s\"\n",retstring); +#endif + return retstring; +} |