2 Unix SMB/Netbios implementation.
4 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-1999
6 Copyright (C) Luke Kenneth Casson Leighton 1996-1999
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.
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.
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.
25 void free_void_array(uint32 num_entries, void **entries,
26 void(free_item)(void*))
31 for (i = 0; i < num_entries; i++)
33 if (entries[i] != NULL)
35 free_item(entries[i]);
42 void* add_copy_to_array(uint32 *len, void ***array, const void *item,
43 void*(item_dup)(const void*), BOOL alloc_anyway)
45 if (len == NULL || array == NULL)
50 if (item != NULL || alloc_anyway)
53 if (item != NULL || alloc_anyway)
55 copy = item_dup(item);
57 add_item_to_array(len, array, copy);
62 void* add_item_to_array(uint32 *len, void ***array, void *item)
64 if (len == NULL || array == NULL)
69 (*array) = (void**)Realloc((*array), ((*len)+1)*sizeof((*array)[0]));
73 (*array)[(*len)] = item;
80 static void use_info_free(struct use_info *item)
84 if (item->srv_name != NULL)
88 if (item->user_name != NULL)
90 free(item->user_name);
92 if (item->domain != NULL)
100 static struct use_info *use_info_dup(const struct use_info *from)
104 struct use_info *copy = (struct use_info *)
105 malloc(sizeof(struct use_info));
109 copy->connected = from->connected;
110 if (from->srv_name != NULL)
112 copy->srv_name = strdup(from->srv_name );
114 if (from->user_name != NULL)
116 copy->user_name = strdup(from->user_name);
118 if (from->domain != NULL)
120 copy->domain = strdup(from->domain );
128 void free_use_info_array(uint32 num_entries, struct use_info **entries)
130 void(*fn)(void*) = (void(*)(void*))&use_info_free;
131 free_void_array(num_entries, (void**)entries, *fn);
134 struct use_info* add_use_info_to_array(uint32 *len, struct use_info ***array,
135 const struct use_info *name)
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);
143 void free_char_array(uint32 num_entries, char **entries)
145 void(*fn)(void*) = (void(*)(void*))&free;
146 free_void_array(num_entries, (void**)entries, *fn);
149 char* add_chars_to_array(uint32 *len, char ***array, const char *name)
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);
157 static uint32 *uint32_dup(const uint32* from)
161 uint32 *copy = (uint32 *)malloc(sizeof(uint32));
164 memcpy(copy, from, sizeof(*copy));
171 void free_uint32_array(uint32 num_entries, uint32 **entries)
173 void(*fn)(void*) = (void(*)(void*))&free;
174 free_void_array(num_entries, (void**)entries, *fn);
177 uint32* add_uint32s_to_array(uint32 *len, uint32 ***array, const uint32 *name)
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);
185 void free_unistr_array(uint32 num_entries, UNISTR2 **entries)
187 void(*fn)(void*) = (void(*)(void*))&unistr2_free;
188 free_void_array(num_entries, (void**)entries, *fn);
191 UNISTR2* add_unistr_to_array(uint32 *len, UNISTR2 ***array, UNISTR2 *name)
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);
198 void free_sid_array(uint32 num_entries, DOM_SID **entries)
200 void(*fn)(void*) = (void(*)(void*))&free;
201 free_void_array(num_entries, (void**)entries, *fn);
204 DOM_SID* add_sid_to_array(uint32 *len, DOM_SID ***array, const DOM_SID *sid)
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);
211 void free_devmode(DEVICEMODE *devmode)
215 if (devmode->private!=NULL)
216 free(devmode->private);
221 void free_printer_info_2(PRINTER_INFO_2 *printer)
225 free_devmode(printer->devmode);
230 static PRINTER_INFO_2 *prt2_dup(const PRINTER_INFO_2* from)
232 PRINTER_INFO_2 *copy = (PRINTER_INFO_2 *)malloc(sizeof(PRINTER_INFO_2));
237 memcpy(copy, from, sizeof(*copy));
247 void free_print2_array(uint32 num_entries, PRINTER_INFO_2 **entries)
249 void(*fn)(void*) = (void(*)(void*))&free_printer_info_2;
250 free_void_array(num_entries, (void**)entries, *fn);
253 PRINTER_INFO_2 *add_print2_to_array(uint32 *len, PRINTER_INFO_2 ***array,
254 const PRINTER_INFO_2 *prt)
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);
261 static PRINTER_INFO_1 *prt1_dup(const PRINTER_INFO_1* from)
263 PRINTER_INFO_1 *copy = (PRINTER_INFO_1 *)malloc(sizeof(PRINTER_INFO_1));
268 memcpy(copy, from, sizeof(*copy));
278 void free_print1_array(uint32 num_entries, PRINTER_INFO_1 **entries)
280 void(*fn)(void*) = (void(*)(void*))&free;
281 free_void_array(num_entries, (void**)entries, *fn);
284 PRINTER_INFO_1 *add_print1_to_array(uint32 *len, PRINTER_INFO_1 ***array,
285 const PRINTER_INFO_1 *prt)
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);
292 static JOB_INFO_1 *job1_dup(const JOB_INFO_1* from)
294 JOB_INFO_1 *copy = (JOB_INFO_1 *)malloc(sizeof(JOB_INFO_1));
299 memcpy(copy, from, sizeof(*copy));
309 void free_job1_array(uint32 num_entries, JOB_INFO_1 **entries)
311 void(*fn)(void*) = (void(*)(void*))&free;
312 free_void_array(num_entries, (void**)entries, *fn);
315 JOB_INFO_1 *add_job1_to_array(uint32 *len, JOB_INFO_1 ***array,
316 const JOB_INFO_1 *job)
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);
323 static JOB_INFO_2 *job2_dup(const JOB_INFO_2* from)
325 JOB_INFO_2 *copy = (JOB_INFO_2 *)malloc(sizeof(JOB_INFO_2));
330 memcpy(copy, from, sizeof(*copy));
340 void free_job2_array(uint32 num_entries, JOB_INFO_2 **entries)
342 void(*fn)(void*) = (void(*)(void*))&free;
343 free_void_array(num_entries, (void**)entries, *fn);
346 JOB_INFO_2 *add_job2_to_array(uint32 *len, JOB_INFO_2 ***array,
347 const JOB_INFO_2 *job)
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);