[kvm-unit-tests PATCH v4 05/13] lib/util: add args_parse_keyval

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

 



The function parses command line arguments in "key=val" format, and
treats "key" as "key=1".

Signed-off-by: Radim Krčmář <rkrcmar@xxxxxxxxxx>
---
 v4: new

 lib/util.c | 17 +++++++++++++++++
 lib/util.h | 10 ++++++++++
 2 files changed, 27 insertions(+)

diff --git a/lib/util.c b/lib/util.c
index 69b18100c972..8b33d474f4c0 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -16,3 +16,20 @@ int parse_keyval(char *s, long *val)
 	*val = atol(p+1);
 	return p - s;
 }
+
+long args_parse_keyval(int argc, char **argv, char *key)
+{
+	int i;
+	size_t keylen = strlen(key);
+
+	for (i = 1; i < argc; i++) {
+		if (keylen > 0 && strncmp(argv[i], key, keylen - 1))
+			continue;
+		if (argv[i][keylen] == '\0')
+			return 1;
+		if (argv[i][keylen] == '=')
+			return atol(argv[i] + keylen + 1);
+	}
+
+	return 0;
+}
diff --git a/lib/util.h b/lib/util.h
index 4c4b44132277..d475b058526b 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -20,4 +20,14 @@
  */
 extern int parse_keyval(char *s, long *val);
 
+/*
+ * argc and argv are standard C arguments to main().
+ * args_parse_keyval looks for an element of argv that matches the key.
+ * Returns val interpreted as a long if the element is in key=val format.
+ * Returns 1 if the element is key.
+ * Returns 0 otherwise.
+ *
+ */
+long args_parse_keyval(int argc, char **argv, char *key);
+
 #endif
-- 
2.8.2

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



[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux