5c519e9f4896f324fcd6df117e338c12b1f6db28
[mmh] / sbr / concat.c
1 /*
2 ** concat.c -- concatenate a variable number (minimum of 1)
3 **             of strings in managed memory
4 **
5 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
6 ** COPYRIGHT file in the root directory of the nmh distribution for
7 ** complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12
13
14 static char *
15 copy(char *from, char *to)
16 {
17         while ((*to = *from)) {
18                 to++;
19                 from++;
20         }
21         return (to);
22 }
23
24
25 char *
26 concat(char *s1, ...)
27 {
28         char *cp, *dp, *sp;
29         size_t len;
30         va_list list;
31
32         len = strlen(s1) + 1;
33         va_start(list, s1);
34         while ((cp = va_arg(list, char *)))
35                 len += strlen(cp);
36         va_end(list);
37
38         dp = sp = mh_xmalloc(len);
39
40         sp = copy(s1, sp);
41
42         va_start(list, s1);
43         while ((cp = va_arg(list, char *)))
44                 sp = copy(cp, sp);
45         va_end(list);
46
47         return dp;
48 }