summaryrefslogtreecommitdiff
path: root/Userland/ls.cpp
diff options
context:
space:
mode:
authorRobin Burchell <robin+git@viroteck.net>2019-05-27 09:26:54 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-05-27 15:27:23 +0200
commit9d2b08e06eb3139fcdebef283d0635365fddd4b5 (patch)
tree02084b0ffcc3c58f7d4d31d57fbce46f60d0d5b1 /Userland/ls.cpp
parentf352a5094d66052d855a23d9c01e5006da70cc1a (diff)
downloadserenity-9d2b08e06eb3139fcdebef283d0635365fddd4b5.zip
LibCore: Add CDirIterator, and use it in everything rather than readdir
Diffstat (limited to 'Userland/ls.cpp')
-rw-r--r--Userland/ls.cpp19
1 files changed, 9 insertions, 10 deletions
diff --git a/Userland/ls.cpp b/Userland/ls.cpp
index 3e10190044..9dd54f0be2 100644
--- a/Userland/ls.cpp
+++ b/Userland/ls.cpp
@@ -10,6 +10,7 @@
#include <sys/stat.h>
#include <AK/AKString.h>
#include <AK/Vector.h>
+#include <LibCore/CDirIterator.h>
static int do_file_system_object_long(const char* path);
static int do_file_system_object_short(const char* path);
@@ -221,9 +222,9 @@ int do_file_system_object_short(const char* path)
int columns;
get_geometry(rows, columns);
- DIR* dirp = opendir(path);
- if (!dirp) {
- if (errno == ENOTDIR) {
+ CDirIterator di(path, !flag_show_dotfiles ? CDirIterator::SkipDots : CDirIterator::Flags::NoFlags);
+ if (di.has_error()) {
+ if (di.error() == ENOTDIR) {
int nprinted;
bool status = print_filesystem_object_short(path, path, &nprinted);
printf("\n");
@@ -231,20 +232,18 @@ int do_file_system_object_short(const char* path)
return 0;
return 2;
}
- perror("opendir");
+ fprintf(stderr, "CDirIterator: %s\n", di.error_string());
return 1;
}
Vector<String, 1024> names;
int longest_name = 0;
- while (auto* de = readdir(dirp)) {
- if (de->d_name[0] == '.' && !flag_show_dotfiles)
- continue;
- names.append(de->d_name);
+ while (di.has_next()) {
+ String name = di.next_path();
+ names.append(name);
if (names.last().length() > longest_name)
- longest_name = names.last().length();
+ longest_name = name.length();
}
- closedir(dirp);
int printed_on_row = 0;
int nprinted;