From: Karel Zak Date: Thu, 15 Jul 2010 14:00:42 +0000 (+0200) Subject: libmount: add {start,end}swith() functions X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b49103ed47580ecc94969d00ff4ebd5101fa53fd;p=util-linux libmount: add {start,end}swith() functions Signed-off-by: Karel Zak --- diff --git a/shlibs/mount/src/Makefile.am b/shlibs/mount/src/Makefile.am index 0494a7a0..37a5177e 100644 --- a/shlibs/mount/src/Makefile.am +++ b/shlibs/mount/src/Makefile.am @@ -16,7 +16,8 @@ libmount_la_SOURCES = mountP.h version.c utils.c test.c init.c cache.c \ $(top_srcdir)/lib/at.c \ $(top_srcdir)/include/list.h \ $(top_srcdir)/lib/mangle.c \ - $(top_srcdir)/lib/canonicalize.c + $(top_srcdir)/lib/canonicalize.c \ + $(top_srcdir)/lib/strutils.c nodist_libmount_la_SOURCES = mountP.h diff --git a/shlibs/mount/src/mountP.h b/shlibs/mount/src/mountP.h index 1de13917..04876a0b 100644 --- a/shlibs/mount/src/mountP.h +++ b/shlibs/mount/src/mountP.h @@ -67,6 +67,9 @@ extern int mnt_run_test(struct mtest *tests, int argc, char *argv[]); /* utils.c */ extern char *mnt_getenv_safe(const char *arg); +extern int endswith(const char *s, const char *sx); +extern int startswith(const char *s, const char *sx); + extern char *mnt_get_username(const uid_t uid); extern int mnt_has_regular_mtab(void); diff --git a/shlibs/mount/src/utils.c b/shlibs/mount/src/utils.c index 67b00c31..eb183ee0 100644 --- a/shlibs/mount/src/utils.c +++ b/shlibs/mount/src/utils.c @@ -28,6 +28,7 @@ #include #include +#include "strutils.h" #include "pathnames.h" #include "mountP.h" @@ -52,6 +53,37 @@ char *mnt_getenv_safe(const char *arg) #endif } +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); +} + /** * mnt_fstype_is_pseudofs: * @type: filesystem name @@ -318,12 +350,32 @@ int test_match_options(struct mtest *ts, int argc, char *argv[]) return 0; } +int test_startswith(struct mtest *ts, int argc, char *argv[]) +{ + char *optstr = argv[1]; + char *pattern = argv[2]; + + printf("%s\n", startswith(optstr, pattern) ? "YES" : "NOT"); + return 0; +} + +int test_endswith(struct mtest *ts, int argc, char *argv[]) +{ + char *optstr = argv[1]; + char *pattern = argv[2]; + + printf("%s\n", endswith(optstr, pattern) ? "YES" : "NOT"); + return 0; +} + int main(int argc, char *argv[]) { struct mtest tss[] = { { "--match-fstype", test_match_fstype, " FS types matching" }, { "--match-options", test_match_options, " options matching" }, + { "--starts-with", test_startswith, " " }, + { "--ends-with", test_endswith, " " }, { NULL } };