/src/samba/lib/smbconf/smbconf_util.c
Line | Count | Source |
1 | | /* |
2 | | * Unix SMB/CIFS implementation. |
3 | | * libsmbconf - Samba configuration library, utility functions |
4 | | * Copyright (C) Michael Adam 2008 |
5 | | * |
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. |
10 | | * |
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. |
15 | | * |
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/>. |
18 | | */ |
19 | | |
20 | | #include "includes.h" |
21 | | #include "smbconf_private.h" |
22 | | |
23 | | |
24 | | static int smbconf_destroy_ctx(struct smbconf_ctx *ctx) |
25 | 0 | { |
26 | 0 | return ctx->ops->shutdown(ctx); |
27 | 0 | } |
28 | | |
29 | | /** |
30 | | * Initialize the configuration. |
31 | | * |
32 | | * This should be the first function in a sequence of calls to smbconf |
33 | | * functions: |
34 | | * |
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. |
38 | | * |
39 | | * After the work with the configuration is completed, smbconf_shutdown() |
40 | | * should be called. |
41 | | */ |
42 | | sbcErr smbconf_init_internal(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx, |
43 | | const char *path, struct smbconf_ops *ops) |
44 | 0 | { |
45 | 0 | sbcErr err = SBC_ERR_OK; |
46 | 0 | struct smbconf_ctx *ctx; |
47 | |
|
48 | 0 | if (conf_ctx == NULL) { |
49 | 0 | return SBC_ERR_INVALID_PARAM; |
50 | 0 | } |
51 | | |
52 | 0 | ctx = talloc_zero(mem_ctx, struct smbconf_ctx); |
53 | 0 | if (ctx == NULL) { |
54 | 0 | return SBC_ERR_NOMEM; |
55 | 0 | } |
56 | | |
57 | 0 | ctx->ops = ops; |
58 | |
|
59 | 0 | err = ctx->ops->init(ctx, path); |
60 | 0 | if (!SBC_ERROR_IS_OK(err)) { |
61 | 0 | goto fail; |
62 | 0 | } |
63 | | |
64 | 0 | talloc_set_destructor(ctx, smbconf_destroy_ctx); |
65 | |
|
66 | 0 | *conf_ctx = ctx; |
67 | 0 | return err; |
68 | | |
69 | 0 | fail: |
70 | 0 | talloc_free(ctx); |
71 | 0 | return err; |
72 | 0 | } |
73 | | |
74 | | |
75 | | /** |
76 | | * add a string to a talloced array of strings. |
77 | | */ |
78 | | sbcErr smbconf_add_string_to_array(TALLOC_CTX *mem_ctx, |
79 | | char ***array, |
80 | | uint32_t count, |
81 | | const char *string) |
82 | 0 | { |
83 | 0 | char **new_array = NULL; |
84 | |
|
85 | 0 | if (array == NULL) { |
86 | 0 | return SBC_ERR_INVALID_PARAM; |
87 | 0 | } |
88 | | |
89 | 0 | new_array = talloc_realloc(mem_ctx, *array, char *, count + 1); |
90 | 0 | if (new_array == NULL) { |
91 | 0 | return SBC_ERR_NOMEM; |
92 | 0 | } |
93 | | |
94 | 0 | if (string == NULL) { |
95 | 0 | new_array[count] = NULL; |
96 | 0 | } else { |
97 | 0 | new_array[count] = talloc_strdup(new_array, string); |
98 | 0 | if (new_array[count] == NULL) { |
99 | 0 | talloc_free(new_array); |
100 | 0 | return SBC_ERR_NOMEM; |
101 | 0 | } |
102 | 0 | } |
103 | | |
104 | 0 | *array = new_array; |
105 | |
|
106 | 0 | return SBC_ERR_OK; |
107 | 0 | } |
108 | | |
109 | | bool smbconf_find_in_array(const char *string, char **list, |
110 | | uint32_t num_entries, uint32_t *entry) |
111 | 0 | { |
112 | 0 | uint32_t i; |
113 | |
|
114 | 0 | if (list == NULL) { |
115 | 0 | return false; |
116 | 0 | } |
117 | | |
118 | 0 | for (i = 0; i < num_entries; i++) { |
119 | 0 | if (((string == NULL) && (list[i] == NULL)) || |
120 | 0 | strequal(string, list[i])) |
121 | 0 | { |
122 | 0 | if (entry != NULL) { |
123 | 0 | *entry = i; |
124 | 0 | } |
125 | 0 | return true; |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | 0 | return false; |
130 | 0 | } |
131 | | |
132 | | bool smbconf_reverse_find_in_array(const char *string, char **list, |
133 | | uint32_t num_entries, uint32_t *entry) |
134 | 0 | { |
135 | 0 | int32_t i; |
136 | |
|
137 | 0 | if ((string == NULL) || (list == NULL) || (num_entries == 0)) { |
138 | 0 | return false; |
139 | 0 | } |
140 | | |
141 | 0 | for (i = num_entries - 1; i >= 0; i--) { |
142 | 0 | if (strequal(string, list[i])) { |
143 | 0 | if (entry != NULL) { |
144 | 0 | *entry = i; |
145 | 0 | } |
146 | 0 | return true; |
147 | 0 | } |
148 | 0 | } |
149 | | |
150 | 0 | return false; |
151 | 0 | } |