From 04f01366b19c6f3687dd8ff0423286a595eb9411 Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Mon, 7 Apr 2025 10:56:12 -0400 Subject: [PATCH] add function to expand tilde and expand tilde on device specific config files --- src/monocoque/helper/confighelper.c | 7 +++--- src/monocoque/helper/dirhelper.c | 36 ++++++++++++++++++++++------- src/monocoque/helper/dirhelper.h | 1 + 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index f0004aa..b6e5769 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -10,7 +10,7 @@ #include #include "confighelper.h" - +#include "dirhelper.h" #include "../slog/slog.h" #include "parameters.h" @@ -458,7 +458,7 @@ int getconfigtouse(const char* config_file_str, char* car, int sim) } -int loadtachconfig(const char* config_file, DeviceSettings* ds) +int loadtachconfig(char* config_file, DeviceSettings* ds) { @@ -605,6 +605,7 @@ static int load_device_specific_config(const char* config_file, DeviceSettings* else { ds->specific_config_file = strdup(config_file); + ds->specific_config_file = expand_tilde(ds->specific_config_file); slogt("will try to load config file at %s", ds->specific_config_file); } } @@ -617,7 +618,7 @@ static int load_device_specific_config(const char* config_file, DeviceSettings* slogw("Tachometer must have a device specific config file!"); return 1; } - return loadtachconfig(config_file, ds); + loadtachconfig(ds->specific_config_file, ds); } return 0; } diff --git a/src/monocoque/helper/dirhelper.c b/src/monocoque/helper/dirhelper.c index a052687..09f9257 100644 --- a/src/monocoque/helper/dirhelper.c +++ b/src/monocoque/helper/dirhelper.c @@ -48,19 +48,12 @@ char* gethome() char* homedir = getenv("HOME"); return homedir; - if (homedir != NULL) - { - printf("Home dir in enviroment"); - printf("%s\n", homedir); - } - uid_t uid = getuid(); struct passwd* pw = getpwuid(uid); if (pw == NULL) { - printf("Failed\n"); - exit(EXIT_FAILURE); + return NULL; } return pw->pw_dir; @@ -269,3 +262,30 @@ bool does_file_exist(const char* file) return false; #endif } + +char* expand_tilde(char* path) { + if (path[0] != '~') { + return path; + } + + const char* home_dir = getenv("HOME"); + if (!home_dir) { + return path; + } + + size_t expanded_size = strlen(home_dir) + strlen(path); + char* expanded_path = (char*)malloc(expanded_size + 1); + + if (!expanded_path) { + return path; + } + + strcpy(expanded_path, home_dir); + strcat(expanded_path, path + 1); + + if (path) { + free(path); + } + + return expanded_path; +} diff --git a/src/monocoque/helper/dirhelper.h b/src/monocoque/helper/dirhelper.h index bed7901..5abc8c1 100644 --- a/src/monocoque/helper/dirhelper.h +++ b/src/monocoque/helper/dirhelper.h @@ -12,5 +12,6 @@ bool does_directory_exist(char* path); void restrict_folders_to_cache(char* path, int cachesize); void delete_dir(char* path); bool does_file_exist(const char* file); +char* expand_tilde(char* path); #endif