source: trunk/third/nmh/sbr/add.c @ 12455

Revision 12455, 852 bytes checked in by danw, 26 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r12454, which included commits to RCS files with non-trunk default branches.
Line 
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: add.c,v 1.1.1.1 1999-02-07 18:14:07 danw Exp $
11 */
12
13#include <h/mh.h>
14
15char *
16add (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}
Note: See TracBrowser for help on using the repository browser.