diff mbox series

[V2,05/10] Add helper to concatenate string

Message ID 20241030163957.2822282-6-stefano.babic@swupdate.org
State Accepted
Headers show
Series Introduce BTRFS Snapshot Handler | expand

Commit Message

Stefano Babic Oct. 30, 2024, 4:39 p.m. UTC
Just using strcat() is painful and there is already a function to
generate (and allocate) a string from an array of strings.

Add a helper function to concatenate two strings - this returns a new
allocated string that must be freed by the caller.

Signed-off-by: Stefano Babic <stefano.babic@swupdate.org>
---
 core/util.c    | 18 ++++++++++++++++++
 include/util.h |  1 +
 2 files changed, 19 insertions(+)

--
2.34.1
diff mbox series

Patch

diff --git a/core/util.c b/core/util.c
index d50176fc..32104279 100644
--- a/core/util.c
+++ b/core/util.c
@@ -7,6 +7,7 @@ 

 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <unistd.h>
 #include <string.h>
 #include <ctype.h>
@@ -295,6 +296,23 @@  char *mstrcat(const char **nodes, const char *delim)
 	return dest;
 }

+char *swupdate_strcat(int n, ...)
+{
+	const char *nodes[n + 1];
+
+	va_list valist;
+
+	va_start(valist, n);
+
+	for(int i = 0; i < n; i++)
+		nodes[i] = va_arg(valist, const char *);
+	nodes[n] = NULL;
+
+	va_end(valist);
+
+	return mstrcat(nodes, NULL);
+}
+
 /*
  * Alocate and return a string as part of
  * another string
diff --git a/include/util.h b/include/util.h
index dcfbd4f0..068f0195 100644
--- a/include/util.h
+++ b/include/util.h
@@ -237,6 +237,7 @@  int syslog_init(void);

 char **splitargs(char *args, int *argc);
 char *mstrcat(const char **nodes, const char *delim);
+char *swupdate_strcat(int n, ...);
 char** string_split(const char* a_str, const char a_delim);
 char *substring(const char *src, int first, int len);
 char *string_tolower(char *s);