move default config path to config.h
This commit is contained in:
parent
c128b281d8
commit
5601945db1
3 changed files with 22 additions and 14 deletions
20
config.c
20
config.c
|
@ -7,6 +7,26 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *default_config_path() {
|
||||
char *config_path = NULL;
|
||||
|
||||
char *config_dir = getenv("XDG_CONFIG_HOME");
|
||||
if (config_dir == NULL) {
|
||||
char *home = getenv("$HOME");
|
||||
if (asprintf(&config_dir, "%s/.config", home) < 0) {
|
||||
fprintf(stderr, "error: failed formatting config dir (this shouldn't happen)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (asprintf(&config_path, "%s/modfetch.conf", config_dir) < 0) {
|
||||
fprintf(stderr, "error: failed formatting config path (this shouldn't happen)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return config_path;
|
||||
}
|
||||
|
||||
void parsing_error(size_t line) {
|
||||
fprintf(stderr, "error: failed parsing config at line %zu", line);
|
||||
exit(EXIT_FAILURE);
|
||||
|
|
1
config.h
1
config.h
|
@ -16,6 +16,7 @@ typedef struct {
|
|||
Module *modules;
|
||||
} Config;
|
||||
|
||||
char *default_config_path();
|
||||
|
||||
void parsing_error(size_t line);
|
||||
|
||||
|
|
15
modfetch.c
15
modfetch.c
|
@ -24,20 +24,7 @@ static const semver VERSION = {
|
|||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
char *config_path;
|
||||
char *config_dir = getenv("XDG_CONFIG_HOME");
|
||||
if (config_dir == NULL) {
|
||||
char *home = getenv("$HOME");
|
||||
if (asprintf(&config_dir, "%s/.config", home) < 0) {
|
||||
fprintf(stderr, "error: failed formatting config dir (this shouldn't happen)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (asprintf(&config_path, "%s/modfetch.conf", config_dir) < 0) {
|
||||
fprintf(stderr, "error: failed formatting config path (this shouldn't happen)");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
char *config_path = default_config_path();
|
||||
|
||||
for (size_t i = 1; i < (size_t)argc; ++i) {
|
||||
if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) {
|
||||
|
|
Loading…
Reference in a new issue