Copy the guestfs_int_join_strings() function from libguestfs-common @
3253cd99d135 ("mldrivers: Add support for reading Windows drivers from
DriverDatabase", 2023-01-26).
Cc: Alban Lecorps <alban.lecorps(a)ubisoft.com>
Bugzilla:
https://bugzilla.redhat.com/show_bug.cgi?id=1792141
Signed-off-by: Laszlo Ersek <lersek(a)redhat.com>
Reviewed-by: Richard W.M. Jones <rjones(a)redhat.com>
---
libguestfs/guestfs-utils.h | 1 +
libguestfs/guestfs-utils.c | 35 ++++++++++++++++++++
2 files changed, 36 insertions(+)
diff --git a/libguestfs/guestfs-utils.h b/libguestfs/guestfs-utils.h
index d5557a49680c..62402ccdd202 100644
--- a/libguestfs/guestfs-utils.h
+++ b/libguestfs/guestfs-utils.h
@@ -48,6 +48,7 @@
extern void guestfs_int_free_string_list (char **);
extern size_t guestfs_int_count_strings (char *const *);
extern char **guestfs_int_copy_string_list (char *const *);
+extern char *guestfs_int_join_strings (const char *sep, char *const *);
extern char **guestfs_int_split_string (char sep, const char *);
extern int guestfs_int_random_string (char *ret, size_t len);
extern char *guestfs_int_drive_name (size_t index, char *ret);
diff --git a/libguestfs/guestfs-utils.c b/libguestfs/guestfs-utils.c
index 505c9f6c0a32..9f0b012d2968 100644
--- a/libguestfs/guestfs-utils.c
+++ b/libguestfs/guestfs-utils.c
@@ -77,6 +77,41 @@ guestfs_int_copy_string_list (char *const *argv)
return ret;
}
+char *
+guestfs_int_join_strings (const char *sep, char *const *argv)
+{
+ size_t i, len, seplen, rlen;
+ char *r;
+
+ seplen = strlen (sep);
+
+ len = 0;
+ for (i = 0; argv[i] != NULL; ++i) {
+ if (i > 0)
+ len += seplen;
+ len += strlen (argv[i]);
+ }
+ len++; /* for final \0 */
+
+ r = malloc (len);
+ if (r == NULL)
+ return NULL;
+
+ rlen = 0;
+ for (i = 0; argv[i] != NULL; ++i) {
+ if (i > 0) {
+ memcpy (&r[rlen], sep, seplen);
+ rlen += seplen;
+ }
+ len = strlen (argv[i]);
+ memcpy (&r[rlen], argv[i], len);
+ rlen += len;
+ }
+ r[rlen] = '\0';
+
+ return r;
+}
+
/**
* Split string at separator character C<sep>, returning the list of
* strings. Returns C<NULL> on memory allocation failure.