[PATCH] config: Expand ~ to $HOME

Subject: [PATCH] config: Expand ~ to $HOME

Date: Sun, 8 May 2016 17:49:34 +0200

To: notmuch@notmuchmail.org

Cc:

From: Bijan Chokoufe Nejad


Very useful in case you want to keep your .notmuch-config synchronized across
machines where you have different user names.
---
Not sure this is completely plattform independent.
I also don't know how to implement a unit test for this.
---
 notmuch-config.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/notmuch-config.c b/notmuch-config.c
index d252bb2..c9f26ef 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -605,7 +605,16 @@ _config_set_list (notmuch_config_t *config,
 const char *
 notmuch_config_get_database_path (notmuch_config_t *config)
 {
-    return _config_get (config, &config->database_path, "database", "path");
+    const char* path = _config_get (config, &config->database_path, "database", "path");
+    if (path != NULL && path[0] == '~') {
+        char *home_path = getenv("HOME");
+        char *shortened_path = malloc( sizeof(char) * ( strlen (path) - 1 ) );
+        strncpy(shortened_path, path + 2, strlen(path));
+        return talloc_asprintf (NULL, "%s/%s", home_path, shortened_path);
+    }
+    else {
+        return path;
+    }
 }
 
 void
-- 
1.9.1


Thread: