summaryrefslogtreecommitdiff
path: root/usr/src/lib/libcmdutils/common/custr.c
blob: 1ec72de9ddfa5bad16260b9753b550f3c85fb4d4 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * String utility functions with dynamic memory management.
 */

/*
 * Copyright 2014, Joyent, Inc.
 */

#include <stdlib.h>
#include <err.h>
#include <string.h>

#include "libcmdutils.h"

struct custr {
	size_t cus_strlen;
	size_t cus_datalen;
	char *cus_data;
};

#define	STRING_CHUNK_SIZE	64

void
custr_reset(custr_t *cus)
{
	if (cus->cus_data == NULL)
		return;

	cus->cus_strlen = 0;
	cus->cus_data[0] = '\0';
}

size_t
custr_len(custr_t *cus)
{
	return (cus->cus_strlen);
}

const char *
custr_cstr(custr_t *cus)
{
	return (cus->cus_data);
}

int
custr_appendc(custr_t *cus, char newc)
{
	char news[2];

	news[0] = newc;
	news[1] = '\0';

	return (custr_append(cus, news));
}

int
custr_append(custr_t *cus, const char *news)
{
	size_t len = strlen(news);
	size_t chunksz = STRING_CHUNK_SIZE;

	while (chunksz < len) {
		chunksz *= 2;
	}

	if (len + cus->cus_strlen + 1 >= cus->cus_datalen) {
		char *new_data;
		size_t new_datalen = cus->cus_datalen + chunksz;

		/*
		 * Allocate replacement memory:
		 */
		if ((new_data = malloc(new_datalen)) == NULL) {
			return (-1);
		}

		/*
		 * Copy existing data into replacement memory and free
		 * the old memory.
		 */
		if (cus->cus_data != NULL) {
			(void) memcpy(new_data, cus->cus_data,
			    cus->cus_strlen + 1);
			free(cus->cus_data);
		}

		/*
		 * Swap in the replacement buffer:
		 */
		cus->cus_data = new_data;
		cus->cus_datalen = new_datalen;
	}
	/*
	 * Append new string to existing string:
	 */
	(void) memcpy(cus->cus_data + cus->cus_strlen, news, len + 1);
	cus->cus_strlen += len;

	return (0);
}

int
custr_alloc(custr_t **cus)
{
	custr_t *t;

	if ((t = calloc(1, sizeof (*t))) == NULL) {
		*cus = NULL;
		return (-1);
	}

	*cus = t;
	return (0);
}

void
custr_free(custr_t *cus)
{
	if (cus == NULL)
		return;

	free(cus->cus_data);
	free(cus);
}