home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / c2man-2.0pl33.lha / c2man-2.0 / strconcat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-14  |  1.4 KB  |  75 lines

  1. /* $Id: strconcat.c,v 2.0.1.1 1993/05/17 02:12:09 greyham Exp $
  2.  * concatenate a list of strings, storing them in a malloc'ed region
  3.  */
  4. #include "c2man.h"
  5. #include "strconcat.h"
  6.  
  7. #ifdef I_STDARG
  8. #include <stdarg.h>
  9. #endif
  10. #ifdef I_VARARGS
  11. #include <varargs.h>
  12. #endif
  13.  
  14. extern void outmem();
  15.  
  16. #ifdef I_STDARG
  17. char *strconcat(const char *first, ...)
  18. #else
  19. char *strconcat(va_alist)
  20.     va_dcl
  21. #endif
  22. {
  23.     size_t totallen;
  24.     va_list argp;
  25.     char *s, *retstring;
  26. #ifndef I_STDARG
  27.     char *first;
  28. #endif    
  29.     /* add up the total length */
  30. #ifdef I_STDARG
  31.     va_start(argp,first);
  32. #else
  33.     va_start(argp);
  34.     first = va_arg(argp, char *);
  35. #endif
  36. #ifdef DEBUG
  37.     fprintf(stderr,"strconcat: \"%s\"",first);
  38. #endif
  39.     totallen = strlen(first);
  40.     while ((s = va_arg(argp,char *)) != NULL)
  41.     {
  42.     totallen += strlen(s);
  43. #ifdef DEBUG
  44.     fprintf(stderr,",\"%s\"",s);
  45. #endif
  46.     }
  47. #ifdef DEBUG
  48.     fprintf(stderr,"\nstrlen = %ld\n",(long)totallen);
  49. #endif
  50.     va_end(argp);
  51.     
  52.     /* malloc the memory */
  53.     if ((retstring = malloc(totallen + 1)) == 0)
  54.     outmem();
  55.     
  56. #ifdef I_STDARG
  57.     va_start(argp,first);
  58. #else
  59.     va_start(argp);
  60.     first = va_arg(argp, char *);
  61. #endif
  62.     /* copy the stuff in */
  63.     strcpy(retstring,first);
  64.  
  65.     while ((s = va_arg(argp,char *)) != NULL)
  66.     strcat(retstring,s);
  67.  
  68.     va_end(argp);
  69.  
  70. #ifdef DEBUG
  71.     fprintf(stderr,"strconcat returns \"%s\"\n",retstring);
  72. #endif
  73.     return retstring;
  74. }
  75.