tNew function to concatenate arrays - synk - synchronize files between hosts
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit 41a5e83a69cf024c072941210d90196e213c3f9e
DIR parent da5b2a4bb3b995389485a50167dc232c8337b740
HTML Author: Willy <willyatmailoodotorg>
Date: Wed, 31 Aug 2016 23:46:50 +0200
New function to concatenate arrays
Diffstat:
M synk.c | 39 +++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+), 0 deletions(-)
---
DIR diff --git a/synk.c b/synk.c
t@@ -1,5 +1,6 @@
#include <limits.h>
#include <pthread.h>
+#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
t@@ -52,6 +53,7 @@ void *sendmetadata(void *arg);
int serverloop(in_addr_t, in_port_t);
char *echo(char * []);
+char **concat(int, ...);
struct peer_t *addpeer(struct peers_t *, in_addr_t, in_port_t);
long gettimestamp(const char *path);
t@@ -96,6 +98,42 @@ echo(char *args[])
}
/*
+ * Take a variable number of arrays, and concatenate them in a single array.
+ * The first argument is the number of arrays passed
+ * All arrays should be NULL terminated, or undefined behavior may occur.
+ */
+char **
+concat(int n, ...)
+{
+ size_t i, len = 0;
+ va_list args;
+ char **p, **tmp, **cat = { NULL };
+
+ va_start(args, n);
+ while (n --> 0) {
+ p = va_arg(args, char * []);
+
+ /* count args in the given array */
+ for (i=0; p[i]; ++i);
+
+ /* leave room for a trailing NULL arg if we're at the last array */
+ i += n ? 0 : 1;
+
+ cat = realloc(cat, (len + i) * sizeof(char *));
+ if (!cat) {
+ perror("realloc");
+ va_end(args);
+ return NULL;
+ }
+ memcpy(cat + len, p, i*sizeof(char *));
+ len += i;
+ }
+
+ va_end(args);
+ return cat;
+}
+
+/*
* Returns the UNIX timestamp for the given file, or -1 in case stat(2)
* is in error.
*/
t@@ -426,6 +464,7 @@ main(int argc, char *argv[])
case 's': mode = SYNK_SERVER; break;
}ARGEND;
+
switch(mode) {
case SYNK_CLIENT:
while ((fn = *(argv++)) != NULL) {