From 1b30f4c8e521b3d54803a50f582b7f7cb540e38d Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Mon, 30 Nov 2009 00:03:03 +0200 Subject: [PATCH] * mu-util: use wordexp for shell-like expansion --- src/mu-util.c | 28 +++++++++++++++++----------- src/mu-util.h | 6 ++---- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/mu-util.c b/src/mu-util.c index 230b1294..5467aa5b 100644 --- a/src/mu-util.c +++ b/src/mu-util.c @@ -17,30 +17,36 @@ ** */ +#define _XOPEN_SOURCE +#include /* for shell-style globbing */ + #include #include #include "mu-util.h" char* -mu_util_homedir_expand (const char *path) +mu_util_dir_expand (const char *path) { - const char* home; + wordexp_t wexp; + char *dir; - if (!path) - return NULL; - - if (path[0] != '~' || path[1] != G_DIR_SEPARATOR) - return g_strdup (path); + g_return_val_if_fail (path, NULL); - home = getenv ("HOME"); - if (!home) - home = g_get_home_dir (); + dir = NULL; + wordexp (path, &wexp, 0); + if (wexp.we_wordc != 1) + g_printerr ("error expanding dir '%s'", path); + else + dir = g_strdup (wexp.we_wordv[0]); - return g_strdup_printf ("%s%s", home, path + 1); + wordfree (&wexp); + + return dir; } + GSList * mu_util_strlist_from_args (int argc, char *argv[]) { diff --git a/src/mu-util.h b/src/mu-util.h index 81b2b906..9c74406c 100644 --- a/src/mu-util.h +++ b/src/mu-util.h @@ -25,16 +25,14 @@ G_BEGIN_DECLS /** - * get the expanded path; currently only a starting '~/' will be - * replaced by the users home directory, ie. for a user 'bob' this could mean - * something like "~/my/path/to/something" --> "/home/bob/my/path/to/something" + * get the expanded path; ie. perform shell expansion on the path * * @param path path to expand * * @return the expanded path as a newly allocated string, or NULL in * case of error */ -char* mu_util_homedir_expand (const char* path); +char* mu_util_dir_expand (const char* path); /**