From fb350e788bed62a945cc93f98071db9788fe3397 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Wed, 29 May 2019 15:56:12 +0200 Subject: [PATCH] Search for system-wide volk_gnsssdr_config file --- .../volk_gnsssdr/lib/volk_gnsssdr_prefs.c | 51 +++++++++++++++---- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/src/algorithms/libs/volk_gnsssdr_module/volk_gnsssdr/lib/volk_gnsssdr_prefs.c b/src/algorithms/libs/volk_gnsssdr_module/volk_gnsssdr/lib/volk_gnsssdr_prefs.c index 87a51732d..7045ee2aa 100644 --- a/src/algorithms/libs/volk_gnsssdr_module/volk_gnsssdr/lib/volk_gnsssdr_prefs.c +++ b/src/algorithms/libs/volk_gnsssdr_module/volk_gnsssdr/lib/volk_gnsssdr_prefs.c @@ -19,6 +19,13 @@ #include #include #include +#if defined(_MSC_VER) +#include +#define access _access +#define F_OK 0 +#else +#include +#endif #include @@ -29,7 +36,7 @@ void volk_gnsssdr_get_config_path(char *path) const char *suffix2 = "/volk_gnsssdr/volk_gnsssdr_config"; // non-hidden char *home = NULL; - //allows config redirection via env variable + // allows config redirection via env variable home = getenv("VOLK_CONFIGPATH"); if (home != NULL) { @@ -38,17 +45,43 @@ void volk_gnsssdr_get_config_path(char *path) return; } - if (home == NULL) home = getenv("HOME"); - if (home == NULL) home = getenv("APPDATA"); - if (home == NULL) + // check for user-local config file + home = getenv("HOME"); + if (home != NULL) { - path[0] = 0; + strncpy(path, home, 512); + strcat(path, suffix); + if (access(path, F_OK) != -1) + { + return; + } + } + + // check for config file in APPDATA (Windows) + home = getenv("APPDATA"); + if (home != NULL) + { + strncpy(path, home, 512); + strcat(path, suffix); + if (access(path, F_OK) != -1) + { + return; + } + } + + // check for system-wide config file + if (access("/etc/volk_gnsssdr/volk_gnsssdr_config", F_OK) != -1) + { + strncpy(path, "/etc", 512); + strcat(path, suffix2); return; } - strncpy(path, home, 512); - strcat(path, suffix); + + path[0] = 0; + return; } + size_t volk_gnsssdr_load_preferences(volk_gnsssdr_arch_pref_t **prefs_res) { FILE *config_file; @@ -56,13 +89,13 @@ size_t volk_gnsssdr_load_preferences(volk_gnsssdr_arch_pref_t **prefs_res) size_t n_arch_prefs = 0; volk_gnsssdr_arch_pref_t *prefs = NULL; - //get the config path + // get the config path volk_gnsssdr_get_config_path(path); if (!path[0]) return n_arch_prefs; //no prefs found config_file = fopen(path, "r"); if (!config_file) return n_arch_prefs; //no prefs found - //reset the file pointer and write the prefs into volk_gnsssdr_arch_prefs + // reset the file pointer and write the prefs into volk_gnsssdr_arch_prefs while (fgets(line, sizeof(line), config_file) != NULL) { prefs = (volk_gnsssdr_arch_pref_t *)realloc(prefs, (n_arch_prefs + 1) * sizeof(*prefs));