2 #include "argv-array.h"
5 const char *empty_argv[] = { NULL };
7 void argv_array_init(struct argv_array *array)
9 array->argv = empty_argv;
14 static void argv_array_push_nodup(struct argv_array *array, const char *value)
16 if (array->argv == empty_argv)
19 ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
20 array->argv[array->argc++] = value;
21 array->argv[array->argc] = NULL;
24 void argv_array_push(struct argv_array *array, const char *value)
26 argv_array_push_nodup(array, xstrdup(value));
29 void argv_array_pushf(struct argv_array *array, const char *fmt, ...)
32 struct strbuf v = STRBUF_INIT;
35 strbuf_vaddf(&v, fmt, ap);
38 argv_array_push_nodup(array, strbuf_detach(&v, NULL));
41 void argv_array_pushl(struct argv_array *array, ...)
47 while((arg = va_arg(ap, const char *)))
48 argv_array_push(array, arg);
52 void argv_array_pushv(struct argv_array *array, const char **argv)
55 argv_array_push(array, *argv);
58 void argv_array_pop(struct argv_array *array)
62 free((char *)array->argv[array->argc - 1]);
63 array->argv[array->argc - 1] = NULL;
67 void argv_array_clear(struct argv_array *array)
69 if (array->argv != empty_argv) {
71 for (i = 0; i < array->argc; i++)
72 free((char *)array->argv[i]);
75 argv_array_init(array);
78 const char **argv_array_detach(struct argv_array *array)
80 if (array->argv == empty_argv)
81 return xcalloc(1, sizeof(const char *));
83 const char **ret = array->argv;
84 argv_array_init(array);