large collection of minor fixes. Mostly typos
[Samba.git] / source / lib / util_array.c
blobdcb2a6b5f08e62c7d587be35c4f72155e09df424
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
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.
23 #include "includes.h"
25 void free_void_array(uint32 num_entries, void **entries,
26 void(free_item)(void*))
28 uint32 i;
29 if (entries != NULL)
31 for (i = 0; i < num_entries; i++)
33 if (entries[i] != NULL)
35 free_item(entries[i]);
38 free(entries);
42 void* add_copy_to_array(uint32 *len, void ***array, const void *item,
43 void*(item_dup)(const void*), BOOL alloc_anyway)
45 void* copy = NULL;
46 if (len == NULL || array == NULL)
48 return NULL;
51 if (item != NULL || alloc_anyway)
53 copy = item_dup(item);
54 return add_item_to_array(len, array, copy);
56 return copy;
59 void* add_item_to_array(uint32 *len, void ***array, void *item)
61 void **tary;
63 if (len == NULL || array == NULL)
64 return NULL;
66 tary = (void**)Realloc((*array), ((*len)+1)*sizeof((*array)[0]));
68 if (tary != NULL) {
69 (*array) = tary;
70 (*array)[(*len)] = item;
71 (*len)++;
72 return item;
73 } else {
74 free((char *)*array);
76 return NULL;
79 static void use_info_free(struct use_info *item)
81 if (item != NULL)
83 if (item->srv_name != NULL)
85 free(item->srv_name);
87 if (item->user_name != NULL)
89 free(item->user_name);
91 if (item->domain != NULL)
93 free(item->domain);
95 free(item);
99 static struct use_info *use_info_dup(const struct use_info *from)
101 if (from != NULL)
103 struct use_info *copy = (struct use_info *)
104 malloc(sizeof(struct use_info));
105 if (copy != NULL)
107 ZERO_STRUCTP(copy);
108 copy->connected = from->connected;
109 if (from->srv_name != NULL)
111 copy->srv_name = strdup(from->srv_name );
113 if (from->user_name != NULL)
115 copy->user_name = strdup(from->user_name);
117 if (from->domain != NULL)
119 copy->domain = strdup(from->domain );
122 return copy;
124 return NULL;
127 void free_use_info_array(uint32 num_entries, struct use_info **entries)
129 void(*fn)(void*) = (void(*)(void*))&use_info_free;
130 free_void_array(num_entries, (void**)entries, *fn);
133 struct use_info* add_use_info_to_array(uint32 *len, struct use_info ***array,
134 const struct use_info *name)
136 void*(*fn)(const void*) = (void*(*)(const void*))&use_info_dup;
137 return (struct use_info*)add_copy_to_array(len,
138 (void***)array, (const void*)name, *fn, False);
142 void free_char_array(uint32 num_entries, char **entries)
144 void(*fn)(void*) = (void(*)(void*))&free;
145 free_void_array(num_entries, (void**)entries, *fn);
148 char* add_chars_to_array(uint32 *len, char ***array, const char *name)
150 void*(*fn)(const void*) = (void*(*)(const void*))&strdup;
151 return (char*)add_copy_to_array(len,
152 (void***)array, (const void*)name, *fn, False);
156 static uint32 *uint32_dup(const uint32* from)
158 if (from != NULL)
160 uint32 *copy = (uint32 *)malloc(sizeof(uint32));
161 if (copy != NULL)
163 memcpy(copy, from, sizeof(*copy));
165 return copy;
167 return NULL;
170 void free_uint32_array(uint32 num_entries, uint32 **entries)
172 void(*fn)(void*) = (void(*)(void*))&free;
173 free_void_array(num_entries, (void**)entries, *fn);
176 uint32* add_uint32s_to_array(uint32 *len, uint32 ***array, const uint32 *name)
178 void*(*fn)(const void*) = (void*(*)(const void*))&uint32_dup;
179 return (uint32*)add_copy_to_array(len,
180 (void***)array, (const void*)name, *fn, False);
184 void free_sid_array(uint32 num_entries, DOM_SID **entries)
186 void(*fn)(void*) = (void(*)(void*))&free;
187 free_void_array(num_entries, (void**)entries, *fn);
190 DOM_SID* add_sid_to_array(uint32 *len, DOM_SID ***array, const DOM_SID *sid)
192 void*(*fn)(const void*) = (void*(*)(const void*))&sid_dup;
193 return (DOM_SID*)add_copy_to_array(len,
194 (void***)array, (const void*)sid, *fn, False);