synk

synchronize files between hosts
Log | Files | Refs | README | LICENSE

commit 41a5e83a69cf024c072941210d90196e213c3f9e
parent da5b2a4bb3b995389485a50167dc232c8337b740
Author: Willy <willyatmailoodotorg>
Date:   Wed Aug 31 23:46:50 +0200

New function to concatenate arrays

Diffstat:
synk.c | 39+++++++++++++++++++++++++++++++++++++++
1 file changed, 39 insertions(+), 0 deletions(-)
diff --git a/synk.c b/synk.c @@ -1,5 +1,6 @@ #include <limits.h> #include <pthread.h> +#include <stdarg.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -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); @@ -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. */ @@ -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) {