mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 10:57:36 +00:00
LibCore: Add CDirIterator, and use it in everything rather than readdir
This commit is contained in:
parent
f352a5094d
commit
9d2b08e06e
9 changed files with 152 additions and 55 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue