File: DStrCat.c

package info (click to toggle)
ncftp 2%3A3.2.5-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 3,376 kB
  • ctags: 3,358
  • sloc: ansic: 39,640; sh: 3,560; makefile: 897; cpp: 612; perl: 101
file content (43 lines) | stat: -rw-r--r-- 911 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "syshdrs.h"
#ifdef PRAGMA_HDRSTOP
#	pragma hdrstop
#endif

const char *
DStrCat(DStr *const dst, const char *const src)
{
	size_t srcLen, allocSize, newLen, curLen;
	char *cp;

	if (IS_DSTR_CORRUPT(dst))
		return NULL;

	srcLen = strlen(src) + 1 /* copy NUL byte also */;
	curLen = dst->len;
	newLen = srcLen + curLen;
	if (newLen > 0x00FFFFFF)
		return NULL;
	if (dst->allocSize < newLen) {
		/* Need to resize buffer before copying. */
		allocSize = (newLen + 16) & 0xFFFFFFF0;
		if (dst->s == NULL) {
			cp = calloc(allocSize, (size_t) 1);
			if (cp == NULL)
				return NULL;
		} else {
			cp = realloc(dst->s, allocSize);
			if (cp == NULL)
				return NULL;
			memset(cp + curLen, 0, allocSize - curLen);
		}
		dst->s = cp;
		dst->allocSize = allocSize;
	} else {
		cp = dst->s;
	}

	memcpy(cp + curLen, src, --srcLen);
	dst->len = newLen - 1;
	cp[newLen - 1] = '\0';
	return (cp);
}	/* DStrCat */