diff options
author | Clownacy <Clownacy@users.noreply.github.com> | 2020-06-28 14:19:33 +0100 |
---|---|---|
committer | Harry Jeffery <harry@exec64.co.uk> | 2020-07-21 01:52:17 +0100 |
commit | 61533df66cddc5ddc0f57bf91c1623a0bb43d728 (patch) | |
tree | 944e126934b1a868643b558a0894b4dbe24b38e5 | |
parent | 500a6b3acc2f8e3bb6be1883c02004619c9edd32 (diff) | |
download | imv-61533df66cddc5ddc0f57bf91c1623a0bb43d728.tar.gz |
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.
-rw-r--r-- | src/navigator.c | 12 |
1 files changed, 7 insertions, 5 deletions
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 { |