]> diplodocus.org Git - nmh/blob - sbr/concat.c
new.c: Order two return statements to match comment.
[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 <h/utils.h>
11
12
13 /* concat returns a non-NULL malloc'd pointer to the catenation of the
14 * argument strings less their NUL terminators other than the last. The
15 * arguments are terminated by a NULL.
16 *
17 * Example: concat("abc", "def", "", "g", NULL) returns "abcdefg". */
18 char *
19 concat (const char *s1, ...)
20 {
21 char *cp, *dp, *sp;
22 size_t len;
23 va_list list;
24
25 len = strlen (s1) + 1;
26 va_start(list, s1);
27 while ((cp = va_arg(list, char *)))
28 len += strlen (cp);
29 va_end(list);
30
31 dp = sp = mh_xmalloc(len);
32
33 sp = stpcpy(sp, s1);
34
35 va_start(list, s1);
36 while ((cp = va_arg (list, char *)))
37 sp = stpcpy(sp, cp);
38 va_end(list);
39
40 return dp;
41 }