40201c12ec300981d7e7dfeb70e41179964b7fb4
[mmh] / sbr / add.c
1
2 /*
3  * add.c -- If "s1" is NULL, this routine just creates a
4  *       -- copy of "s2" into newly malloc'ed memory.
5  *       --
6  *       -- If "s1" is not NULL, then copy the concatenation
7  *       -- of "s1" and "s2" (note the order) into newly
8  *       -- malloc'ed memory.  Then free "s1".
9  *
10  * $Id$
11  *
12  * This code is Copyright (c) 2002, by the authors of nmh.  See the
13  * COPYRIGHT file in the root directory of the nmh distribution for
14  * complete copyright information.
15  */
16
17 #include <h/mh.h>
18 #include <h/utils.h>
19
20 char *
21 add (char *s2, char *s1)
22 {
23     char *cp;
24     size_t len1 = 0, len2 = 0;
25
26     if (s1)
27         len1 = strlen (s1);
28     if (s2)
29         len2 = strlen (s2);
30
31
32     cp = mh_xmalloc (len1 + len2 + 1);
33
34     /* Copy s1 and free it */
35     if (s1) {
36         memcpy (cp, s1, len1);
37         free (s1);
38     }
39
40     /* Copy s2 */
41     if (s2)
42         memcpy (cp + len1, s2, len2);
43
44     /* Now NULL terminate the string */
45     cp[len1 + len2] = '\0';
46
47     return cp;
48 }