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 "param/loadparm.h"
23
#include "system/filesys.h"
25
struct param_section *param_get_section(struct param_context *ctx, const char *name)
27
struct param_section *sect;
32
for (sect = ctx->sections; sect; sect = sect->next) {
33
if (!strcasecmp_m(sect->name, name))
40
struct param_opt *param_section_get(struct param_section *section,
45
for (p = section->parameters; p; p = p->next) {
46
if (strcasecmp_m(p->key, name) == 0)
53
struct param_opt *param_get (struct param_context *ctx, const char *name, const char *section_name)
55
struct param_section *section = param_get_section(ctx, section_name);
59
return param_section_get(section, name);
62
struct param_section *param_add_section(struct param_context *ctx, const char *section_name)
64
struct param_section *section;
65
section = talloc_zero(ctx, struct param_section);
69
section->name = talloc_strdup(section, section_name);
70
DLIST_ADD_END(ctx->sections, section, struct param_section *);
74
/* Look up parameter. If it is not found, add it */
75
struct param_opt *param_get_add(struct param_context *ctx, const char *name, const char *section_name)
77
struct param_section *section;
80
SMB_ASSERT(section_name != NULL);
81
SMB_ASSERT(name != NULL);
83
section = param_get_section(ctx, section_name);
85
if (section == NULL) {
86
section = param_add_section(ctx, section_name);
89
p = param_section_get(section, name);
91
p = talloc_zero(section, struct param_opt);
95
p->key = talloc_strdup(p, name);
96
DLIST_ADD_END(section->parameters, p, struct param_opt *);
102
const char *param_get_string(struct param_context *ctx, const char *param, const char *section)
104
struct param_opt *p = param_get(ctx, param, section);
112
int param_set_string(struct param_context *ctx, const char *param, const char *value, const char *section)
114
struct param_opt *p = param_get_add(ctx, param, section);
119
p->value = talloc_strdup(p, value);
124
const char **param_get_string_list(struct param_context *ctx, const char *param, const char *separator, const char *section)
126
struct param_opt *p = param_get(ctx, param, section);
131
return (const char **)str_list_make(ctx, p->value, separator);
134
int param_set_string_list(struct param_context *ctx, const char *param, const char **list, const char *section)
136
struct param_opt *p = param_get_add(ctx, param, section);
138
p->value = str_list_join(p, list, ' ');
143
int param_get_int(struct param_context *ctx, const char *param, int default_v, const char *section)
145
const char *value = param_get_string(ctx, param, section);
148
return strtol(value, NULL, 0);
153
void param_set_int(struct param_context *ctx, const char *param, int value, const char *section)
155
struct param_opt *p = param_get_add(ctx, section, param);
160
p->value = talloc_asprintf(p, "%d", value);
163
unsigned long param_get_ulong(struct param_context *ctx, const char *param, unsigned long default_v, const char *section)
165
const char *value = param_get_string(ctx, param, section);
168
return strtoul(value, NULL, 0);
173
void param_set_ulong(struct param_context *ctx, const char *name, unsigned long value, const char *section)
175
struct param_opt *p = param_get_add(ctx, name, section);
180
p->value = talloc_asprintf(p, "%lu", value);
183
static bool param_sfunc (const char *name, void *_ctx)
185
struct param_context *ctx = (struct param_context *)_ctx;
186
struct param_section *section = param_get_section(ctx, name);
188
if (section == NULL) {
189
section = talloc_zero(ctx, struct param_section);
193
section->name = talloc_strdup(section, name);
195
DLIST_ADD_END(ctx->sections, section, struct param_section *);
198
/* Make sure this section is on top of the list for param_pfunc */
199
DLIST_PROMOTE(ctx->sections, section);
204
static bool param_pfunc (const char *name, const char *value, void *_ctx)
206
struct param_context *ctx = (struct param_context *)_ctx;
207
struct param_opt *p = param_section_get(ctx->sections, name);
210
p = talloc_zero(ctx->sections, struct param_opt);
214
p->key = talloc_strdup(p, name);
215
p->value = talloc_strdup(p, value);
216
DLIST_ADD(ctx->sections->parameters, p);
217
} else { /* Replace current value */
218
talloc_free(p->value);
219
p->value = talloc_strdup(p, value);
225
struct param_context *param_init(TALLOC_CTX *mem_ctx)
227
return talloc_zero(mem_ctx, struct param_context);
231
int param_read(struct param_context *ctx, const char *fn)
233
ctx->sections = talloc_zero(ctx, struct param_section);
234
if (ctx->sections == NULL)
237
ctx->sections->name = talloc_strdup(ctx->sections, "global");
238
if (!pm_process( fn, param_sfunc, param_pfunc, ctx)) {
245
int param_use(struct loadparm_context *lp_ctx, struct param_context *ctx)
247
struct param_section *section;
249
for (section = ctx->sections; section; section = section->next) {
250
struct param_opt *param;
251
bool isglobal = strcmp(section->name, "global") == 0;
252
for (param = section->parameters; param; param = param->next) {
254
lp_do_global_parameter(lp_ctx, param->key,
257
struct loadparm_service *service =
258
lp_service(lp_ctx, section->name);
260
service = lp_add_service(lp_ctx, lp_default_service(lp_ctx), section->name);
261
lp_do_service_parameter(lp_ctx, service, param->key, param->value);
268
int param_write(struct param_context *ctx, const char *fn)
271
struct param_section *section;
273
if (fn == NULL || ctx == NULL)
276
file = open(fn, O_WRONLY|O_CREAT, 0755);
281
for (section = ctx->sections; section; section = section->next) {
282
struct param_opt *param;
284
fdprintf(file, "[%s]\n", section->name);
285
for (param = section->parameters; param; param = param->next) {
286
fdprintf(file, "\t%s = %s\n", param->key, param->value);
288
fdprintf(file, "\n");