Skip to content

Commit 7ec86cf

Browse files
donutAneesbrndnmtthws
authored andcommitted
Implemented path resolution
1 parent 9afcfad commit 7ec86cf

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/common.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <cerrno>
3939
#include <ctime>
4040
#include <vector>
41+
#include <wordexp.h>
4142
#include "config.h"
4243
#include "conky.h"
4344
#include "core.h"
@@ -130,13 +131,19 @@ double get_time() {
130131
}
131132

132133
/* Converts '~/...' paths to '/home/blah/...'. It's similar to
133-
* variable_substitute, except only cheques for $HOME and ~/ in
134-
* path. If HOME is unset it uses an empty string for substitution */
134+
* variable_substitute, works for any enviroment variable */
135135
std::string to_real_path(const std::string &source) {
136-
const char *homedir = getenv("HOME") != nullptr ? getenv("HOME") : "";
137-
if (source.find("~/") == 0) { return homedir + source.substr(1); }
138-
if (source.find("$HOME/") == 0) { return homedir + source.substr(5); }
139-
return source;
136+
wordexp_t p;
137+
char **w;
138+
int i;
139+
const char *csource = source.c_str();
140+
if (wordexp(csource, &p, 0) != 0) {
141+
return nullptr;
142+
}
143+
w = p.we_wordv;
144+
const char *resolved_path = strdup(w[0]);
145+
wordfree(&p);
146+
return std::string(resolved_path);
140147
}
141148

142149
int open_fifo(const char *file, int *reported) {

src/common.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ struct process *get_first_process(void);
5858
void get_cpu_count(void);
5959
double get_time(void);
6060

61-
/* Converts '~/...' paths to '/home/blah/...'
62-
* It's similar to variable_substitute, except only cheques for $HOME and ~/ in
63-
* path */
61+
/* Converts '~/...' paths to '/home/blah/...'. It's similar to
62+
* variable_substitute, works for any enviroment variable */
6463
std::string to_real_path(const std::string &source);
6564
FILE *open_file(const char *file, int *reported);
6665
int open_fifo(const char *file, int *reported);

0 commit comments

Comments
 (0)