2 * Unix SMB/CIFS implementation.
3 * Copyright (C) Jelmer Vernooij 2005
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "lib/util/dlinklist.h"
21 #include "param/param.h"
22 #include "system/filesys.h"
24 struct param_section *param_get_section(struct param_context *ctx, const char *name)
26 struct param_section *sect;
31 for (sect = ctx->sections; sect; sect = sect->next) {
32 if (!strcasecmp_m(sect->name, name))
39 struct param_opt *param_section_get(struct param_section *section,
44 for (p = section->parameters; p; p = p->next) {
45 if (strcasecmp_m(p->key, name) == 0)
52 struct param_opt *param_get (struct param_context *ctx, const char *section_name, const char *name)
54 struct param_section *section = param_get_section(ctx, section_name);
58 return param_section_get(section, name);
61 /* Look up parameter. If it is not found, add it */
62 static struct param_opt *param_get_add(struct param_context *ctx, const char *section_name, const char *name)
64 struct param_section *section;
67 SMB_ASSERT(section_name != NULL);
68 SMB_ASSERT(name != NULL);
70 section = param_get_section(ctx, section_name);
72 if (section == NULL) {
73 section = talloc_zero(ctx, struct param_section);
77 section->name = talloc_strdup(section, section_name);
78 DLIST_ADD_END(ctx->sections, section, struct param_section *);
81 p = param_section_get(section, name);
83 p = talloc_zero(section, struct param_opt);
87 p->key = talloc_strdup(p, name);
88 DLIST_ADD_END(section->parameters, p, struct param_opt *);
94 const char *param_get_string(struct param_context *ctx, const char *section, const char *param)
96 struct param_opt *p = param_get(ctx, section, param);
104 int param_set_string(struct param_context *ctx, const char *section, const char *param, const char *value)
106 struct param_opt *p = param_get_add(ctx, section, param);
111 p->value = talloc_strdup(p, value);
116 const char **param_get_string_list(struct param_context *ctx, const char *section, const char *param,
117 const char *separator)
119 struct param_opt *p = param_get(ctx, section, param);
124 if (separator == NULL)
125 separator = LIST_SEP;
127 return str_list_make(ctx, p->value, separator);
130 int param_set_string_list(struct param_context *ctx, const char *section, const char *param, const char **list)
132 struct param_opt *p = param_get_add(ctx, section, param);
134 p->value = str_list_join(p, list, ' ');
139 int param_get_int(struct param_context *ctx, const char *section, const char *param, int default_v)
141 const char *value = param_get_string(ctx, section, param);
144 return strtol(value, NULL, 0);
149 void param_set_int(struct param_context *ctx, const char *section, const char *param, int value)
151 struct param_opt *p = param_get_add(ctx, section, param);
156 p->value = talloc_asprintf(p, "%d", value);
159 unsigned long param_get_ulong(struct param_context *ctx, const char *section, const char *param, unsigned long default_v)
161 const char *value = param_get_string(ctx, section, param);
164 return strtoul(value, NULL, 0);
169 void param_set_ulong(struct param_context *ctx, const char *section, const char *name, unsigned long value)
171 struct param_opt *p = param_get_add(ctx, section, name);
176 p->value = talloc_asprintf(p, "%lu", value);
179 static bool param_sfunc (const char *name, void *_ctx)
181 struct param_context *ctx = (struct param_context *)_ctx;
182 struct param_section *section = param_get_section(ctx, name);
184 if (section == NULL) {
185 section = talloc_zero(ctx, struct param_section);
189 section->name = talloc_strdup(section, name);
191 DLIST_ADD_END(ctx->sections, section, struct param_section *);
194 /* Make sure this section is on top of the list for param_pfunc */
195 DLIST_PROMOTE(ctx->sections, section);
200 static bool param_pfunc (const char *name, const char *value, void *_ctx)
202 struct param_context *ctx = (struct param_context *)_ctx;
203 struct param_opt *p = param_section_get(ctx->sections, name);
206 p = talloc_zero(ctx->sections, struct param_opt);
210 p->key = talloc_strdup(p, name);
211 p->value = talloc_strdup(p, value);
212 DLIST_ADD(ctx->sections->parameters, p);
213 } else { /* Replace current value */
214 talloc_free(p->value);
215 p->value = talloc_strdup(p, value);
221 struct param_context *param_init(TALLOC_CTX *mem_ctx)
223 return talloc_zero(mem_ctx, struct param_context);
227 int param_read(struct param_context *ctx, const char *fn)
229 ctx->sections = talloc_zero(ctx, struct param_section);
230 if (ctx->sections == NULL)
233 ctx->sections->name = talloc_strdup(ctx->sections, "global");
234 if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
241 int param_use(struct loadparm_context *lp_ctx, struct param_context *ctx)
243 struct param_section *section;
245 for (section = ctx->sections; section; section = section->next) {
246 struct param_opt *param;
247 bool isglobal = strcmp(section->name, "global") == 0;
248 for (param = section->parameters; param; param = param->next) {
250 lp_do_global_parameter(lp_ctx, param->key,
253 struct loadparm_service *service =
254 lp_service(lp_ctx, section->name);
256 service = lp_add_service(lp_ctx, &sDefault, section->name);
257 lp_do_service_parameter(lp_ctx,
267 int param_write(struct param_context *ctx, const char *fn)
270 struct param_section *section;
272 if (fn == NULL || ctx == NULL)
275 file = open(fn, O_WRONLY|O_CREAT, 0755);
280 for (section = ctx->sections; section; section = section->next) {
281 struct param_opt *param;
283 fdprintf(file, "[%s]\n", section->name);
284 for (param = section->parameters; param; param = param->next) {
285 fdprintf(file, "\t%s = %s\n", param->key, param->value);
287 fdprintf(file, "\n");