]> diplodocus.org Git - nmh/blob - sbr/concat.c
sendsbr.c: Move interface to own file.
[nmh] / sbr / concat.c
1 /* concat.c -- concatenate a variable number (minimum of 1)
2 * of strings in managed memory
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include "h/mh.h"
10 #include "concat.h"
11 #include "h/utils.h"
12
13
14 /* concat returns a non-NULL malloc'd pointer to the catenation of the
15 * argument strings less their NUL terminators other than the last. The
16 * arguments are terminated by a NULL.
17 *
18 * Example: concat("abc", "def", "", "g", NULL) returns "abcdefg". */
19 char *
20 concat (const char *s1, ...)
21 {
22 char *cp, *dp, *sp;
23 size_t len;
24 va_list list;
25
26 len = strlen (s1) + 1;
27 va_start(list, s1);
28 while ((cp = va_arg(list, char *)))
29 len += strlen (cp);
30 va_end(list);
31
32 dp = sp = mh_xmalloc(len);
33
34 sp = stpcpy(sp, s1);
35
36 va_start(list, s1);
37 while ((cp = va_arg (list, char *)))
38 sp = stpcpy(sp, cp);
39 va_end(list);
40
41 return dp;
42 }