diff options
author | Richard W.M. Jones <rjones@redhat.com> | 2012-08-14 13:55:00 +0100 |
---|---|---|
committer | Richard W.M. Jones <rjones@redhat.com> | 2012-08-30 21:25:25 +0100 |
commit | 878212cb8d7eda9ec80b96695e115c14cab17664 (patch) | |
tree | 2e782c57742fd6790159e0e90d18c0e49fc33267 | |
parent | 9aad41f14548acd0b012f4b333088e62eefe8e8c (diff) | |
download | libguestfs-878212cb8d7eda9ec80b96695e115c14cab17664.tar.gz libguestfs-878212cb8d7eda9ec80b96695e115c14cab17664.tar.xz libguestfs-878212cb8d7eda9ec80b96695e115c14cab17664.zip |
daemon: New utility functions 'join_strings', 'concat_strings'.
Useful functions for concatenating strings together.
(cherry picked from commit 637b06ab1384f0c904715ba76fc759dac1b95500)
-rw-r--r-- | daemon/daemon.h | 7 | ||||
-rw-r--r-- | daemon/guestfsd.c | 41 |
2 files changed, 48 insertions, 0 deletions
diff --git a/daemon/daemon.h b/daemon/daemon.h index fa0e054d..6df2580a 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -82,6 +82,13 @@ extern void free_stringslen (char **argv, size_t len); extern void sort_device_names (char **argv, size_t len); extern int compare_device_names (const char *a, const char *b); +/* Concatenate strings, optionally with a separator string between + * each. On error, these return NULL but do NOT call reply_with_* nor + * free anything. + */ +extern char *concat_strings (char *const *argv); +extern char *join_strings (const char *separator, char *const *argv); + extern char **split_lines (char *str); #define command(out,err,name,...) commandf((out),(err),0,(name),__VA_ARGS__) diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 2b0acf96..80175e08 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -578,6 +578,47 @@ sort_device_names (char **argv, size_t len) qsort (argv, len, sizeof (char *), compare_device_names_vp); } +char * +concat_strings (char *const *argv) +{ + return join_strings ("", argv); +} + +char * +join_strings (const char *separator, char *const *argv) +{ + size_t i, len, seplen, rlen; + char *r; + + seplen = strlen (separator); + + 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], separator, seplen); + rlen += seplen; + } + len = strlen (argv[i]); + memcpy (&r[rlen], argv[i], len); + rlen += len; + } + r[rlen] = '\0'; + + return r; +} + /* Easy ways to run external commands. For full documentation, see * 'commandrvf' below. */ |