diff --git a/doc/alts.1 b/doc/alts.1 index b6810b1..8946014 100644 --- a/doc/alts.1 +++ b/doc/alts.1 @@ -35,7 +35,7 @@ for a simple per-system or per-user override of the default configuration. alts -h --- this help screen alts -l[name] --- list programs or just one with given name alts [-u] [-s] -n [-p ] - sets an override with a given priority as default + sets the alternative with the given priority as default if priority is not set, then resets to default by removing override -u -- user override, default for non-root users -s -- system override, default for root users diff --git a/utils/alternatives.c b/utils/alternatives.c index 84dc503..554e72d 100644 --- a/utils/alternatives.c +++ b/utils/alternatives.c @@ -24,13 +24,12 @@ #include #include #include +#include "utils.h" #include "../src/libalternatives.h" #include "../config.h" const char binname[] = "alts"; -extern int printInstalledBinariesAndTheirOverrideStates(const char *program); - static int printTargetBinary(const char *program) { struct AlternativeLink *target = NULL; @@ -186,6 +185,19 @@ static int processOptions(int argc, char *argv[]) } } + if (!command) { + command = 1; + if (!program && optind < argc) { + program = argv[optind++]; + // TODO: actually this one should check for path + // starting with / and then print the + // alternative that belongs to the specified binary + // TODO: check next arg too and use it to + // set the alternative to the specified + // binary + } + } + switch (command) { case 'h': printHelp(); @@ -195,6 +207,8 @@ static int processOptions(int argc, char *argv[]) case 0: printHelp(); return -1; + case 1: + return printInstalledBinariesAndTheirSetting(program); case 'l': return printInstalledBinariesAndTheirOverrideStates(program); case 't': diff --git a/utils/list_binaries.c b/utils/list_binaries.c index 11722d7..94e1618 100644 --- a/utils/list_binaries.c +++ b/utils/list_binaries.c @@ -233,3 +233,52 @@ int printInstalledBinariesAndTheirOverrideStates(const char *program) return ret; } + + +static int printInstalledBinaryAndItsSetting(const char *program) +{ + int ret = 0; + struct AlternativeLink *alts = NULL; + + int priority = libalts_read_configured_priority(program, NULL); + + if (priority > 0) { + ret = libalts_load_exact_priority_binary_alternatives(program, priority, &alts); + if (ret != 0) { + priority = 0; + } + } + if (priority == 0) + ret = libalts_load_highest_priority_binary_alternatives(program, &alts); + + if (ret == 0 && alts) { + printf("%s\t%s\n", program, alts->target); + } + + return ret; +} + +int printInstalledBinariesAndTheirSetting(const char *program) +{ + char **binary_names_array = NULL; + size_t bin_size = 0; + int ret = 0; + + if (program) { + return printInstalledBinaryAndItsSetting(program); + } + + if (libalts_load_available_binaries(&binary_names_array, &bin_size) != 0) { + perror(binname); + return 1; + } + + qsort_r(&binary_names_array[0], bin_size, sizeof(char*), strcmpp, NULL); + + for (size_t i = 0; i < bin_size; ++i) { + if (printInstalledBinaryAndItsSetting(binary_names_array[i])) + ret = 1; + } + + return ret; +} diff --git a/utils/utils.h b/utils/utils.h index 6b05239..d8ad342 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -38,5 +38,6 @@ enum ConsistencyCheckFlags CONSISTENCY_LOAD_ADDITIONAL_BINARIES = 1 }; +int printInstalledBinariesAndTheirSetting(const char *program); int printInstalledBinariesAndTheirOverrideStates(const char *program); int checkGroupConsistencies(const struct InstalledBinaryData *data, unsigned n_binaries, enum ConsistencyCheckFlags flags, struct ConsistencyError **errors, unsigned *n_errors);