From b6b08accf59af045a2c0b27c48177a250b5f0456 Mon Sep 17 00:00:00 2001 From: ache Date: Wed, 6 Feb 2019 12:42:27 +0100 Subject: Search for a config file --- readConfig.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/readConfig.c b/readConfig.c index 5c09957..d32aa5b 100644 --- a/readConfig.c +++ b/readConfig.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "config.h" config_t config = { .SMS_INBOX = "", @@ -16,18 +17,64 @@ int searchConfigFile(char* configFile) { // Search a config file. // First then in the home, then in /etc and finaly in /var. // In debug mode, this function search in ./fixtures + + const char* paths[] = {"/etc/mesmsrc", "/var/spool/sms/mesmsrc"}; + FILE* f_canRead = NULL; + char path[256] = ""; - strcpy(configFile, "/home/ache/.mesmsrc"); - return 0; -} -int readConfigFile(void) { - char fileName[256] = ""; +#ifdef DEBUG + // If debug, local dir first + getcwd(path, 256); + strcat(path, "/mesmsrc"); + + f_canRead = fopen(path, "r"); + if( f_canRead ) { + fclose(f_canRead); + strcpy(configFile, path); + return 0; + } + + getcwd(path, 256); + strcat(path, "/.mesmsrc"); + + f_canRead = fopen(path, "r"); + if( f_canRead ) { + fclose(f_canRead); + strcpy(configFile, path); + return 0; + } + +#endif + + // Home first + sprintf(path, "%s/.mesmsrc", getenv("HOME")); + + f_canRead = fopen(path, "r"); + if( f_canRead ) { + fclose(f_canRead); + strcpy(configFile, path); + return 0; + } + + for( int i = 0 ; i < sizeof paths / sizeof *paths ; i++) { + f_canRead = fopen(paths[i], "r"); + if( f_canRead ) { + fclose(f_canRead); + strcpy(configFile, paths[i]); + return 0; + } + } #ifdef DEBUG - fprintf(stderr, "ReadConfig\n"); + fprintf(stderr, "No config file found\n"); #endif + return 1; +} +int readConfigFile(void) { + char fileName[256] = ""; + if( searchConfigFile(fileName) > 0 ) { return 1; } -- cgit v1.2.3