summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
Diffstat (limited to 'Userland')
-rw-r--r--Userland/ls.cpp19
-rw-r--r--Userland/pape.cpp16
-rw-r--r--Userland/sysctl.cpp19
3 files changed, 26 insertions, 28 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;
diff --git a/Userland/pape.cpp b/Userland/pape.cpp
index 910673d051..138b98642d 100644
--- a/Userland/pape.cpp
+++ b/Userland/pape.cpp
@@ -10,22 +10,22 @@
#include <AK/Vector.h>
#include <AK/FileSystemPath.h>
#include <LibCore/CArgsParser.h>
+#include <LibCore/CDirIterator.h>
#include <LibGUI/GDesktop.h>
#include <LibGUI/GApplication.h>
static int handle_show_all()
{
- DIR* dirp = opendir("/res/wallpapers");
- if (!dirp) {
- perror("opendir");
+ CDirIterator di("/res/wallpapers", CDirIterator::SkipDots);
+ if (di.has_error()) {
+ fprintf(stderr, "CDirIterator: %s\n", di.error_string());
return 1;
}
- while (auto* de = readdir(dirp)) {
- if (de->d_name[0] == '.')
- continue;
- printf("%s\n", de->d_name);
+
+ while (di.has_next()) {
+ String name = di.next_path();
+ printf("%s\n", name.characters());
}
- closedir(dirp);
return 0;
}
diff --git a/Userland/sysctl.cpp b/Userland/sysctl.cpp
index dbdc75ae21..0b11a62a92 100644
--- a/Userland/sysctl.cpp
+++ b/Userland/sysctl.cpp
@@ -8,6 +8,7 @@
#include <AK/StringBuilder.h>
#include <AK/Vector.h>
#include <LibCore/CArgsParser.h>
+#include <LibCore/CDirIterator.h>
static String read_var(const String& name)
{
@@ -52,17 +53,16 @@ static void write_var(const String& name, const String& value)
static int handle_show_all()
{
- DIR* dirp = opendir("/proc/sys");
- if (!dirp) {
- perror("opendir");
+ CDirIterator di("/proc/sys", CDirIterator::SkipDots);
+ if (di.has_error()) {
+ fprintf(stderr, "CDirIterator: %s\n", di.error_string());
return 1;
}
- char pathbuf[PATH_MAX];
- while (auto* de = readdir(dirp)) {
- if (de->d_name[0] == '.')
- continue;
- sprintf(pathbuf, "/proc/sys/%s", de->d_name);
+ char pathbuf[PATH_MAX];
+ while (di.has_next()) {
+ String name = di.next_path();
+ sprintf(pathbuf, "/proc/sys/%s", name.characters());
int fd = open(pathbuf, O_RDONLY);
if (fd < 0) {
perror("open");
@@ -76,11 +76,10 @@ static int handle_show_all()
continue;
}
buffer[nread] = '\0';
- printf("%s = %s", de->d_name, buffer);
+ printf("%s = %s", name.characters(), buffer);
if (nread && buffer[nread - 1] != '\n')
printf("\n");
}
- closedir(dirp);
return 0;
}