From bc6fb162c5b644bd3d31bf1b5eb8d8b2f5054dfe Mon Sep 17 00:00:00 2001 From: nova Date: Tue, 8 Jul 2025 00:43:19 +0200 Subject: [PATCH] accounting for files "." and ".." --- backend.c | 6 +++++- sorting.c | 15 +++++++-------- sorting.h | 2 ++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/backend.c b/backend.c index 975d87b..69af219 100644 --- a/backend.c +++ b/backend.c @@ -38,6 +38,10 @@ unsigned long get_dir_size(char *path){ } } closedir(dir); + if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { + /* removes files "." and ".." */ + entry_count -= 2; + } return entry_count; } @@ -45,7 +49,7 @@ 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, NULL, alphasort); + scandir(path, &entry, skip_dot, alphasort); } else { scandir(path, &entry, skip_hidden_files, alphasort); } diff --git a/sorting.c b/sorting.c index f9173e5..0164edd 100644 --- a/sorting.c +++ b/sorting.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "defines.h" extern unsigned int settings; @@ -13,6 +14,12 @@ int skip_hidden_files(const struct dirent *entry){ } return 1; } +int skip_dot(const struct dirent *entry){ + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { + return 0; + } + return 1; +} int sort_natural(const void *file0, const void *file1){ unsigned char file_type0 = ((file*)file0)->file_type; @@ -49,8 +56,6 @@ int sort_alpha(const void *file0, const void *file1){ 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; - char *file_name0 = ((file*)file0)->file_name; - char *file_name1 = ((file*)file1)->file_name; static int seed = 0; static int random = 0; @@ -60,9 +65,6 @@ int sort_random(const void *file0, const void *file1){ if (random == 0) { random = seed; } - if (strcmp(file_name0, ".") == 0 || strcmp(file_name0, "..") == 0) { - return -1; - } char weight = 0; if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) { weight |= 1; @@ -129,9 +131,6 @@ int sort_size(const void *file0, const void *file1){ unsigned long file_size1 = ((file*)file1)->file_size; char *file_name0 = ((file*)file0)->file_name; char *file_name1 = ((file*)file1)->file_name; - if (strcmp(file_name0, ".") == 0 || strcmp(file_name0, "..") == 0) { - return -1; - } if (file_type0 == file_type1) { if (file_size0 > file_size1) { return -1; diff --git a/sorting.h b/sorting.h index 8170a20..0ab0dc8 100644 --- a/sorting.h +++ b/sorting.h @@ -6,6 +6,8 @@ #include "sorting.c" #endif +int skip_hidden_files(const struct dirent *entry); +int skip_dot(const struct dirent *entry); void sort_dir(unsigned long *dir_length_width, char *dir_content); void sort_dir(unsigned long *dir_length_width, char *dir_content); int sort_natural(const void *file0, const void *file1);