accounting for files "." and ".."

This commit is contained in:
nova
2025-07-08 00:43:19 +02:00
parent b77c9a2a29
commit bc6fb162c5
3 changed files with 14 additions and 9 deletions

View File

@ -38,6 +38,10 @@ unsigned long get_dir_size(char *path){
} }
} }
closedir(dir); closedir(dir);
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
/* removes files "." and ".." */
entry_count -= 2;
}
return entry_count; 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){ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){
struct dirent **entry; struct dirent **entry;
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */ if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
scandir(path, &entry, NULL, alphasort); scandir(path, &entry, skip_dot, alphasort);
} else { } else {
scandir(path, &entry, skip_hidden_files, alphasort); scandir(path, &entry, skip_hidden_files, alphasort);
} }

View File

@ -2,6 +2,7 @@
#include <dirent.h> #include <dirent.h>
#include <strings.h> #include <strings.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "defines.h" #include "defines.h"
extern unsigned int settings; extern unsigned int settings;
@ -13,6 +14,12 @@ int skip_hidden_files(const struct dirent *entry){
} }
return 1; 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){ int sort_natural(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type; 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){ int sort_random(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type; unsigned char file_type0 = ((file*)file0)->file_type;
unsigned char file_type1 = ((file*)file1)->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 seed = 0;
static int random = 0; static int random = 0;
@ -60,9 +65,6 @@ int sort_random(const void *file0, const void *file1){
if (random == 0) { if (random == 0) {
random = seed; random = seed;
} }
if (strcmp(file_name0, ".") == 0 || strcmp(file_name0, "..") == 0) {
return -1;
}
char weight = 0; char weight = 0;
if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) { if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) {
weight |= 1; weight |= 1;
@ -129,9 +131,6 @@ int sort_size(const void *file0, const void *file1){
unsigned long file_size1 = ((file*)file1)->file_size; unsigned long file_size1 = ((file*)file1)->file_size;
char *file_name0 = ((file*)file0)->file_name; char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->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_type0 == file_type1) {
if (file_size0 > file_size1) { if (file_size0 > file_size1) {
return -1; return -1;

View File

@ -6,6 +6,8 @@
#include "sorting.c" #include "sorting.c"
#endif #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);
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); int sort_natural(const void *file0, const void *file1);