Initial revision
[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
13 #include <h/mh.h>
14
15 char *
16 add (char *s2, char *s1)
17 {
18     char *cp;
19     size_t len1 = 0, len2 = 0;
20
21     if (s1)
22         len1 = strlen (s1);
23     if (s2)
24         len2 = strlen (s2);
25
26
27     if (!(cp = malloc (len1 + len2 + 1)))
28         adios (NULL, "unable to allocate string storage");
29
30     /* Copy s1 and free it */
31     if (s1) {
32         memcpy (cp, s1, len1);
33         free (s1);
34     }
35
36     /* Copy s2 */
37     if (s2)
38         memcpy (cp + len1, s2, len2);
39
40     /* Now NULL terminate the string */
41     cp[len1 + len2] = '\0';
42
43     return cp;
44 }