Make fortune file selection random ##core

This commit is contained in:
condret 2023-05-30 14:02:47 +02:00 committed by GitHub
parent b0b1a49eb7
commit 7cb41a4367
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -71,6 +71,7 @@ R_API void r_core_fortune_list_types(void) {
R_API void r_core_fortune_list(RCore *core) {
// TODO: use file.fortunes // can be dangerous in sandbox mode
const char *types = (char *)r_config_get (core->config, "cfg.fortunes.type");
RList *ftypes = r_core_fortune_types ();
if (!ftypes) {
return;
@ -94,16 +95,28 @@ R_API void r_core_fortune_list(RCore *core) {
}
static char *getrandomline(RCore *core) {
const char *types = (char *)r_config_get (core->config, "cfg.fortunes.type");
RList *types = r_str_split_duplist (
r_config_get (core->config, "cfg.fortunes.type"), ",", false);
if (r_list_empty (types)) {
r_list_free (types);
return NULL;
}
const char *file = (const char *)r_list_get_n (types, r_num_rand (r_list_length (types)));
char *type = r_str_new (file);
r_list_free (types);
if (!type) {
return NULL;
}
char *line = NULL, *templine;
RList *ftypes = r_core_fortune_types ();
if (!ftypes) {
free (type);
return NULL;
}
RListIter *iter;
char *fortunes;
r_list_foreach (ftypes, iter, fortunes) {
if (strstr (types, fortunes)) {
if (!strcmp (type, fortunes)) {
int lines = 0;
char *file = getFortuneFile (core, fortunes);
if (file) {
@ -116,6 +129,7 @@ static char *getrandomline(RCore *core) {
}
}
}
free (type);
r_list_free (ftypes);
return line;
}