summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStef Walter <stef@thewalter.net>2013-06-28 12:51:30 +0200
committerStef Walter <stef@thewalter.net>2013-07-03 10:29:22 +0200
commit7eabbee227f09cc4ff9e472520f03bba1e35596b (patch)
treedf165a9f6de85ae0005112ce289ea17d386dfb91
parent1e777512e554db76ba2f1aba800ee09a9fa074f0 (diff)
downloadp11-kit-7eabbee227f09cc4ff9e472520f03bba1e35596b.tar.gz
path: Add p11_path_prefix() function
Checks if a wellformed path is identical to or a prefix of another path.
-rw-r--r--common/path.c17
-rw-r--r--common/path.h3
-rw-r--r--common/tests/test-path.c13
3 files changed, 33 insertions, 0 deletions
diff --git a/common/path.c b/common/path.c
index a2ba6ec..8362765 100644
--- a/common/path.c
+++ b/common/path.c
@@ -298,3 +298,20 @@ p11_path_parent (const char *path)
return_val_if_fail (parent != NULL, NULL);
return parent;
}
+
+bool
+p11_path_prefix (const char *string,
+ const char *prefix)
+{
+ int a, b;
+
+ return_val_if_fail (string != NULL, false);
+ return_val_if_fail (prefix != NULL, false);
+
+ a = strlen (string);
+ b = strlen (prefix);
+
+ return a > b &&
+ strncmp (string, prefix, b) == 0 &&
+ is_path_component_or_null (string[b]);
+}
diff --git a/common/path.h b/common/path.h
index 1fce607..cd135cb 100644
--- a/common/path.h
+++ b/common/path.h
@@ -61,4 +61,7 @@ bool p11_path_absolute (const char *path);
char * p11_path_parent (const char *path);
+bool p11_path_prefix (const char *string,
+ const char *prefix);
+
#endif /* P11_PATH_H__ */
diff --git a/common/tests/test-path.c b/common/tests/test-path.c
index ec2c200..1671381 100644
--- a/common/tests/test-path.c
+++ b/common/tests/test-path.c
@@ -189,6 +189,18 @@ test_parent (void)
assert_ptr_eq (NULL, p11_path_parent (""));
}
+static void
+test_prefix (void)
+{
+ assert (p11_path_prefix ("/test/second", "/test"));
+ assert (!p11_path_prefix ("/test", "/test"));
+ assert (!p11_path_prefix ("/different/prefix", "/test"));
+ assert (!p11_path_prefix ("/te", "/test"));
+ assert (!p11_path_prefix ("/test", "/test/blah"));
+ assert (p11_path_prefix ("/test/other/second", "/test"));
+ assert (p11_path_prefix ("/test//other//second", "/test"));
+}
+
int
main (int argc,
char *argv[])
@@ -198,6 +210,7 @@ main (int argc,
p11_test (test_expand, "/path/expand");
p11_test (test_absolute, "/path/absolute");
p11_test (test_parent, "/path/parent");
+ p11_test (test_prefix, "/path/prefix");
return p11_test_run (argc, argv);
}