#include <errno.h>
#include <stddef.h>
+#include <stdio.h>
#include <stdlib.h>
#include "args.h"
struct collecty_args {
collecty_ctx* ctx;
int nrefs;
+
+ // Arguments
+ char** argv;
+ int argc;
};
static void collecty_args_free(collecty_args* self) {
+ if (self->argv) {
+ for (int i = 0; i < self->argc; i++)
+ free(self->argv[i]);
+ free(self->argv);
+ }
if (self->ctx)
collecty_ctx_unref(self->ctx);
free(self);
collecty_args_free(self);
return NULL;
}
+
+int collecty_args_push(collecty_args* self, const char* format, ...) {
+ char** argv = NULL;
+ char* arg = NULL;
+ va_list args;
+ int r;
+
+ // Format the argument
+ va_start(args, format);
+ r = vasprintf(&arg, format, args);
+ va_end(args);
+
+ // Abort if we could not format the string
+ if (r < 0)
+ goto ERROR;
+
+ // Grow argv
+ argv = reallocarray(self->argv, self->argc + 1, sizeof(*self->argv));
+ if (!argv)
+ goto ERROR;
+
+ // Reference the string
+ argv[self->argc + 0] = arg;
+ argv[self->argc + 1] = NULL;
+
+ // Replace the array and increment the counter
+ self->argv = argv;
+ self->argc++;
+
+ return 0;
+
+ERROR:
+ if (arg)
+ free(arg);
+
+ return -errno;
+}
collecty_args* collecty_args_ref(collecty_args* self);
collecty_args* collecty_args_unref(collecty_args* self);
+int collecty_args_push(collecty_args* self, const char* format, ...)
+ __attribute__((format(printf, 2, 3)));
+
#endif /* COLLECTY_ARGS_H */