From: Michael Tremer Date: Sun, 28 Sep 2025 09:04:19 +0000 (+0000) Subject: args: Implement pushing arguments to the array X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=42959fef277cd2a1ed40d169a1c71886189d6e52;p=telemetry.git args: Implement pushing arguments to the array Signed-off-by: Michael Tremer --- diff --git a/src/daemon/args.c b/src/daemon/args.c index 915d920..ed7368b 100644 --- a/src/daemon/args.c +++ b/src/daemon/args.c @@ -20,6 +20,7 @@ #include #include +#include #include #include "args.h" @@ -28,9 +29,18 @@ 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); @@ -67,3 +77,40 @@ collecty_args* collecty_args_unref(collecty_args* 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; +} diff --git a/src/daemon/args.h b/src/daemon/args.h index 458a4ee..f108e03 100644 --- a/src/daemon/args.h +++ b/src/daemon/args.h @@ -30,4 +30,7 @@ int collecty_args_create(collecty_args** args, collecty_ctx* ctx); 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 */