Add global_only argument to discover_config_file()

This commit is contained in:
Thomas Jensen 2021-03-02 20:50:56 +01:00
parent 365dcea348
commit 97a2e2d76d
No known key found for this signature in database
GPG Key ID: A4ACEE270D0FB7DB
3 changed files with 20 additions and 8 deletions

View File

@ -107,7 +107,7 @@ static void usage(FILE *st)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
{
char *config_file = discover_config_file();
char *config_file = discover_config_file(0);
fprintf(st, "Usage: %s [options] [infile [outfile]]\n", PROJECT);
fprintf(st, " -a fmt alignment/positioning of text inside box [default: hlvt]\n");
@ -1368,7 +1368,7 @@ int main(int argc, char *argv[])
/*
* Parse config file, then reset design pointer
*/
char *config_file = discover_config_file();
char *config_file = discover_config_file(0);
if (config_file == NULL) {
exit(EXIT_FAILURE);
}

View File

@ -224,22 +224,34 @@ static char *exe_to_cfg()
#endif
char *discover_config_file()
char *discover_config_file(const int global_only)
{
int error_printed = 0;
char *result = locate_config_common(&error_printed);
int error_printed = global_only;
char *result = NULL;
if (!global_only) {
result = locate_config_common(&error_printed);
}
if (result == NULL && !error_printed) {
const char *globalconf_marker = "::GLOBALCONF::";
const char *dirs[] = {
const char *user_dirs[] = {
from_env_var("HOME", ""),
from_env_var("XDG_CONFIG_HOME", "/boxes"),
from_env_var("HOME", "/.config/boxes"),
from_env_var("HOME", "/.config/boxes")
};
const char *global_dirs[] = {
globalconf_marker,
"/etc/xdg/boxes",
"/usr/local/share/boxes",
"/usr/share/boxes"
};
const char *dirs[global_only ? 4 : 7];
if (global_only) {
memcpy(dirs, global_dirs, 4 * sizeof(char *));
} else {
memcpy(dirs, user_dirs, 3 * sizeof(char *));
memcpy(dirs + 3, global_dirs, 4 * sizeof(char *));
}
for (size_t i = 0; i < (sizeof(dirs) / sizeof(const char *)); i++) {
const char *dir = dirs[i];
if (dir == globalconf_marker) {

View File

@ -26,7 +26,7 @@
#define BOXES_DISCOVERY_H
char *discover_config_file();
char *discover_config_file(const int global_only);
#endif /* BOXES_DISCOVERY_H */