summaryrefslogtreecommitdiff
path: root/common/path.c
diff options
context:
space:
mode:
authorStef Walter <stef@thewalter.net>2013-06-14 12:53:15 +0200
committerStef Walter <stef@thewalter.net>2013-06-14 12:59:29 +0200
commit045df29606ea9853b4fc8bdba062a5e4a7a5be95 (patch)
treeef19ba4bb04471cf3c42d6bd3d1f4c11a1d4d754 /common/path.c
parent8c6dd48789bdaf2a3dc800df7ed3416ddc3b7e1f (diff)
path: Add p11_path_parent() function
Gets the parent element of the path, removing the last component. Handles trailing and duplicate path separators correctly.
Diffstat (limited to 'common/path.c')
-rw-r--r--common/path.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/common/path.c b/common/path.c
index d3d881d..a2ba6ec 100644
--- a/common/path.c
+++ b/common/path.c
@@ -262,3 +262,39 @@ p11_path_build (const char *path,
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;
+}