[PATCH 2/7] recursive btrfs sub snapshot/delete: create pathjoin() function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The pathjoin() function creates a path from a list of strings.

Signed-off-by: Goffredo Baroncelli <kreijack@xxxxxxxxx>
---
 utils.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 utils.h |  1 +
 2 files changed, 64 insertions(+)

diff --git a/utils.c b/utils.c
index f499023..5f4d0ef 100644
--- a/utils.c
+++ b/utils.c
@@ -38,6 +38,7 @@
 #include <linux/kdev_t.h>
 #include <limits.h>
 #include <blkid/blkid.h>
+#include <stdarg.h>
 #include "kerncompat.h"
 #include "radix-tree.h"
 #include "ctree.h"
@@ -2108,3 +2109,65 @@ int lookup_ino_rootid(int fd, u64 *rootid)
 
 	return 0;
 }
+
+/*
+ * Joints a list of string. The list has to be NULL terminated
+ */
+char *pathjoin( char *s, ...)
+{
+	va_list		ap;
+	int 		size;
+	char		*dst;
+
+	if (!s || !strlen(s))
+		return NULL;
+
+	size = strlen(s)+1;
+	va_start(ap, s);
+
+	do {
+		char *p;
+
+		p = va_arg(ap, char *);
+		if (!p)
+			break;
+
+		size += strlen(p)+1;
+	} while(1);
+
+	va_end(ap);
+	size++;
+
+	dst = malloc(size);
+	if (!dst)
+		return NULL;
+
+	strcpy(dst, s);
+	va_start(ap, s);
+
+	do {
+		char *p;
+		int l, l2;
+		p = va_arg(ap, char *);
+		if (!p)
+			break;
+		if (*p == '/')
+			p++;
+
+		l2 = strlen(p);
+		if (!l2)
+			continue;
+		if (p[l2-1] == '/')
+			p[l2-1] = 0;
+
+		l = strlen(dst);
+		if (dst[l-1] != '/')
+			strcat(dst, "/");
+		strcat(dst, p);
+
+	} while(1);
+	va_end(ap);
+
+	return dst;
+
+}
\ No newline at end of file
diff --git a/utils.h b/utils.h
index 6f4b10c..d6c52a6 100644
--- a/utils.h
+++ b/utils.h
@@ -94,5 +94,6 @@ int ask_user(char *question);
 int lookup_ino_rootid(int fd, u64 *rootid);
 int btrfs_scan_lblkid(int update_kernel);
 int get_btrfs_mount(const char *dev, char *mp, size_t mp_size);
+char *pathjoin(char *, ...);
 
 #endif
-- 
1.8.4.3

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Filesystem Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux