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>
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
#include <errno.h>
#include <sys/stat.h>
#include <string.h>
+#include <assert.h>
#include "c.h"
#include "nls.h"
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
#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 {
#include "linux_version.h"
#include "mountP.h"
+#include "strutils.h"
/*
* Kernel supports only one MS_PROPAGATION flag change by one mount(2) syscall,
#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);
* mount/mount.h.
*/
#include "mountP.h"
+#include "strutils.h"
/*
* fs-independent mount flags (built-in MNT_LINUX_MAP)
#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;