| 1 |
/** |
| 2 |
* This file is part of the Detox package. |
| 3 |
* |
| 4 |
* Copyright (c) Doug Harple <[email protected]> |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdlib.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include "detox_struct.h" |
| 15 |
|
| 16 |
#include "config_file.h" |
| 17 |
#include "config_file_spoof.h" |
| 18 |
#include "wrapped.h" |
| 19 |
|
| 20 |
config_file_t *config_file_init(void) |
| 21 |
{ |
| 22 |
config_file_t *ret; |
| 23 |
|
| 24 |
ret = wrapped_malloc(sizeof(config_file_t)); |
| 25 |
|
| 26 |
memset(ret, 0, sizeof(config_file_t)); |
| 27 |
|
| 28 |
return ret; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Loads a config file, any config file. |
| 33 |
* |
| 34 |
* @param options_t *main_options The options from the command line. |
| 35 |
* |
| 36 |
* @return config_file_t |
| 37 |
*/ |
| 38 |
config_file_t *config_file_load(options_t *main_options) |
| 39 |
{ |
| 40 |
config_file_t *config_file = NULL; |
| 41 |
char *check_config_file = NULL; |
| 42 |
char *file_work = NULL; |
| 43 |
int err; |
| 44 |
|
| 45 |
if (main_options->check_config_file) { |
| 46 |
check_config_file = wrapped_strdup(main_options->check_config_file); |
| 47 |
} |
| 48 |
|
| 49 |
if (check_config_file != NULL) { |
| 50 |
config_file = parse_config_file(check_config_file, NULL, main_options); |
| 51 |
if (config_file == NULL) { |
| 52 |
fprintf(stderr, "detox: unable to open: %s\n", check_config_file); |
| 53 |
exit(EXIT_FAILURE); |
| 54 |
} |
| 55 |
} else { |
| 56 |
check_config_file = wrapped_malloc(MAX_PATH_LEN); |
| 57 |
|
| 58 |
#ifdef SYSCONFDIR |
| 59 |
err = snprintf(check_config_file, MAX_PATH_LEN, "%s/detoxrc", SYSCONFDIR); |
| 60 |
if (err < MAX_PATH_LEN) { |
| 61 |
config_file = parse_config_file(check_config_file, NULL, main_options); |
| 62 |
} |
| 63 |
#endif |
| 64 |
|
| 65 |
if (config_file == NULL) { |
| 66 |
config_file = parse_config_file("/etc/detoxrc", NULL, main_options); |
| 67 |
} |
| 68 |
|
| 69 |
if (config_file == NULL) { |
| 70 |
config_file = parse_config_file("/usr/local/etc/detoxrc", NULL, main_options); |
| 71 |
} |
| 72 |
|
| 73 |
file_work = getenv("HOME"); |
| 74 |
if (file_work != NULL) { |
| 75 |
err = snprintf(check_config_file, MAX_PATH_LEN, "%s/.detoxrc", file_work); |
| 76 |
if (err < MAX_PATH_LEN) { |
| 77 |
config_file = parse_config_file(check_config_file, config_file, main_options); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
file_work = getenv("XDG_CONFIG_HOME"); |
| 82 |
if (file_work != NULL) { |
| 83 |
err = snprintf(check_config_file, MAX_PATH_LEN, "%s/detox/detoxrc", file_work); |
| 84 |
if (err < MAX_PATH_LEN) { |
| 85 |
config_file = parse_config_file(check_config_file, config_file, main_options); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (config_file == NULL) { |
| 90 |
config_file = spoof_config_file(); |
| 91 |
} |
| 92 |
|
| 93 |
free(check_config_file); |
| 94 |
} |
| 95 |
|
| 96 |
return config_file; |
| 97 |
} |