From 61533df66cddc5ddc0f57bf91c1623a0bb43d728 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Sun, 28 Jun 2020 14:19:33 +0100 Subject: Sort directory entries alphabetically Previously, they would be sorted by however readdir returned them. I suppose a proper configuration option to control sorting would be nice, but for now, I think replacing forced random order with forced alphabetical order is an improvement. --- src/navigator.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/navigator.c b/src/navigator.c index c399404..f2901cf 100644 --- a/src/navigator.c +++ b/src/navigator.c @@ -76,10 +76,11 @@ int imv_navigator_add(struct imv_navigator *nav, const char *path, if ((stat(path, &path_info) == 0) && S_ISDIR(path_info.st_mode)) { int result = 0; - DIR *d = opendir(path); - if (d) { - struct dirent *dir; - while ((dir = readdir(d)) != NULL) { + struct dirent **dir_list; + int total_dirs = scandir(path, &dir_list, 0, alphasort); + if (total_dirs >= 0) { + for (int i = 0; i < total_dirs; ++i) { + struct dirent *dir = dir_list[i]; if (strcmp(dir->d_name, "..") == 0 || strcmp(dir->d_name, ".") == 0) { continue; } @@ -101,8 +102,9 @@ int imv_navigator_add(struct imv_navigator *nav, const char *path, break; } } + free(dir_list[i]); } - closedir(d); + free(dir_list); } return result; } else { -- cgit v1.2.3