move default config path to config.h

This commit is contained in:
jacekpoz 2024-02-11 22:37:03 +01:00
parent c128b281d8
commit 5601945db1
No known key found for this signature in database
GPG key ID: 94E812A8B12AAE3C
3 changed files with 22 additions and 14 deletions

View file

@ -7,6 +7,26 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.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) { void parsing_error(size_t line) {
fprintf(stderr, "error: failed parsing config at line %zu", line); fprintf(stderr, "error: failed parsing config at line %zu", line);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View file

@ -16,6 +16,7 @@ typedef struct {
Module *modules; Module *modules;
} Config; } Config;
char *default_config_path();
void parsing_error(size_t line); void parsing_error(size_t line);

View file

@ -24,20 +24,7 @@ static const semver VERSION = {
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
char *config_path; char *config_path = default_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);
}
for (size_t i = 1; i < (size_t)argc; ++i) { for (size_t i = 1; i < (size_t)argc; ++i) {
if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) { if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config") == 0) {