forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listdir.cpp
66 lines (55 loc) · 2.29 KB
/
listdir.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* Copyright (c) 2024, Liav A. <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Function.h>
#include <AK/StringView.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DirIterator.h>
#include <LibCore/DirectoryEntry.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
static bool flag_show_unix_posix_file_type = false;
static bool flag_show_total_count = false;
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath"));
Vector<StringView> paths;
Core::ArgsParser args_parser;
args_parser.set_general_help("List Dirent entries in a directory.");
args_parser.add_option(flag_show_unix_posix_file_type, "Show POSIX names for file types", "posix-names", 'P');
args_parser.add_option(flag_show_total_count, "Show total count for each directory being iterated", "total-entries-count", 't');
args_parser.add_positional_argument(paths, "Directory to list", "path", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
if (paths.is_empty())
paths.append("."sv);
for (auto& path : paths) {
Core::DirIterator di(path, Core::DirIterator::NoStat);
if (di.has_error()) {
auto error = di.error();
warnln("Failed to open {} - {}", path, error);
return error;
}
outln("Traversing {}", path);
size_t count = 0;
Function<StringView(Core::DirectoryEntry::Type)> name_from_directory_entry_type;
if (flag_show_unix_posix_file_type)
name_from_directory_entry_type = Core::DirectoryEntry::posix_name_from_directory_entry_type;
else
name_from_directory_entry_type = Core::DirectoryEntry::representative_name_from_directory_entry_type;
while (di.has_next()) {
auto dir_entry = di.next();
if (dir_entry.has_value()) {
outln(" {} (Type: {}, Inode number: {})",
dir_entry.value().name,
name_from_directory_entry_type(dir_entry.value().type),
dir_entry.value().inode_number);
count++;
}
}
if (flag_show_total_count)
outln("Directory {} has {} which has being listed during the program runtime", path, count);
}
return 0;
}