diff options
author | ache <ache@ache.one> | 2019-02-06 12:42:27 +0100 |
---|---|---|
committer | ache <ache@ache.one> | 2019-02-06 12:42:27 +0100 |
commit | b6b08accf59af045a2c0b27c48177a250b5f0456 (patch) | |
tree | 64943af383db65db97abce6dc2fa5b2f3f11e046 | |
parent | Add or edit contact (diff) |
Search for a config file
-rw-r--r-- | readConfig.c | 59 |
1 files 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 <stdio.h> #include <wordexp.h> #include <string.h> +#include <unistd.h> #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; } |