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)
46 if (len == NULL || array == NULL)
51 if (item != NULL || alloc_anyway)
53 copy = item_dup(item);
54 return add_item_to_array(len, array, copy);
59 void* add_item_to_array(uint32 *len, void ***array, void *item)
61 if (len == NULL || array == NULL)
66 (*array) = (void**)Realloc((*array), ((*len)+1)*sizeof((*array)[0]));
70 (*array)[(*len)] = item;
77 static void use_info_free(struct use_info *item)
81 if (item->srv_name != NULL)
85 if (item->user_name != NULL)
87 free(item->user_name);
89 if (item->domain != NULL)
97 static struct use_info *use_info_dup(const struct use_info *from)
101 struct use_info *copy = (struct use_info *)
102 malloc(sizeof(struct use_info));
106 copy->connected = from->connected;
107 if (from->srv_name != NULL)
109 copy->srv_name = strdup(from->srv_name );
111 if (from->user_name != NULL)
113 copy->user_name = strdup(from->user_name);
115 if (from->domain != NULL)
117 copy->domain = strdup(from->domain );
125 void free_use_info_array(uint32 num_entries, struct use_info **entries)
127 void(*fn)(void*) = (void(*)(void*))&use_info_free;
128 free_void_array(num_entries, (void**)entries, *fn);
131 struct use_info* add_use_info_to_array(uint32 *len, struct use_info ***array,
132 const struct use_info *name)
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);
140 void free_char_array(uint32 num_entries, char **entries)
142 void(*fn)(void*) = (void(*)(void*))&free;
143 free_void_array(num_entries, (void**)entries, *fn);
146 char* add_chars_to_array(uint32 *len, char ***array, const char *name)
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);
154 static uint32 *uint32_dup(const uint32* from)
158 uint32 *copy = (uint32 *)malloc(sizeof(uint32));
161 memcpy(copy, from, sizeof(*copy));
168 void free_uint32_array(uint32 num_entries, uint32 **entries)
170 void(*fn)(void*) = (void(*)(void*))&free;
171 free_void_array(num_entries, (void**)entries, *fn);
174 uint32* add_uint32s_to_array(uint32 *len, uint32 ***array, const uint32 *name)
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);
182 void free_unistr_array(uint32 num_entries, UNISTR2 **entries)
184 void(*fn)(void*) = (void(*)(void*))&unistr2_free;
185 free_void_array(num_entries, (void**)entries, *fn);
188 UNISTR2* add_unistr_to_array(uint32 *len, UNISTR2 ***array, UNISTR2 *name)
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);
195 void free_sid_array(uint32 num_entries, DOM_SID **entries)
197 void(*fn)(void*) = (void(*)(void*))&free;
198 free_void_array(num_entries, (void**)entries, *fn);
201 DOM_SID* add_sid_to_array(uint32 *len, DOM_SID ***array, const DOM_SID *sid)
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);
208 void free_devmode(DEVICEMODE *devmode)
212 if (devmode->private!=NULL)
213 free(devmode->private);
218 void free_printer_info_2(PRINTER_INFO_2 *printer)
222 free_devmode(printer->devmode);
227 static PRINTER_INFO_2 *prt2_dup(const PRINTER_INFO_2* from)
229 PRINTER_INFO_2 *copy = (PRINTER_INFO_2 *)malloc(sizeof(PRINTER_INFO_2));
234 memcpy(copy, from, sizeof(*copy));
244 void free_print2_array(uint32 num_entries, PRINTER_INFO_2 **entries)
246 void(*fn)(void*) = (void(*)(void*))&free_printer_info_2;
247 free_void_array(num_entries, (void**)entries, *fn);
250 PRINTER_INFO_2 *add_print2_to_array(uint32 *len, PRINTER_INFO_2 ***array,
251 const PRINTER_INFO_2 *prt)
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);
258 static PRINTER_INFO_1 *prt1_dup(const PRINTER_INFO_1* from)
260 PRINTER_INFO_1 *copy = (PRINTER_INFO_1 *)malloc(sizeof(PRINTER_INFO_1));
265 memcpy(copy, from, sizeof(*copy));
275 void free_print1_array(uint32 num_entries, PRINTER_INFO_1 **entries)
277 void(*fn)(void*) = (void(*)(void*))&free;
278 free_void_array(num_entries, (void**)entries, *fn);
281 PRINTER_INFO_1 *add_print1_to_array(uint32 *len, PRINTER_INFO_1 ***array,
282 const PRINTER_INFO_1 *prt)
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);
289 static JOB_INFO_1 *job1_dup(const JOB_INFO_1* from)
291 JOB_INFO_1 *copy = (JOB_INFO_1 *)malloc(sizeof(JOB_INFO_1));
296 memcpy(copy, from, sizeof(*copy));
306 void free_job1_array(uint32 num_entries, JOB_INFO_1 **entries)
308 void(*fn)(void*) = (void(*)(void*))&free;
309 free_void_array(num_entries, (void**)entries, *fn);
312 JOB_INFO_1 *add_job1_to_array(uint32 *len, JOB_INFO_1 ***array,
313 const JOB_INFO_1 *job)
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);
320 static JOB_INFO_2 *job2_dup(const JOB_INFO_2* from)
322 JOB_INFO_2 *copy = (JOB_INFO_2 *)malloc(sizeof(JOB_INFO_2));
327 memcpy(copy, from, sizeof(*copy));
337 void free_job2_array(uint32 num_entries, JOB_INFO_2 **entries)
339 void(*fn)(void*) = (void(*)(void*))&free;
340 free_void_array(num_entries, (void**)entries, *fn);
343 JOB_INFO_2 *add_job2_to_array(uint32 *len, JOB_INFO_2 ***array,
344 const JOB_INFO_2 *job)
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);