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