Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented path resolution #2003

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <cerrno>
#include <ctime>
#include <vector>
#include <wordexp.h>
#include "config.h"
#include "conky.h"
#include "core.h"
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading