summaryrefslogtreecommitdiffstats
path: root/lib/strutils.c
diff options
context:
space:
mode:
authorSami Kerola2013-08-29 16:50:17 +0200
committerSami Kerola2013-08-29 19:14:08 +0200
commit199e939d88333162f440ea50b83415dac625c89c (patch)
tree33b50feb3b2a065311e3c1477971e728e1fffccc /lib/strutils.c
parentlib/time-util: copy time parsing functions from systemd (diff)
downloadkernel-qcow2-util-linux-199e939d88333162f440ea50b83415dac625c89c.tar.gz
kernel-qcow2-util-linux-199e939d88333162f440ea50b83415dac625c89c.tar.xz
kernel-qcow2-util-linux-199e939d88333162f440ea50b83415dac625c89c.zip
lib/strutils: move *swith() functions to private library
Avoid code dublication in libmount and time-util. Proposed-by: Karel Zak <kzak@redhat.com> Reference: http://markmail.org/message/h7zexvqsieqngtmx Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'lib/strutils.c')
-rw-r--r--lib/strutils.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/strutils.c b/lib/strutils.c
index c263b86b2..95ab5a87e 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -10,6 +10,7 @@
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
+#include <assert.h>
#include "c.h"
#include "nls.h"
@@ -685,6 +686,72 @@ int streq_except_trailing_slash(const char *s1, const char *s2)
return equal;
}
+/*
+ * Match string beginning.
+ */
+char *startswith(const char *s, const char *prefix)
+{
+ const char *a, *b;
+
+ assert(s);
+ assert(prefix);
+
+ a = s, b = prefix;
+ for (;;) {
+ if (*b == 0)
+ return (char *)a;
+ if (*a != *b)
+ return NULL;
+
+ a++, b++;
+ }
+}
+
+/*
+ * Case insensitive match string beginning.
+ */
+char *startswith_no_case(const char *s, const char *prefix)
+{
+ const char *a, *b;
+
+ assert(s);
+ assert(prefix);
+
+ a = s, b = prefix;
+ for (;;) {
+ if (*b == 0)
+ return (char *)a;
+ if (tolower(*a) != tolower(*b))
+ return NULL;
+
+ a++, b++;
+ }
+}
+
+/*
+ * Match string ending.
+ */
+char *endswith(const char *s, const char *postfix)
+{
+ size_t sl, pl;
+
+ assert(s);
+ assert(postfix);
+
+ sl = strlen(s);
+ pl = strlen(postfix);
+
+ if (pl == 0)
+ return (char *)s + sl;
+
+ if (sl < pl)
+ return NULL;
+
+ if (memcmp(s + sl - pl, postfix, pl) != 0)
+ return NULL;
+
+ return (char *)s + sl - pl;
+}
#ifdef TEST_PROGRAM