]> granicus.if.org Git - p11-kit/commitdiff
path: Add p11_path_prefix() function
authorStef Walter <stef@thewalter.net>
Fri, 28 Jun 2013 10:51:30 +0000 (12:51 +0200)
committerStef Walter <stef@thewalter.net>
Wed, 3 Jul 2013 08:29:22 +0000 (10:29 +0200)
Checks if a wellformed path is identical to or a prefix
of another path.

common/path.c
common/path.h
common/tests/test-path.c

index a2ba6ec3231d08c941b84f264cc76ac32088476e..8362765ac397949aa0b4f7e4462fdf84e9c97840 100644 (file)
@@ -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]);
+}
index 1fce607284b2e981e329801cc7943ced7d3326eb..cd135cb99ebce3062bf8b75fce57ece99a364a97 100644 (file)
@@ -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__ */
index ec2c2007ca39a4f37f518bdc01fb9dcb9bc08ba0..1671381fbfe38a110899841684ebc78b722c2b9c 100644 (file)
@@ -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);
 }