built[at] = '\0';
return built;
}
+
+char *
+p11_path_parent (const char *path)
+{
+ const char *e;
+ char *parent;
+ bool had = false;
+
+ return_val_if_fail (path != NULL, NULL);
+
+ /* Find the end of the last component */
+ e = path + strlen (path);
+ while (e != path && is_path_component_or_null (*e))
+ e--;
+
+ /* Find the beginning of the last component */
+ while (e != path && !is_path_component_or_null (*e)) {
+ had = true;
+ e--;
+ }
+
+ /* Find the end of the last component */
+ while (e != path && is_path_component_or_null (*e))
+ e--;
+
+ if (e == path) {
+ if (!had)
+ return NULL;
+ parent = strdup ("/");
+ } else {
+ parent = strndup (path, (e - path) + 1);
+ }
+
+ return_val_if_fail (parent != NULL, NULL);
+ return parent;
+}
#endif
}
+static void
+test_parent (void)
+{
+ check_equals_and_free ("/", p11_path_parent ("/root"));
+ check_equals_and_free ("/", p11_path_parent ("/root/"));
+ check_equals_and_free ("/", p11_path_parent ("/root//"));
+ check_equals_and_free ("/root", p11_path_parent ("/root/second"));
+ check_equals_and_free ("/root", p11_path_parent ("/root//second"));
+ check_equals_and_free ("/root", p11_path_parent ("/root//second//"));
+ check_equals_and_free ("/root", p11_path_parent ("/root///second"));
+ check_equals_and_free ("/root/second", p11_path_parent ("/root/second/test.file"));
+ assert_ptr_eq (NULL, p11_path_parent ("/"));
+ assert_ptr_eq (NULL, p11_path_parent ("//"));
+ assert_ptr_eq (NULL, p11_path_parent (""));
+}
+
int
main (int argc,
char *argv[])
p11_test (test_build, "/path/build");
p11_test (test_expand, "/path/expand");
p11_test (test_absolute, "/path/absolute");
+ p11_test (test_parent, "/path/parent");
return p11_test_run (argc, argv);
}