]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/strutils: move *swith() functions to private library
authorSami Kerola <kerolasa@iki.fi>
Thu, 29 Aug 2013 14:50:17 +0000 (15:50 +0100)
committerSami Kerola <kerolasa@iki.fi>
Thu, 29 Aug 2013 17:14:08 +0000 (18:14 +0100)
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>
include/strutils.h
lib/strutils.c
lib/time-util.c
libmount/src/context_mount.c
libmount/src/mountP.h
libmount/src/optmap.c
libmount/src/utils.c

index 709fcadc5421a6a989427a316ebbc7daea0c62ba..8d8a6e46cfd84c3897c5649132fe73b7457267bb 100644 (file)
@@ -102,4 +102,8 @@ extern int parse_range(const char *str, int *lower, int *upper, int def);
 
 extern int streq_except_trailing_slash(const char *s1, const char *s2);
 
+extern char *startswith(const char *s, const char *prefix);
+extern char *startswith_no_case(const char *s, const char *prefix);
+extern char *endswith(const char *s, const char *postfix);
+
 #endif
index c263b86b27ba9d7ae8a1912a2b4bd0398ce9579d..95ab5a87eef59a8d008ba64a71d0173b26009b77 100644 (file)
@@ -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
 
index c8fcc2a6aa4a038914668bde8464c0acebc90b5e..a0b27bf52f8e0dc24ee5e9b7ff52f0634b384d62 100644 (file)
 
 #define streq(a,b) (strcmp((a),(b)) == 0)
 
-static 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++;
-       }
-}
-
-static 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++;
-       }
-}
-
-static 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;
-}
-
 static int parse_sec(const char *t, usec_t *usec)
 {
         static const struct {
index 94db1acdaa74264a332187988f021e04ae208272..4f376e4dd8f090826c4974b6621c11d675e53e7d 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "linux_version.h"
 #include "mountP.h"
+#include "strutils.h"
 
 /*
  * Kernel supports only one MS_PROPAGATION flag change by one mount(2) syscall,
index 9e6f4bd2828526202f0a913effeb32a0b026430d..9362c0042e5f35029a2cfb1eae02a14677c02510 100644 (file)
@@ -137,11 +137,6 @@ extern int mnt_run_test(struct libmnt_test *tests, int argc, char *argv[]);
 #endif
 
 /* utils.c */
-extern int endswith(const char *s, const char *sx)
-                       __attribute__((nonnull));
-extern int startswith(const char *s, const char *sx)
-                       __attribute__((nonnull));
-
 extern char *stripoff_last_component(char *path);
 
 extern int mnt_valid_tagname(const char *tagname);
index 504a5cf8545aae2b8cd423e3b761f0aa76c6a876..5b25b8f292e50e5502f31836ca06f94beeb8fd14 100644 (file)
@@ -58,6 +58,7 @@
  * mount/mount.h.
  */
 #include "mountP.h"
+#include "strutils.h"
 
 /*
  * fs-independent mount flags (built-in MNT_LINUX_MAP)
index 4e6a13148140084260b514e30ef4f78d811e950f..9f992412f42ddfeda0ccfe20f7c87c16021ad668 100644 (file)
 #include "env.h"
 #include "match.h"
 
-int endswith(const char *s, const char *sx)
-{
-       ssize_t off;
-
-       assert(s);
-       assert(sx);
-
-       off = strlen(s);
-       if (!off)
-               return 0;
-       off -= strlen(sx);
-       if (off < 0)
-               return 0;
-
-        return !strcmp(s + off, sx);
-}
-
-int startswith(const char *s, const char *sx)
-{
-       size_t off;
-
-       assert(s);
-       assert(sx);
-
-       off = strlen(sx);
-       if (!off)
-               return 0;
-
-        return !strncmp(s, sx, off);
-}
-
 int append_string(char **a, const char *b)
 {
        size_t al, bl;