[gnome-builder] posix: add helpers to expand/collapse user paths
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] posix: add helpers to expand/collapse user paths
- Date: Tue, 28 Jun 2016 20:49:31 +0000 (UTC)
commit 5494639f0df74b74029f03ceb3dd32482fbfbe75
Author: Christian Hergert <chergert redhat com>
Date: Tue Jun 28 13:48:20 2016 -0700
posix: add helpers to expand/collapse user paths
Generally, using ~ in paths inside the program is tedious, but there are a
few use cases where we want to support that. (Such as the path used in the
create project dialog).
This is a simple helper that uses the POSIX wordexp(3) function to expand
simple shell features. It does not, however, perform command substitution.
libide/util/ide-posix.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++
libide/util/ide-posix.h | 2 +
2 files changed, 67 insertions(+), 0 deletions(-)
---
diff --git a/libide/util/ide-posix.c b/libide/util/ide-posix.c
index ef4c9bb..5605001 100644
--- a/libide/util/ide-posix.c
+++ b/libide/util/ide-posix.c
@@ -21,6 +21,7 @@
#include <sys/user.h>
#include <sys/utsname.h>
#include <unistd.h>
+#include <wordexp.h>
#include "ide-posix.h"
@@ -44,3 +45,67 @@ ide_get_system_page_size (void)
{
return sysconf (_SC_PAGE_SIZE);
}
+
+/**
+ * ide_path_expand:
+ *
+ * This function will expand various "shell-like" features of the provided
+ * path using the POSIX wordexp(3) function. Command substitution will
+ * not be enabled, but path features such as ~user will be expanded.
+ *
+ * Returns: (transfer full): A newly allocated string containing the
+ * expansion. A copy of the input string upon failure to expand.
+ */
+gchar *
+ide_path_expand (const gchar *path)
+{
+ wordexp_t state = { 0 };
+ gchar *ret = NULL;
+ int r;
+
+ if (path == NULL)
+ return NULL;
+
+ r = wordexp (path, &state, WRDE_NOCMD);
+ if (r == 0 && state.we_wordc > 0)
+ ret = g_strdup (state.we_wordv [0]);
+ wordfree (&state);
+
+ if (!g_path_is_absolute (ret))
+ {
+ g_autofree gchar *freeme = ret;
+
+ ret = g_build_filename (g_get_home_dir (), freeme, NULL);
+ }
+
+ return ret;
+}
+
+/**
+ * ide_path_collapse:
+ *
+ * This function will collapse a path that starts with the users home
+ * directory into a shorthand notation using ~/ for the home directory.
+ *
+ * If the path does not have the home directory as a prefix, it will
+ * simply return a copy of @path.
+ *
+ * Returns: (transfer full): A new path, possibly collapsed.
+ */
+gchar *
+ide_path_collapse (const gchar *path)
+{
+ g_autofree gchar *expanded = NULL;
+
+ if (path == NULL)
+ return NULL;
+
+ expanded = ide_path_expand (path);
+
+ if (g_str_has_prefix (expanded, g_get_home_dir ()))
+ return g_build_filename ("~",
+ expanded + strlen (g_get_home_dir ()),
+ NULL);
+
+ return g_steal_pointer (&expanded);
+}
diff --git a/libide/util/ide-posix.h b/libide/util/ide-posix.h
index 438774a..b0bb05f 100644
--- a/libide/util/ide-posix.h
+++ b/libide/util/ide-posix.h
@@ -25,6 +25,8 @@ G_BEGIN_DECLS
gchar *ide_get_system_arch (void);
gsize ide_get_system_page_size (void) G_GNUC_CONST;
+gchar *ide_path_collapse (const gchar *path);
+gchar *ide_path_expand (const gchar *path);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]