]> diplodocus.org Git - nmh/blob - sbr/add.c
Fix stupid accidental dependence on a bash quirk in previous
[nmh] / 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
19 char *
20 add (char *s2, char *s1)
21 {
22 char *cp;
23 size_t len1 = 0, len2 = 0;
24
25 if (s1)
26 len1 = strlen (s1);
27 if (s2)
28 len2 = strlen (s2);
29
30
31 if (!(cp = malloc (len1 + len2 + 1)))
32 adios (NULL, "unable to allocate string storage");
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 }