From d0af224e6da2505601b9c068912903b3d32e37c6 Mon Sep 17 00:00:00 2001 From: "S. Christoffer Eliesen" Date: Thu, 19 Nov 2015 11:52:58 +0100 Subject: [PATCH] stringop: lenient_strcmp: Add. --- include/stringop.h | 3 +++ sway/stringop.c | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/stringop.h b/include/stringop.h index febbbaba5..bb681bcdb 100644 --- a/include/stringop.h +++ b/include/stringop.h @@ -14,6 +14,9 @@ char *strip_whitespace(char *str); char *strip_comments(char *str); void strip_quotes(char *str); +// strcmp that also handles null pointers. +int lenient_strcmp(char *a, char *b); + // Simply split a string with delims, free with `free_flat_list` list_t *split_string(const char *str, const char *delims); void free_flat_list(list_t *list); diff --git a/sway/stringop.c b/sway/stringop.c index 8d6cac2fd..fe5a97ca1 100644 --- a/sway/stringop.c +++ b/sway/stringop.c @@ -74,6 +74,19 @@ void strip_quotes(char *str) { *end = '\0'; } +// strcmp that also handles null pointers. +int lenient_strcmp(char *a, char *b) { + if (a == b) { + return 0; + } else if (!a) { + return -1; + } else if (!b) { + return 1; + } else { + return strcmp(a, b); + } +} + list_t *split_string(const char *str, const char *delims) { list_t *res = create_list(); char *copy = strdup(str);