summaryrefslogtreecommitdiff
path: root/common/path.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/path.c')
-rw-r--r--common/path.c60
1 files changed, 51 insertions, 9 deletions
diff --git a/common/path.c b/common/path.c
index bba2c23..a2ba6ec 100644
--- a/common/path.c
+++ b/common/path.c
@@ -97,6 +97,9 @@ expand_homedir (const char *remainder)
{
const char *env;
+ if (remainder[0] == '\0')
+ remainder = NULL;
+
env = getenv ("HOME");
if (env && env[0]) {
return p11_path_build (env, remainder, NULL);
@@ -137,6 +140,9 @@ expand_tempdir (const char *remainder)
{
const char *env;
+ if (remainder[0] == '\0')
+ remainder = NULL;
+
env = getenv ("TEMP");
if (env && env[0]) {
return p11_path_build (env, remainder, NULL);
@@ -164,10 +170,10 @@ expand_tempdir (const char *remainder)
}
}
-static bool
+static inline bool
is_path_component_or_null (char ch)
{
- return (ch == '0' || ch == '/'
+ return (ch == '\0' || ch == '/'
#ifdef OS_WIN32
|| ch == '\\'
#endif
@@ -181,15 +187,15 @@ p11_path_expand (const char *path)
if (strncmp (path, "~", 1) == 0 &&
is_path_component_or_null (path[1])) {
- return expand_homedir (path + 2);
+ return expand_homedir (path + 1);
} else if (strncmp (path, "$HOME", 5) == 0 &&
is_path_component_or_null (path[5])) {
- return expand_homedir (path + 6);
+ return expand_homedir (path + 5);
} else if (strncmp (path, "$TEMP", 5) == 0 &&
is_path_component_or_null (path[5])) {
- return expand_tempdir (path + 6);
+ return expand_tempdir (path + 5);
} else {
return strdup (path);
@@ -201,11 +207,11 @@ p11_path_absolute (const char *path)
{
return_val_if_fail (path != NULL, false);
-#ifdef OS_UNIX
- return (path[0] == '/');
-#else
- return (path[0] != '\0' && path[1] == ':' && path[2] == '\\');
+ return (path[0] == '/')
+#ifdef OS_WIN32
+ || (path[0] != '\0' && path[1] == ':' && path[2] == '\\')
#endif
+ ;
}
char *
@@ -256,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;
+}