add function to expand tilde and expand tilde on device specific config files

This commit is contained in:
Paul Dino Jones 2025-04-07 10:56:12 -04:00
parent e627392dda
commit 04f01366b1
3 changed files with 33 additions and 11 deletions

View File

@ -10,7 +10,7 @@
#include <libxml/tree.h> #include <libxml/tree.h>
#include "confighelper.h" #include "confighelper.h"
#include "dirhelper.h"
#include "../slog/slog.h" #include "../slog/slog.h"
#include "parameters.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 else
{ {
ds->specific_config_file = strdup(config_file); 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); 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!"); slogw("Tachometer must have a device specific config file!");
return 1; return 1;
} }
return loadtachconfig(config_file, ds); loadtachconfig(ds->specific_config_file, ds);
} }
return 0; return 0;
} }

View File

@ -48,19 +48,12 @@ char* gethome()
char* homedir = getenv("HOME"); char* homedir = getenv("HOME");
return homedir; return homedir;
if (homedir != NULL)
{
printf("Home dir in enviroment");
printf("%s\n", homedir);
}
uid_t uid = getuid(); uid_t uid = getuid();
struct passwd* pw = getpwuid(uid); struct passwd* pw = getpwuid(uid);
if (pw == NULL) if (pw == NULL)
{ {
printf("Failed\n"); return NULL;
exit(EXIT_FAILURE);
} }
return pw->pw_dir; return pw->pw_dir;
@ -269,3 +262,30 @@ bool does_file_exist(const char* file)
return false; return false;
#endif #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;
}

View File

@ -12,5 +12,6 @@ bool does_directory_exist(char* path);
void restrict_folders_to_cache(char* path, int cachesize); void restrict_folders_to_cache(char* path, int cachesize);
void delete_dir(char* path); void delete_dir(char* path);
bool does_file_exist(const char* file); bool does_file_exist(const char* file);
char* expand_tilde(char* path);
#endif #endif