Lines Matching full:vector
2 * Vector handling (counted lists of char *'s).
4 * A vector is a table for handling a list of strings with less overhead than
9 * This is based on the util/vector.c library, but that library uses xmalloc
11 * modified version of the vector library that instead returns false on
17 * Only the portions of the vector library used by PAM modules are
39 #include <pam-util/vector.h>
43 * Allocate a new, empty vector. Returns NULL if memory allocation fails.
45 struct vector *
48 struct vector *vector; in vector_new() local
50 vector = calloc(1, sizeof(struct vector)); in vector_new()
51 vector->allocated = 1; in vector_new()
52 vector->strings = calloc(1, sizeof(char *)); in vector_new()
53 return vector; in vector_new()
58 * Allocate a new vector that's a copy of an existing vector. Returns NULL if
61 struct vector *
62 vector_copy(const struct vector *old) in vector_copy()
64 struct vector *vector; in vector_copy() local
67 vector = vector_new(); in vector_copy()
68 if (!vector_resize(vector, old->count)) { in vector_copy()
69 vector_free(vector); in vector_copy()
72 vector->count = old->count; in vector_copy()
74 vector->strings[i] = strdup(old->strings[i]); in vector_copy()
75 if (vector->strings[i] == NULL) { in vector_copy()
76 vector_free(vector); in vector_copy()
80 return vector; in vector_copy()
85 * Resize a vector (using reallocarray to resize the table). Return false if
89 vector_resize(struct vector *vector, size_t size) in vector_resize() argument
94 if (vector->count > size) { in vector_resize()
95 for (i = size; i < vector->count; i++) in vector_resize()
96 free(vector->strings[i]); in vector_resize()
97 vector->count = size; in vector_resize()
101 strings = reallocarray(vector->strings, size, sizeof(char *)); in vector_resize()
104 vector->strings = strings; in vector_resize()
105 vector->allocated = size; in vector_resize()
111 * Add a new string to the vector, resizing the vector as necessary. The
112 * vector is resized an element at a time; if a lot of resizes are expected,
117 vector_add(struct vector *vector, const char *string) in vector_add() argument
119 size_t next = vector->count; in vector_add()
121 if (vector->count == vector->allocated) in vector_add()
122 if (!vector_resize(vector, vector->allocated + 1)) in vector_add()
124 vector->strings[next] = strdup(string); in vector_add()
125 if (vector->strings[next] == NULL) in vector_add()
127 vector->count++; in vector_add()
133 * Empty a vector but keep the allocated memory for the pointer table.
136 vector_clear(struct vector *vector) in vector_clear() argument
140 for (i = 0; i < vector->count; i++) in vector_clear()
141 if (vector->strings[i] != NULL) in vector_clear()
142 free(vector->strings[i]); in vector_clear()
143 vector->count = 0; in vector_clear()
148 * Free a vector completely.
151 vector_free(struct vector *vector) in vector_free() argument
153 if (vector == NULL) in vector_free()
155 vector_clear(vector); in vector_free()
156 free(vector->strings); in vector_free()
157 free(vector); in vector_free()
162 * Given a vector that we may be reusing, clear it out. If the first argument
163 * is NULL, allocate a new vector. Used by vector_split*. Returns NULL if
166 static struct vector *
167 vector_reuse(struct vector *vector) in vector_reuse() argument
169 if (vector == NULL) in vector_reuse()
172 vector_clear(vector); in vector_reuse()
173 return vector; in vector_reuse()
207 * vector, copying each string segment. If the third argument isn't NULL,
208 * reuse that vector; otherwise, allocate a new one. Any number of
210 * memory allocation failure, after which the provided vector may only have
213 struct vector *
214 vector_split_multi(const char *string, const char *seps, struct vector *vector) in vector_split_multi() argument
220 if (vector == NULL) in vector_split_multi()
222 vector = vector_reuse(vector); in vector_split_multi()
223 if (vector == NULL) in vector_split_multi()
227 if (vector->allocated < count && !vector_resize(vector, count)) in vector_split_multi()
230 vector->count = 0; in vector_split_multi()
234 vector->strings[i] = strndup(start, (size_t)(p - start)); in vector_split_multi()
235 if (vector->strings[i] == NULL) in vector_split_multi()
238 vector->count++; in vector_split_multi()
243 vector->strings[i] = strndup(start, (size_t)(p - start)); in vector_split_multi()
244 if (vector->strings[i] == NULL) in vector_split_multi()
246 vector->count++; in vector_split_multi()
248 return vector; in vector_split_multi()
252 vector_free(vector); in vector_split_multi()
258 * Given a vector and a path to a program, exec that program with the vector
259 * as its arguments. This requires adding a NULL terminator to the vector and
264 vector_exec(const char *path, struct vector *vector) in vector_exec() argument
266 if (vector->allocated == vector->count) in vector_exec()
267 if (!vector_resize(vector, vector->count + 1)) in vector_exec()
269 vector->strings[vector->count] = NULL; in vector_exec()
270 return execv(path, (char *const *) vector->strings); in vector_exec()
275 * Given a vector, a path to a program, and the environment, exec that program
276 * with the vector as its arguments and the given environment. This requires
277 * adding a NULL terminator to the vector and casting it appropriately.
281 vector_exec_env(const char *path, struct vector *vector, in vector_exec_env() argument
284 if (vector->allocated == vector->count) in vector_exec_env()
285 if (!vector_resize(vector, vector->count + 1)) in vector_exec_env()
287 vector->strings[vector->count] = NULL; in vector_exec_env()
288 return execve(path, (char *const *) vector->strings, (char *const *) env); in vector_exec_env()