doc-xml/smbdotconf: fix server [min|max] protocol documentation
[Samba/gebeck_regimport.git] / lib / smbconf / smbconf_util.c
blob86a95988f1069e776126af70ad5f1a610f14ea6f
1 /*
2 * Unix SMB/CIFS implementation.
3 * libsmbconf - Samba configuration library, utility functions
4 * Copyright (C) Michael Adam 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "smbconf_private.h"
24 static int smbconf_destroy_ctx(struct smbconf_ctx *ctx)
26 return ctx->ops->shutdown(ctx);
29 /**
30 * Initialize the configuration.
32 * This should be the first function in a sequence of calls to smbconf
33 * functions:
35 * Upon success, this creates and returns the conf context
36 * that should be passed around in subsequent calls to the other
37 * smbconf functions.
39 * After the work with the configuration is completed, smbconf_shutdown()
40 * should be called.
42 sbcErr smbconf_init_internal(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
43 const char *path, struct smbconf_ops *ops)
45 sbcErr err = SBC_ERR_OK;
46 struct smbconf_ctx *ctx;
48 if (conf_ctx == NULL) {
49 return SBC_ERR_INVALID_PARAM;
52 ctx = talloc_zero(mem_ctx, struct smbconf_ctx);
53 if (ctx == NULL) {
54 return SBC_ERR_NOMEM;
57 ctx->ops = ops;
59 err = ctx->ops->init(ctx, path);
60 if (!SBC_ERROR_IS_OK(err)) {
61 goto fail;
64 talloc_set_destructor(ctx, smbconf_destroy_ctx);
66 *conf_ctx = ctx;
67 return err;
69 fail:
70 talloc_free(ctx);
71 return err;
75 /**
76 * add a string to a talloced array of strings.
78 sbcErr smbconf_add_string_to_array(TALLOC_CTX *mem_ctx,
79 char ***array,
80 uint32_t count,
81 const char *string)
83 char **new_array = NULL;
85 if (array == NULL) {
86 return SBC_ERR_INVALID_PARAM;
89 new_array = talloc_realloc(mem_ctx, *array, char *, count + 1);
90 if (new_array == NULL) {
91 return SBC_ERR_NOMEM;
94 if (string == NULL) {
95 new_array[count] = NULL;
96 } else {
97 new_array[count] = talloc_strdup(new_array, string);
98 if (new_array[count] == NULL) {
99 talloc_free(new_array);
100 return SBC_ERR_NOMEM;
104 *array = new_array;
106 return SBC_ERR_OK;
109 bool smbconf_find_in_array(const char *string, char **list,
110 uint32_t num_entries, uint32_t *entry)
112 uint32_t i;
114 if (list == NULL) {
115 return false;
118 for (i = 0; i < num_entries; i++) {
119 if (((string == NULL) && (list[i] == NULL)) ||
120 strequal(string, list[i]))
122 if (entry != NULL) {
123 *entry = i;
125 return true;
129 return false;
132 bool smbconf_reverse_find_in_array(const char *string, char **list,
133 uint32_t num_entries, uint32_t *entry)
135 int32_t i;
137 if ((string == NULL) || (list == NULL) || (num_entries == 0)) {
138 return false;
141 for (i = num_entries - 1; i >= 0; i--) {
142 if (strequal(string, list[i])) {
143 if (entry != NULL) {
144 *entry = i;
146 return true;
150 return false;