summaryrefslogtreecommitdiffstats
path: root/lib/util/util_strlist_v3.c
blob: c248575c5255cceec8b38977b1c21c64fa3b16b7 (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
/*
   Unix SMB/CIFS implementation.

   Copyright (C) Andrew Tridgell 2005
   Copyright (C) Jelmer Vernooij 2005

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "system/locale.h"
#include "lib/util/tsort.h"

#undef strcasecmp

/**
 * @file
 * @brief String list manipulation v3
 */

/**
 * Needed for making an "unconst" list "const"
 */
_PUBLIC_ const char **const_str_list(char **list)
{
	return discard_const_p(const char *, list);
}

/**
 * str_list_make, v3 version. The v4 version does not
 * look at quoted strings with embedded blanks, so
 * do NOT merge this function please!
 */
#define S_LIST_ABS 16 /* List Allocation Block Size */

char **str_list_make_v3(TALLOC_CTX *mem_ctx, const char *string,
	const char *sep)
{
	char **list;
	const char *str;
	char *s, *tok;
	int num, lsize;

	if (!string || !*string)
		return NULL;

	list = talloc_array(mem_ctx, char *, S_LIST_ABS+1);
	if (list == NULL) {
		return NULL;
	}
	lsize = S_LIST_ABS;

	s = talloc_strdup(list, string);
	if (s == NULL) {
		DEBUG(0,("str_list_make: Unable to allocate memory"));
		TALLOC_FREE(list);
		return NULL;
	}

	/*
	 * DON'T REPLACE THIS BY "LIST_SEP". The common version of
	 * LIST_SEP does not contain the ;, which used to be accepted
	 * by Samba 4.0 before param merges. It would be the far
	 * better solution to split the _v3 version again to source3/
	 * where it belongs, see the _v3 in its name.
	 *
	 * Unfortunately it is referenced in /lib/param/loadparm.c,
	 * which depends on the version that the AD-DC mandates,
	 * namely without the ; as part of the list separator. I am
	 * missing the waf fu to properly work around the wrong
	 * include paths here for this defect.
	 */
	if (sep == NULL) {
		sep = " \t,;\n\r";
	}

	num = 0;
	str = s;

	while (next_token_talloc(list, &str, &tok, sep)) {

		if (num == lsize) {
			char **tmp;

			lsize += S_LIST_ABS;

			tmp = talloc_realloc(mem_ctx, list, char *,
						   lsize + 1);
			if (tmp == NULL) {
				DEBUG(0,("str_list_make: "
					"Unable to allocate memory"));
				TALLOC_FREE(list);
				return NULL;
			}

			list = tmp;

			memset (&list[num], 0,
				((sizeof(char*)) * (S_LIST_ABS +1)));
		}

		list[num] = tok;
		num += 1;
	}

	list[num] = NULL;

	TALLOC_FREE(s);
	return list;
}

const char **str_list_make_v3_const(TALLOC_CTX *mem_ctx,
				    const char *string,
				    const char *sep)
{
	return const_str_list(str_list_make_v3(mem_ctx, string, sep));
}