File: DStrCpyList.c

package info (click to toggle)
ncftp 2%3A3.2.4-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,316 kB
  • ctags: 3,323
  • sloc: ansic: 39,621; sh: 3,174; makefile: 883; cpp: 612; perl: 101
file content (83 lines) | stat: -rw-r--r-- 1,549 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "syshdrs.h"
#ifdef PRAGMA_HDRSTOP
#	pragma hdrstop
#endif

/*VARARGS*/
const char *
DStrCpyList(DStr *const dst, ...)
{
	size_t newLen, allocSize, srcLen;
	char *dcp, *cp, *osrc;
	const char *src;
	char *recursive;
	va_list ap;

	if (IS_DSTR_CORRUPT(dst))
		return NULL;

	osrc = dst->s;
	recursive = NULL;
	newLen = 0;
	va_start(ap, dst);
	src = va_arg(ap, char *);
	while (src != NULL) {
		if (src == osrc) {
			if (recursive == NULL)
				recursive = strdup(src);
		}
		newLen += strlen(src);
		src = va_arg(ap, char *);
	}
	va_end(ap);

	newLen++; 		/* copy NUL byte also */;
	if (newLen > 0x00FFFFFF) {
		if (recursive != NULL)
			free(recursive);
		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) {
				if (recursive != NULL)
					free(recursive);
				return NULL;
			}
		} else {
			cp = realloc(dst->s, allocSize);
			if (cp == NULL) {
				if (recursive != NULL)
					free(recursive);
				return NULL;
			}
			memset(cp, 0, allocSize);
		}
		dst->s = cp;
		dst->allocSize = allocSize;
	} else {
		cp = dst->s;
	}

	dcp = cp;
	va_start(ap, dst);
	src = va_arg(ap, char *);
	while (src != NULL) {
		if (src == osrc)
			src = recursive;
		srcLen = strlen(src);
		memcpy(dcp, src, srcLen);
		dcp += srcLen;
		src = va_arg(ap, char *);
	}
	va_end(ap);
	*dcp = '\0';

	dst->len = newLen - 1;
	if (recursive != NULL)
		free(recursive);
	return (cp);
}	/* DStrCpyList */