5a21053cc6b56ebe4d6246d00fa64caf33cd8694
[kai/samba-autobuild/.git] / source3 / 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         if (len == NULL || array == NULL)
46         {
47                 return NULL;
48         }
49
50         if (item != NULL || alloc_anyway)
51         {
52                 void* copy = NULL;
53                 if (item != NULL || alloc_anyway)
54                 {
55                         copy = item_dup(item);
56                 }
57                 add_item_to_array(len, array, copy);
58         }
59         return NULL;
60 }
61
62 void* add_item_to_array(uint32 *len, void ***array, void *item)
63 {
64         if (len == NULL || array == NULL)
65         {
66                 return NULL;
67         }
68
69         (*array) = (void**)Realloc((*array), ((*len)+1)*sizeof((*array)[0]));
70
71         if ((*array) != NULL)
72         {
73                 (*array)[(*len)] = item;
74                 (*len)++;
75                 return item;
76         }
77         return NULL;
78 }
79
80 static void use_info_free(struct use_info *item)
81 {
82         if (item != NULL)
83         {
84                 if (item->srv_name != NULL)
85                 {
86                         free(item->srv_name);
87                 }
88                 if (item->user_name != NULL)
89                 {
90                         free(item->user_name);
91                 }
92                 if (item->domain != NULL)
93                 {
94                         free(item->domain);
95                 }
96                 free(item);
97         }
98 }
99
100 static struct use_info *use_info_dup(const struct use_info *from)
101 {
102         if (from != NULL)
103         {
104                 struct use_info *copy = (struct use_info *)
105                                         malloc(sizeof(struct use_info));
106                 if (copy != NULL)
107                 {
108                         ZERO_STRUCTP(copy);
109                         copy->connected = from->connected;
110                         if (from->srv_name != NULL)
111                         {
112                                 copy->srv_name  = strdup(from->srv_name );
113                         }
114                         if (from->user_name != NULL)
115                         {
116                                 copy->user_name = strdup(from->user_name);
117                         }
118                         if (from->domain != NULL)
119                         {
120                                 copy->domain    = strdup(from->domain   );
121                         }
122                 }
123                 return copy;
124         }
125         return NULL;
126 }
127
128 void free_use_info_array(uint32 num_entries, struct use_info **entries)
129 {
130         void(*fn)(void*) = (void(*)(void*))&use_info_free;
131         free_void_array(num_entries, (void**)entries, *fn);
132 }
133
134 struct use_info* add_use_info_to_array(uint32 *len, struct use_info ***array,
135                                 const struct use_info *name)
136 {
137         void*(*fn)(const void*) = (void*(*)(const void*))&use_info_dup;
138         return (struct use_info*)add_copy_to_array(len,
139                              (void***)array, (const void*)name, *fn, False);
140                                 
141 }
142
143 void free_char_array(uint32 num_entries, char **entries)
144 {
145         void(*fn)(void*) = (void(*)(void*))&free;
146         free_void_array(num_entries, (void**)entries, *fn);
147 }
148
149 char* add_chars_to_array(uint32 *len, char ***array, const char *name)
150 {
151         void*(*fn)(const void*) = (void*(*)(const void*))&strdup;
152         return (char*)add_copy_to_array(len,
153                              (void***)array, (const void*)name, *fn, False);
154                                 
155 }
156
157 static uint32 *uint32_dup(const uint32* from)
158 {
159         if (from != NULL)
160         {
161                 uint32 *copy = (uint32 *)malloc(sizeof(uint32));
162                 if (copy != NULL)
163                 {
164                         memcpy(copy, from, sizeof(*copy));
165                 }
166                 return copy;
167         }
168         return NULL;
169 }
170
171 void free_uint32_array(uint32 num_entries, uint32 **entries)
172 {
173         void(*fn)(void*) = (void(*)(void*))&free;
174         free_void_array(num_entries, (void**)entries, *fn);
175 }
176
177 uint32* add_uint32s_to_array(uint32 *len, uint32 ***array, const uint32 *name)
178 {
179         void*(*fn)(const void*) = (void*(*)(const void*))&uint32_dup;
180         return (uint32*)add_copy_to_array(len,
181                              (void***)array, (const void*)name, *fn, False);
182                                 
183 }
184
185 void free_unistr_array(uint32 num_entries, UNISTR2 **entries)
186 {
187         void(*fn)(void*) = (void(*)(void*))&unistr2_free;
188         free_void_array(num_entries, (void**)entries, *fn);
189 }
190
191 UNISTR2* add_unistr_to_array(uint32 *len, UNISTR2 ***array, UNISTR2 *name)
192 {
193         void*(*fn)(const void*) = (void*(*)(const void*))&unistr2_dup;
194         return (UNISTR2*)add_copy_to_array(len,
195                            (void***)array, (const void*)name, *fn, False);
196 }
197
198 void free_sid_array(uint32 num_entries, DOM_SID **entries)
199 {
200         void(*fn)(void*) = (void(*)(void*))&free;
201         free_void_array(num_entries, (void**)entries, *fn);
202 }
203
204 DOM_SID* add_sid_to_array(uint32 *len, DOM_SID ***array, const DOM_SID *sid)
205 {
206         void*(*fn)(const void*) = (void*(*)(const void*))&sid_dup;
207         return (DOM_SID*)add_copy_to_array(len,
208                           (void***)array, (const void*)sid, *fn, False);
209 }
210
211 void free_devmode(DEVICEMODE *devmode)
212 {
213         if (devmode!=NULL)
214         {
215                 if (devmode->private!=NULL)
216                         free(devmode->private);
217                 free(devmode);
218         }
219 }
220
221 void free_printer_info_2(PRINTER_INFO_2 *printer)
222 {
223         if (printer!=NULL)
224         {
225                 free_devmode(printer->devmode);
226                 free(printer);
227         }
228 }
229
230 static PRINTER_INFO_2 *prt2_dup(const PRINTER_INFO_2* from)
231 {
232         PRINTER_INFO_2 *copy = (PRINTER_INFO_2 *)malloc(sizeof(PRINTER_INFO_2));
233         if (copy != NULL)
234         {
235                 if (from != NULL)
236                 {
237                         memcpy(copy, from, sizeof(*copy));
238                 }
239                 else
240                 {
241                         ZERO_STRUCTP(copy);
242                 }
243         }
244         return copy;
245 }
246
247 void free_print2_array(uint32 num_entries, PRINTER_INFO_2 **entries)
248 {
249         void(*fn)(void*) = (void(*)(void*))&free_printer_info_2;
250         free_void_array(num_entries, (void**)entries, *fn);
251 }
252
253 PRINTER_INFO_2 *add_print2_to_array(uint32 *len, PRINTER_INFO_2 ***array,
254                                 const PRINTER_INFO_2 *prt)
255 {
256         void*(*fn)(const void*) = (void*(*)(const void*))&prt2_dup;
257         return (PRINTER_INFO_2*)add_copy_to_array(len,
258                    (void***)array, (const void*)prt, *fn, True);
259 }
260
261 static PRINTER_INFO_1 *prt1_dup(const PRINTER_INFO_1* from)
262 {
263         PRINTER_INFO_1 *copy = (PRINTER_INFO_1 *)malloc(sizeof(PRINTER_INFO_1));
264         if (copy != NULL)
265         {
266                 if (from != NULL)
267                 {
268                         memcpy(copy, from, sizeof(*copy));
269                 }
270                 else
271                 {
272                         ZERO_STRUCTP(copy);
273                 }
274         }
275         return copy;
276 }
277
278 void free_print1_array(uint32 num_entries, PRINTER_INFO_1 **entries)
279 {
280         void(*fn)(void*) = (void(*)(void*))&free;
281         free_void_array(num_entries, (void**)entries, *fn);
282 }
283
284 PRINTER_INFO_1 *add_print1_to_array(uint32 *len, PRINTER_INFO_1 ***array,
285                                 const PRINTER_INFO_1 *prt)
286 {
287         void*(*fn)(const void*) = (void*(*)(const void*))&prt1_dup;
288         return (PRINTER_INFO_1*)add_copy_to_array(len,
289                            (void***)array, (const void*)prt, *fn, True);
290 }
291
292 static JOB_INFO_1 *job1_dup(const JOB_INFO_1* from)
293 {
294         JOB_INFO_1 *copy = (JOB_INFO_1 *)malloc(sizeof(JOB_INFO_1));
295         if (copy != NULL)
296         {
297                 if (from != NULL)
298                 {
299                         memcpy(copy, from, sizeof(*copy));
300                 }
301                 else
302                 {
303                         ZERO_STRUCTP(copy);
304                 }
305         }
306         return copy;
307 }
308
309 void free_job1_array(uint32 num_entries, JOB_INFO_1 **entries)
310 {
311         void(*fn)(void*) = (void(*)(void*))&free;
312         free_void_array(num_entries, (void**)entries, *fn);
313 }
314
315 JOB_INFO_1 *add_job1_to_array(uint32 *len, JOB_INFO_1 ***array,
316                                 const JOB_INFO_1 *job)
317 {
318         void*(*fn)(const void*) = (void*(*)(const void*))&job1_dup;
319         return (JOB_INFO_1*)add_copy_to_array(len,
320                            (void***)array, (const void*)job, *fn, True);
321 }
322
323 static JOB_INFO_2 *job2_dup(const JOB_INFO_2* from)
324 {
325         JOB_INFO_2 *copy = (JOB_INFO_2 *)malloc(sizeof(JOB_INFO_2));
326         if (copy != NULL)
327         {
328                 if (from != NULL)
329                 {
330                         memcpy(copy, from, sizeof(*copy));
331                 }
332                 else
333                 {
334                         ZERO_STRUCTP(copy);
335                 }
336         }
337         return copy;
338 }
339
340 void free_job2_array(uint32 num_entries, JOB_INFO_2 **entries)
341 {
342         void(*fn)(void*) = (void(*)(void*))&free;
343         free_void_array(num_entries, (void**)entries, *fn);
344 }
345
346 JOB_INFO_2 *add_job2_to_array(uint32 *len, JOB_INFO_2 ***array,
347                                 const JOB_INFO_2 *job)
348 {
349         void*(*fn)(const void*) = (void*(*)(const void*))&job2_dup;
350         return (JOB_INFO_2*)add_copy_to_array(len,
351                            (void***)array, (const void*)job, *fn, True);
352 }
353