diff options
author | cbdev <cb@cbcdn.com> | 2019-11-07 18:44:19 +0100 |
---|---|---|
committer | cbdev <cb@cbcdn.com> | 2019-11-07 18:44:19 +0100 |
commit | 350f0d2d2eaff5f0d57b09857102e2df1e96d733 (patch) | |
tree | fe81a48535d700195034e9173018c9a9a63d02d0 /config.c | |
parent | 6c75f07260639fd2bc6d328d5f00c72ab4382fa8 (diff) | |
download | midimonster-350f0d2d2eaff5f0d57b09857102e2df1e96d733.tar.gz midimonster-350f0d2d2eaff5f0d57b09857102e2df1e96d733.tar.bz2 midimonster-350f0d2d2eaff5f0d57b09857102e2df1e96d733.zip |
Makefile install target and packaging instructions (Fixes #28)
Diffstat (limited to 'config.c')
-rw-r--r-- | config.c | 38 |
1 files changed, 35 insertions, 3 deletions
@@ -1,5 +1,7 @@ #include <string.h> #include <ctype.h> +#include <unistd.h> +#include <errno.h> #include "midimonster.h" #include "config.h" #include "backend.h" @@ -310,16 +312,45 @@ done: return rv; } -int config_read(char* cfg_file){ +int config_read(char* cfg_filepath){ int rv = 1; size_t line_alloc = 0; ssize_t status; map_type mapping_type = map_rtl; char* line_raw = NULL, *line, *separator; - FILE* source = fopen(cfg_file, "r"); + + //create heap copy of file name because original might be in readonly memory + char* source_dir = strdup(cfg_filepath), *source_file = NULL; + #ifdef _WIN32 + char path_separator = '\\'; + #else + char path_separator = '/'; + #endif + + if(!source_dir){ + fprintf(stderr, "Failed to allocate memory\n"); + return 1; + } + + //change working directory to the one containing the configuration file so relative paths work as expected + source_file = strrchr(source_dir, path_separator); + if(source_file){ + *source_file = 0; + source_file++; + if(chdir(source_dir)){ + fprintf(stderr, "Failed to change to configuration file directory %s: %s\n", source_dir, strerror(errno)); + goto bail; + } + } + else{ + source_file = source_dir; + } + + FILE* source = fopen(source_file, "r"); + if(!source){ fprintf(stderr, "Failed to open configuration file for reading\n"); - return 1; + goto bail; } for(status = getline(&line_raw, &line_alloc, source); status >= 0; status = getline(&line_raw, &line_alloc, source)){ @@ -459,6 +490,7 @@ int config_read(char* cfg_file){ rv = 0; bail: + free(source_dir); fclose(source); free(line_raw); return rv; |