first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/samba-autobuild/.git] / source / lib / util_array.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Samba utility functions
5    Copyright (C) Andrew Tridgell              1992-1999
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1999
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 void free_void_array(uint32 num_entries, void **entries,
26                 void(free_item)(void*))
27 {
28         uint32 i;
29         if (entries != NULL)
30         {
31                 for (i = 0; i < num_entries; i++)
32                 {
33                         if (entries[i] != NULL)
34                         {
35                                 free_item(entries[i]);
36                         }
37                 }
38                 free(entries);
39         }
40 }
41
42 void* add_copy_to_array(uint32 *len, void ***array, const void *item,
43         void*(item_dup)(const void*), BOOL alloc_anyway)
44 {
45         void* copy = NULL;
46         if (len == NULL || array == NULL)
47         {
48                 return NULL;
49         }
50
51         if (item != NULL || alloc_anyway)
52         {
53                 copy = item_dup(item);
54                 return add_item_to_array(len, array, copy);
55         }
56         return copy;
57 }
58
59 void* add_item_to_array(uint32 *len, void ***array, void *item)
60 {
61         if (len == NULL || array == NULL)
62         {
63                 return NULL;
64         }
65
66         (*array) = (void**)Realloc((*array), ((*len)+1)*sizeof((*array)[0]));
67
68         if ((*array) != NULL)
69         {
70                 (*array)[(*len)] = item;
71                 (*len)++;
72                 return item;
73         }
74         return NULL;
75 }
76
77 static void use_info_free(struct use_info *item)
78 {
79         if (item != NULL)
80         {
81                 if (item->srv_name != NULL)
82                 {
83                         free(item->srv_name);
84                 }
85                 if (item->user_name != NULL)
86                 {
87                         free(item->user_name);
88                 }
89                 if (item->domain != NULL)
90                 {
91                         free(item->domain);
92                 }
93                 free(item);
94         }
95 }
96
97 static struct use_info *use_info_dup(const struct use_info *from)
98 {
99         if (from != NULL)
100         {
101                 struct use_info *copy = (struct use_info *)
102                                         malloc(sizeof(struct use_info));
103                 if (copy != NULL)
104                 {
105                         ZERO_STRUCTP(copy);
106                         copy->connected = from->connected;
107                         if (from->srv_name != NULL)
108                         {
109                                 copy->srv_name  = strdup(from->srv_name );
110                         }
111                         if (from->user_name != NULL)
112                         {
113                                 copy->user_name = strdup(from->user_name);
114                         }
115                         if (from->domain != NULL)
116                         {
117                                 copy->domain    = strdup(from->domain   );
118                         }
119                 }
120                 return copy;
121         }
122         return NULL;
123 }
124
125 void free_use_info_array(uint32 num_entries, struct use_info **entries)
126 {
127         void(*fn)(void*) = (void(*)(void*))&use_info_free;
128         free_void_array(num_entries, (void**)entries, *fn);
129 }
130
131 struct use_info* add_use_info_to_array(uint32 *len, struct use_info ***array,
132                                 const struct use_info *name)
133 {
134         void*(*fn)(const void*) = (void*(*)(const void*))&use_info_dup;
135         return (struct use_info*)add_copy_to_array(len,
136                              (void***)array, (const void*)name, *fn, False);
137                                 
138 }
139
140 void free_char_array(uint32 num_entries, char **entries)
141 {
142         void(*fn)(void*) = (void(*)(void*))&free;
143         free_void_array(num_entries, (void**)entries, *fn);
144 }
145
146 char* add_chars_to_array(uint32 *len, char ***array, const char *name)
147 {
148         void*(*fn)(const void*) = (void*(*)(const void*))&strdup;
149         return (char*)add_copy_to_array(len,
150                              (void***)array, (const void*)name, *fn, False);
151                                 
152 }
153
154 static uint32 *uint32_dup(const uint32* from)
155 {
156         if (from != NULL)
157         {
158                 uint32 *copy = (uint32 *)malloc(sizeof(uint32));
159                 if (copy != NULL)
160                 {
161                         memcpy(copy, from, sizeof(*copy));
162                 }
163                 return copy;
164         }
165         return NULL;
166 }
167
168 void free_uint32_array(uint32 num_entries, uint32 **entries)
169 {
170         void(*fn)(void*) = (void(*)(void*))&free;
171         free_void_array(num_entries, (void**)entries, *fn);
172 }
173
174 uint32* add_uint32s_to_array(uint32 *len, uint32 ***array, const uint32 *name)
175 {
176         void*(*fn)(const void*) = (void*(*)(const void*))&uint32_dup;
177         return (uint32*)add_copy_to_array(len,
178                              (void***)array, (const void*)name, *fn, False);
179                                 
180 }
181
182 void free_unistr_array(uint32 num_entries, UNISTR2 **entries)
183 {
184         void(*fn)(void*) = (void(*)(void*))&unistr2_free;
185         free_void_array(num_entries, (void**)entries, *fn);
186 }
187
188 UNISTR2* add_unistr_to_array(uint32 *len, UNISTR2 ***array, UNISTR2 *name)
189 {
190         void*(*fn)(const void*) = (void*(*)(const void*))&unistr2_dup;
191         return (UNISTR2*)add_copy_to_array(len,
192                            (void***)array, (const void*)name, *fn, False);
193 }
194
195 void free_sid_array(uint32 num_entries, DOM_SID **entries)
196 {
197         void(*fn)(void*) = (void(*)(void*))&free;
198         free_void_array(num_entries, (void**)entries, *fn);
199 }
200
201 DOM_SID* add_sid_to_array(uint32 *len, DOM_SID ***array, const DOM_SID *sid)
202 {
203         void*(*fn)(const void*) = (void*(*)(const void*))&sid_dup;
204         return (DOM_SID*)add_copy_to_array(len,
205                           (void***)array, (const void*)sid, *fn, False);
206 }
207
208 void free_devmode(DEVICEMODE *devmode)
209 {
210         if (devmode!=NULL)
211         {
212                 if (devmode->private!=NULL)
213                         free(devmode->private);
214                 free(devmode);
215         }
216 }
217
218 void free_printer_info_2(PRINTER_INFO_2 *printer)
219 {
220         if (printer!=NULL)
221         {
222                 free_devmode(printer->devmode);
223                 free(printer);
224         }
225 }
226
227 static PRINTER_INFO_2 *prt2_dup(const PRINTER_INFO_2* from)
228 {
229         PRINTER_INFO_2 *copy = (PRINTER_INFO_2 *)malloc(sizeof(PRINTER_INFO_2));
230         if (copy != NULL)
231         {
232                 if (from != NULL)
233                 {
234                         memcpy(copy, from, sizeof(*copy));
235                 }
236                 else
237                 {
238                         ZERO_STRUCTP(copy);
239                 }
240         }
241         return copy;
242 }
243
244 void free_print2_array(uint32 num_entries, PRINTER_INFO_2 **entries)
245 {
246         void(*fn)(void*) = (void(*)(void*))&free_printer_info_2;
247         free_void_array(num_entries, (void**)entries, *fn);
248 }
249
250 PRINTER_INFO_2 *add_print2_to_array(uint32 *len, PRINTER_INFO_2 ***array,
251                                 const PRINTER_INFO_2 *prt)
252 {
253         void*(*fn)(const void*) = (void*(*)(const void*))&prt2_dup;
254         return (PRINTER_INFO_2*)add_copy_to_array(len,
255                    (void***)array, (const void*)prt, *fn, True);
256 }
257
258 static PRINTER_INFO_1 *prt1_dup(const PRINTER_INFO_1* from)
259 {
260         PRINTER_INFO_1 *copy = (PRINTER_INFO_1 *)malloc(sizeof(PRINTER_INFO_1));
261         if (copy != NULL)
262         {
263                 if (from != NULL)
264                 {
265                         memcpy(copy, from, sizeof(*copy));
266                 }
267                 else
268                 {
269                         ZERO_STRUCTP(copy);
270                 }
271         }
272         return copy;
273 }
274
275 void free_print1_array(uint32 num_entries, PRINTER_INFO_1 **entries)
276 {
277         void(*fn)(void*) = (void(*)(void*))&free;
278         free_void_array(num_entries, (void**)entries, *fn);
279 }
280
281 PRINTER_INFO_1 *add_print1_to_array(uint32 *len, PRINTER_INFO_1 ***array,
282                                 const PRINTER_INFO_1 *prt)
283 {
284         void*(*fn)(const void*) = (void*(*)(const void*))&prt1_dup;
285         return (PRINTER_INFO_1*)add_copy_to_array(len,
286                            (void***)array, (const void*)prt, *fn, True);
287 }
288
289 static JOB_INFO_1 *job1_dup(const JOB_INFO_1* from)
290 {
291         JOB_INFO_1 *copy = (JOB_INFO_1 *)malloc(sizeof(JOB_INFO_1));
292         if (copy != NULL)
293         {
294                 if (from != NULL)
295                 {
296                         memcpy(copy, from, sizeof(*copy));
297                 }
298                 else
299                 {
300                         ZERO_STRUCTP(copy);
301                 }
302         }
303         return copy;
304 }
305
306 void free_job1_array(uint32 num_entries, JOB_INFO_1 **entries)
307 {
308         void(*fn)(void*) = (void(*)(void*))&free;
309         free_void_array(num_entries, (void**)entries, *fn);
310 }
311
312 JOB_INFO_1 *add_job1_to_array(uint32 *len, JOB_INFO_1 ***array,
313                                 const JOB_INFO_1 *job)
314 {
315         void*(*fn)(const void*) = (void*(*)(const void*))&job1_dup;
316         return (JOB_INFO_1*)add_copy_to_array(len,
317                            (void***)array, (const void*)job, *fn, True);
318 }
319
320 static JOB_INFO_2 *job2_dup(const JOB_INFO_2* from)
321 {
322         JOB_INFO_2 *copy = (JOB_INFO_2 *)malloc(sizeof(JOB_INFO_2));
323         if (copy != NULL)
324         {
325                 if (from != NULL)
326                 {
327                         memcpy(copy, from, sizeof(*copy));
328                 }
329                 else
330                 {
331                         ZERO_STRUCTP(copy);
332                 }
333         }
334         return copy;
335 }
336
337 void free_job2_array(uint32 num_entries, JOB_INFO_2 **entries)
338 {
339         void(*fn)(void*) = (void(*)(void*))&free;
340         free_void_array(num_entries, (void**)entries, *fn);
341 }
342
343 JOB_INFO_2 *add_job2_to_array(uint32 *len, JOB_INFO_2 ***array,
344                                 const JOB_INFO_2 *job)
345 {
346         void*(*fn)(const void*) = (void*(*)(const void*))&job2_dup;
347         return (JOB_INFO_2*)add_copy_to_array(len,
348                            (void***)array, (const void*)job, *fn, True);
349 }
350