From 5afabfb4128091e8a03fc2424d6c97bc208d3e5c Mon Sep 17 00:00:00 2001 From: nova Date: Fri, 29 Aug 2025 22:42:46 +0200 Subject: [PATCH] improvenments to random sort --- dir.c | 4 ++-- main.c | 4 ++++ sorting.c | 63 ++++++++++++++----------------------------------------- 3 files changed, 22 insertions(+), 49 deletions(-) diff --git a/dir.c b/dir.c index 8c01940..f2edd75 100644 --- a/dir.c +++ b/dir.c @@ -51,9 +51,9 @@ unsigned long get_dir_size(char *path){ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){ struct dirent **entry; if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */ - scandir(path, &entry, skip_dot, alphasort); + scandir(path, &entry, skip_dot, NULL); } else { - scandir(path, &entry, skip_hidden_files, alphasort); + scandir(path, &entry, skip_hidden_files, NULL); } unsigned long i = 0; diff --git a/main.c b/main.c index 5649b92..818ce72 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "defines.h" #include "threading.h" @@ -20,6 +21,7 @@ unsigned int settings; unsigned int file_modifiers; unsigned int status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); char *start_path; +time_t seed; WINDOW *win_t; WINDOW *win_b; @@ -188,5 +190,7 @@ void init() { char *start_path = getcwd(NULL, 0); setenv("START_PATH", start_path, 0); free(start_path); + time(&seed); + srand(seed); } diff --git a/sorting.c b/sorting.c index b4aae47..1c7fb48 100644 --- a/sorting.c +++ b/sorting.c @@ -1,11 +1,13 @@ #include #include -#include #include +#include #include +#include #include "defines.h" +extern time_t seed; extern unsigned int settings; extern unsigned int file_modifiers; @@ -97,55 +99,22 @@ int sort_alpha(const void *file0, const void *file1){ char *file_name1 = ((file*)file1)->file_name; return strcmp(file_name0, file_name1); } -int sort_random(const void *file0, const void *file1){ - unsigned char file_type0 = ((file*)file0)->file_type; - unsigned char file_type1 = ((file*)file1)->file_type; - static int seed = 0; - static int random = 0; - - if (seed == 0) { - seed = rand(); - } - if (random == 0) { - random = seed; - } - char weight = 0; - if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) { - weight |= 1; - } - if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) { - weight |= 2; - } - if (weight == 0 || weight == 3) { - random = random > 1; - if ((random & 2) == 2) { +int sort_random(const void *file0_, const void *file1_){ + file *file0 = (file*)file0_; + file *file1 = (file*)file1_; + if (file0->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR) && !(file1->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR))) { return -1; - } else { - if (random & 1){ - return 1; - } else { - return 0; - } - } - } else { - if (file_type0 > file_type1) { + } + if (!(file0->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR)) && file1->file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR)) { return 1; - } else if (file_type0 < file_type1) { - return -1; - } else { - random = random > 1; - if ((random & 2) == 2) { - return -1; - } else { - if (random & 1){ - return 1; - } else { - return 0; - } - } - } } - return 0; + int random = rand(); + if (random & 1) { + return 1; + } else { + return -1; + } + } int sort_type(const void *file0, const void *file1){ unsigned char file_type0 = ((file*)file0)->file_type;