[PATCH 6/9 v3 part 1/2] util: add strcmp_null, a strcmp that handles NULL parameters

Jani Nikula jani at nikula.org
Fri Sep 25 09:48:19 PDT 2015


Add strcmp_null, a strcmp that handles NULL strings; in strcmp terms a
NULL string is considered to be less than a non-NULL string.
---
 util/string-util.c | 13 +++++++++++++
 util/string-util.h |  5 +++++
 2 files changed, 18 insertions(+)

diff --git a/util/string-util.c b/util/string-util.c
index 76c0b9025d0f..92af937f45ec 100644
--- a/util/string-util.c
+++ b/util/string-util.c
@@ -223,6 +223,19 @@ parse_boolean_term (void *ctx, const char *str,
 }
 
 int
+strcmp_null (const char *s1, const char *s2)
+{
+    if (s1 && s2)
+	return strcmp (s1, s2);
+    else if (! s1 && ! s2)
+	return 0;
+    else if (s1)
+	return 1;	/* s1 (non-NULL) is greater than s2 (NULL) */
+    else
+	return -1;	/* s1 (NULL) is less than s2 (non-NULL) */
+}
+
+int
 strcase_equal (const void *a, const void *b)
 {
     return strcasecmp (a, b) == 0;
diff --git a/util/string-util.h b/util/string-util.h
index 80d24d1c1053..87917b8fd279 100644
--- a/util/string-util.h
+++ b/util/string-util.h
@@ -64,6 +64,11 @@ int
 parse_boolean_term (void *ctx, const char *str,
 		    char **prefix_out, char **term_out);
 
+/* strcmp that handles NULL strings; in strcmp terms a NULL string is
+ * considered to be less than a non-NULL string.
+ */
+int strcmp_null (const char *s1, const char *s2);
+
 /* GLib GEqualFunc compatible strcasecmp wrapper */
 int strcase_equal (const void *a, const void *b);
 
-- 
2.1.4



More information about the notmuch mailing list