1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2025-01-16 04:02:15 +00:00

Implemented path resolution

This commit is contained in:
donutAnees 2024-07-29 11:45:22 +05:30 committed by Brenden Matthews
parent 9afcfadebb
commit 7ec86cfbd8
2 changed files with 15 additions and 9 deletions

View File

@ -38,6 +38,7 @@
#include <cerrno>
#include <ctime>
#include <vector>
#include <wordexp.h>
#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) {

View File

@ -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);