diff --git a/src/common.cc b/src/common.cc index 53a9fe7c..4a270ce0 100644 --- a/src/common.cc +++ b/src/common.cc @@ -38,6 +38,7 @@ #include #include #include +#include #include "config.h" #include "conky.h" #include "core.h" @@ -130,13 +131,19 @@ double get_time() { } /* Converts '~/...' paths to '/home/blah/...'. It's similar to - * variable_substitute, except only cheques for $HOME and ~/ in - * path. If HOME is unset it uses an empty string for substitution */ + * variable_substitute, works for any enviroment variable */ std::string to_real_path(const std::string &source) { - const char *homedir = getenv("HOME") != nullptr ? getenv("HOME") : ""; - if (source.find("~/") == 0) { return homedir + source.substr(1); } - if (source.find("$HOME/") == 0) { return homedir + source.substr(5); } - return source; + wordexp_t p; + char **w; + int i; + const char *csource = source.c_str(); + if (wordexp(csource, &p, 0) != 0) { + return nullptr; + } + w = p.we_wordv; + const char *resolved_path = strdup(w[0]); + wordfree(&p); + return std::string(resolved_path); } int open_fifo(const char *file, int *reported) { diff --git a/src/common.h b/src/common.h index b637d0b6..b30f69d0 100644 --- a/src/common.h +++ b/src/common.h @@ -58,9 +58,8 @@ struct process *get_first_process(void); void get_cpu_count(void); double get_time(void); -/* Converts '~/...' paths to '/home/blah/...' - * It's similar to variable_substitute, except only cheques for $HOME and ~/ in - * path */ +/* Converts '~/...' paths to '/home/blah/...'. It's similar to + * variable_substitute, works for any enviroment variable */ std::string to_real_path(const std::string &source); FILE *open_file(const char *file, int *reported); int open_fifo(const char *file, int *reported);