bore

Unnamed repository
Log | Files | Refs | README

commit 8b07c6d2c6f7b0bfdaabc7c8abfb896e6d99b77b
parent bd456465521b7cc55684a21531b27c712faa4138
Author: phoebos <ben@bvnf.space>
Date:   Mon, 20 Sep 2021 22:54:25 +0100

ls: use scandir(3) and sort

Diffstat:
Mls.c | 11++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/ls.c b/ls.c @@ -18,15 +18,16 @@ ls(const char *path, int flags) { } if (S_ISDIR(st.st_mode)) { /* directory */ - DIR *dir = opendir(path); - if (dir == NULL) { + int n, i; + struct dirent **dp; + n = scandir(path, &dp, 0, alphasort); + if (n == -1) { fprintf(stderr, "ls: %s: %s\n", path, strerror(errno)); return 1; } - struct dirent *dp; - while ((dp = readdir(dir)) != NULL) { - printf("%s", dp->d_name); + for (i = 0; i < n; i++) { + printf("%s", dp[i]->d_name); if (flags & FLAG_1) putc('\n', stdout);