2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-15 10:34:40 +00:00
|
|
|
#include <AK/Assertions.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2020-01-11 12:30:17 +00:00
|
|
|
#include <AK/HashMap.h>
|
2020-08-15 18:05:46 +00:00
|
|
|
#include <AK/NumberFormat.h>
|
2019-08-20 19:37:25 +00:00
|
|
|
#include <AK/QuickSort.h>
|
|
|
|
#include <AK/StringBuilder.h>
|
2021-05-29 21:15:54 +00:00
|
|
|
#include <AK/URL.h>
|
2020-05-17 11:02:48 +00:00
|
|
|
#include <AK/Utf8View.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <AK/Vector.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-11 19:05:08 +00:00
|
|
|
#include <LibCore/DateTime.h>
|
2023-02-08 20:08:01 +00:00
|
|
|
#include <LibCore/DeprecatedFile.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/DirIterator.h>
|
2021-12-02 21:42:10 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-01-15 22:01:38 +00:00
|
|
|
#include <ctype.h>
|
2018-11-29 02:45:23 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2019-02-08 01:38:21 +00:00
|
|
|
#include <fcntl.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <grp.h>
|
2021-05-01 22:00:52 +00:00
|
|
|
#include <inttypes.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2018-11-29 02:45:23 +00:00
|
|
|
#include <sys/ioctl.h>
|
2019-01-31 04:05:57 +00:00
|
|
|
#include <sys/stat.h>
|
2021-11-26 15:55:45 +00:00
|
|
|
#include <sys/sysmacros.h>
|
2021-05-14 15:55:39 +00:00
|
|
|
#include <sys/types.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2018-10-24 07:48:24 +00:00
|
|
|
|
2021-08-14 18:54:57 +00:00
|
|
|
struct FileMetadata {
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString name;
|
|
|
|
DeprecatedString path;
|
2021-08-18 17:23:59 +00:00
|
|
|
struct stat stat {
|
|
|
|
};
|
2021-08-14 18:54:57 +00:00
|
|
|
};
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static int do_file_system_object_long(char const* path);
|
|
|
|
static int do_file_system_object_short(char const* path);
|
2018-11-29 02:45:23 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static bool print_names(char const* path, size_t longest_name, Vector<FileMetadata> const& files);
|
2021-08-14 18:54:57 +00:00
|
|
|
|
|
|
|
static bool filemetadata_comparator(FileMetadata& a, FileMetadata& b);
|
2021-04-16 16:56:08 +00:00
|
|
|
|
2020-11-11 19:44:30 +00:00
|
|
|
static bool flag_classify = false;
|
2020-06-08 06:36:51 +00:00
|
|
|
static bool flag_colorize = false;
|
2018-11-29 02:45:23 +00:00
|
|
|
static bool flag_long = false;
|
|
|
|
static bool flag_show_dotfiles = false;
|
2020-11-10 04:13:46 +00:00
|
|
|
static bool flag_show_almost_all_dotfiles = false;
|
2020-11-10 13:05:45 +00:00
|
|
|
static bool flag_ignore_backups = false;
|
2020-11-10 17:42:41 +00:00
|
|
|
static bool flag_list_directories_only = false;
|
2019-01-31 16:34:24 +00:00
|
|
|
static bool flag_show_inode = false;
|
2019-06-01 11:23:35 +00:00
|
|
|
static bool flag_print_numeric = false;
|
2020-11-10 13:05:45 +00:00
|
|
|
static bool flag_hide_group = false;
|
2019-09-11 16:59:13 +00:00
|
|
|
static bool flag_human_readable = false;
|
2023-01-10 12:09:22 +00:00
|
|
|
static bool flag_human_readable_si = false;
|
2019-10-19 18:51:21 +00:00
|
|
|
static bool flag_sort_by_timestamp = false;
|
|
|
|
static bool flag_reverse_sort = false;
|
2020-05-09 14:17:47 +00:00
|
|
|
static bool flag_disable_hyperlinks = false;
|
2021-07-11 16:30:48 +00:00
|
|
|
static bool flag_recursive = false;
|
2022-02-19 21:51:39 +00:00
|
|
|
static bool flag_force_newline = false;
|
2018-11-17 00:04:00 +00:00
|
|
|
|
2019-12-09 16:45:40 +00:00
|
|
|
static size_t terminal_rows = 0;
|
|
|
|
static size_t terminal_columns = 0;
|
2019-08-20 19:05:05 +00:00
|
|
|
static bool output_is_terminal = false;
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static HashMap<uid_t, DeprecatedString> users;
|
|
|
|
static HashMap<gid_t, DeprecatedString> groups;
|
2020-01-11 12:30:17 +00:00
|
|
|
|
2021-04-17 20:43:45 +00:00
|
|
|
static bool is_a_tty = false;
|
|
|
|
|
2021-12-02 21:42:10 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2018-10-24 07:48:24 +00:00
|
|
|
{
|
2021-12-02 21:42:10 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath tty"));
|
2020-01-13 00:12:07 +00:00
|
|
|
|
2019-08-20 19:05:05 +00:00
|
|
|
struct winsize ws;
|
|
|
|
int rc = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
|
|
|
|
if (rc == 0) {
|
|
|
|
terminal_rows = ws.ws_row;
|
|
|
|
terminal_columns = ws.ws_col;
|
|
|
|
output_is_terminal = true;
|
|
|
|
}
|
2021-04-17 20:43:45 +00:00
|
|
|
|
|
|
|
is_a_tty = isatty(STDOUT_FILENO);
|
|
|
|
if (!is_a_tty) {
|
2020-06-08 06:36:51 +00:00
|
|
|
flag_disable_hyperlinks = true;
|
|
|
|
} else {
|
|
|
|
flag_colorize = true;
|
|
|
|
}
|
2019-08-20 19:05:05 +00:00
|
|
|
|
2022-04-03 23:17:11 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-01-13 00:12:07 +00:00
|
|
|
|
2021-11-26 21:32:37 +00:00
|
|
|
Vector<StringView> paths;
|
2020-01-27 17:25:36 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 15:22:58 +00:00
|
|
|
args_parser.set_general_help("List files in a directory.");
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(flag_show_dotfiles, "Show dotfiles", "all", 'a');
|
2020-11-10 04:13:46 +00:00
|
|
|
args_parser.add_option(flag_show_almost_all_dotfiles, "Do not list implied . and .. directories", nullptr, 'A');
|
2022-03-22 15:52:51 +00:00
|
|
|
args_parser.add_option(flag_ignore_backups, "Do not list implied entries ending with ~", "ignore-backups", 'B');
|
2020-11-10 17:42:41 +00:00
|
|
|
args_parser.add_option(flag_list_directories_only, "List directories themselves, not their contents", "directory", 'd');
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(flag_long, "Display long info", "long", 'l');
|
|
|
|
args_parser.add_option(flag_sort_by_timestamp, "Sort files by timestamp", nullptr, 't');
|
|
|
|
args_parser.add_option(flag_reverse_sort, "Reverse sort order", "reverse", 'r');
|
2020-11-11 19:44:30 +00:00
|
|
|
args_parser.add_option(flag_classify, "Append a file type indicator to entries", "classify", 'F');
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(flag_colorize, "Use pretty colors", nullptr, 'G');
|
|
|
|
args_parser.add_option(flag_show_inode, "Show inode ids", "inode", 'i');
|
|
|
|
args_parser.add_option(flag_print_numeric, "In long format, display numeric UID/GID", "numeric-uid-gid", 'n');
|
2020-11-10 13:05:45 +00:00
|
|
|
args_parser.add_option(flag_hide_group, "In long format, do not show group information", nullptr, 'o');
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(flag_human_readable, "Print human-readable sizes", "human-readable", 'h');
|
2023-01-10 12:09:22 +00:00
|
|
|
args_parser.add_option(flag_human_readable_si, "Print human-readable sizes in SI units", "si", 0);
|
2020-05-09 14:17:47 +00:00
|
|
|
args_parser.add_option(flag_disable_hyperlinks, "Disable hyperlinks", "no-hyperlinks", 'K');
|
2021-07-11 16:30:48 +00:00
|
|
|
args_parser.add_option(flag_recursive, "List subdirectories recursively", "recursive", 'R');
|
2022-02-19 21:51:39 +00:00
|
|
|
args_parser.add_option(flag_force_newline, "List one file per line", nullptr, '1');
|
2020-02-02 11:34:39 +00:00
|
|
|
args_parser.add_positional_argument(paths, "Directory to list", "path", Core::ArgsParser::Required::No);
|
2021-12-02 21:42:10 +00:00
|
|
|
args_parser.parse(arguments);
|
2018-11-29 02:45:23 +00:00
|
|
|
|
2020-11-10 04:13:46 +00:00
|
|
|
if (flag_show_almost_all_dotfiles)
|
|
|
|
flag_show_dotfiles = true;
|
|
|
|
|
2020-01-11 12:30:17 +00:00
|
|
|
if (flag_long) {
|
|
|
|
setpwent();
|
|
|
|
for (auto* pwd = getpwent(); pwd; pwd = getpwent())
|
|
|
|
users.set(pwd->pw_uid, pwd->pw_name);
|
|
|
|
endpwent();
|
|
|
|
setgrent();
|
|
|
|
for (auto* grp = getgrent(); grp; grp = getgrent())
|
|
|
|
groups.set(grp->gr_gid, grp->gr_name);
|
|
|
|
endgrent();
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
auto do_file_system_object = [&](char const* path) {
|
2019-05-27 13:04:23 +00:00
|
|
|
if (flag_long)
|
|
|
|
return do_file_system_object_long(path);
|
|
|
|
return do_file_system_object_short(path);
|
|
|
|
};
|
|
|
|
|
2021-07-11 16:30:48 +00:00
|
|
|
if (paths.is_empty())
|
2022-07-11 17:32:29 +00:00
|
|
|
paths.append("."sv);
|
2021-04-16 16:56:08 +00:00
|
|
|
|
2021-08-14 18:54:57 +00:00
|
|
|
Vector<FileMetadata> files;
|
|
|
|
for (auto& path : paths) {
|
|
|
|
FileMetadata metadata;
|
|
|
|
metadata.name = path;
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
int rc = lstat(DeprecatedString(path).characters(), &metadata.stat);
|
2022-01-04 20:19:23 +00:00
|
|
|
if (rc < 0) {
|
2021-08-14 18:54:57 +00:00
|
|
|
perror("lstat");
|
2022-01-04 20:19:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-08-18 17:23:59 +00:00
|
|
|
|
2021-08-14 18:54:57 +00:00
|
|
|
files.append(metadata);
|
|
|
|
}
|
|
|
|
quick_sort(files, filemetadata_comparator);
|
2021-04-16 16:56:08 +00:00
|
|
|
|
2021-04-26 07:17:50 +00:00
|
|
|
int status = 0;
|
2021-04-16 16:56:08 +00:00
|
|
|
|
2021-08-14 18:54:57 +00:00
|
|
|
for (size_t i = 0; i < files.size(); i++) {
|
|
|
|
auto path = files[i].name;
|
2021-04-11 14:48:50 +00:00
|
|
|
|
2023-02-08 20:08:01 +00:00
|
|
|
if (flag_recursive && Core::DeprecatedFile::is_directory(path)) {
|
2021-07-11 16:30:48 +00:00
|
|
|
size_t subdirs = 0;
|
|
|
|
Core::DirIterator di(path, Core::DirIterator::SkipParentAndBaseDir);
|
|
|
|
|
|
|
|
if (di.has_error()) {
|
|
|
|
status = 1;
|
2023-03-01 15:55:15 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", path.characters(), strerror(di.error().code()));
|
2021-07-11 16:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (di.has_next()) {
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString directory = di.next_full_path();
|
2023-02-08 20:08:01 +00:00
|
|
|
if (Core::DeprecatedFile::is_directory(directory) && !Core::DeprecatedFile::is_link(directory)) {
|
2021-07-11 16:30:48 +00:00
|
|
|
++subdirs;
|
2021-08-14 18:54:57 +00:00
|
|
|
FileMetadata new_file;
|
|
|
|
new_file.name = move(directory);
|
|
|
|
files.insert(i + subdirs, move(new_file));
|
2021-07-11 16:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:08:01 +00:00
|
|
|
bool show_dir_separator = files.size() > 1 && Core::DeprecatedFile::is_directory(path) && !flag_list_directories_only;
|
2021-04-26 07:17:50 +00:00
|
|
|
if (show_dir_separator) {
|
2021-07-11 16:30:48 +00:00
|
|
|
printf("%s:\n", path.characters());
|
2021-04-26 07:17:50 +00:00
|
|
|
}
|
2021-07-11 16:30:48 +00:00
|
|
|
auto rc = do_file_system_object(path.characters());
|
2021-04-26 07:17:50 +00:00
|
|
|
if (rc != 0)
|
|
|
|
status = rc;
|
2021-08-14 18:54:57 +00:00
|
|
|
if (show_dir_separator && i != files.size() - 1) {
|
2021-04-26 07:17:50 +00:00
|
|
|
puts("");
|
2019-05-27 12:30:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-26 07:17:50 +00:00
|
|
|
|
2019-05-27 12:41:59 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-09-18 16:02:41 +00:00
|
|
|
static int print_escaped(StringView name)
|
2020-01-15 22:01:38 +00:00
|
|
|
{
|
|
|
|
int printed = 0;
|
|
|
|
|
2020-05-17 11:02:48 +00:00
|
|
|
Utf8View utf8_name(name);
|
|
|
|
if (utf8_name.validate()) {
|
2021-09-18 16:02:41 +00:00
|
|
|
out("{}", name);
|
2020-10-20 15:47:34 +00:00
|
|
|
return utf8_name.length();
|
2020-05-17 11:02:48 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 23:15:28 +00:00
|
|
|
for (auto c : name) {
|
|
|
|
if (isprint(c)) {
|
|
|
|
putchar(c);
|
2020-01-15 22:01:38 +00:00
|
|
|
printed++;
|
|
|
|
} else {
|
2022-12-26 23:15:28 +00:00
|
|
|
printed += printf("\\%03d", c);
|
2020-01-15 22:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return printed;
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static DeprecatedString& hostname()
|
2020-05-09 14:17:47 +00:00
|
|
|
{
|
2022-12-04 18:02:33 +00:00
|
|
|
static DeprecatedString s_hostname;
|
2020-05-09 14:17:47 +00:00
|
|
|
if (s_hostname.is_null()) {
|
|
|
|
char buffer[HOST_NAME_MAX];
|
|
|
|
if (gethostname(buffer, sizeof(buffer)) == 0)
|
|
|
|
s_hostname = buffer;
|
|
|
|
else
|
|
|
|
s_hostname = "localhost";
|
|
|
|
}
|
|
|
|
return s_hostname;
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static size_t print_name(const struct stat& st, DeprecatedString const& name, char const* path_for_link_resolution, char const* path_for_hyperlink)
|
2018-11-29 02:45:23 +00:00
|
|
|
{
|
2020-05-09 14:17:47 +00:00
|
|
|
if (!flag_disable_hyperlinks) {
|
2023-02-08 20:08:01 +00:00
|
|
|
auto full_path = Core::DeprecatedFile::real_path_for(path_for_hyperlink);
|
2021-01-03 18:14:57 +00:00
|
|
|
if (!full_path.is_null()) {
|
2021-05-29 21:15:54 +00:00
|
|
|
auto url = URL::create_with_file_scheme(full_path, {}, hostname());
|
|
|
|
out("\033]8;;{}\033\\", url.serialize());
|
2020-05-09 14:17:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 13:49:47 +00:00
|
|
|
size_t nprinted = 0;
|
2020-01-15 22:01:38 +00:00
|
|
|
|
2019-08-20 19:05:05 +00:00
|
|
|
if (!flag_colorize || !output_is_terminal) {
|
2020-01-15 22:01:38 +00:00
|
|
|
nprinted = printf("%s", name.characters());
|
2018-11-29 02:45:23 +00:00
|
|
|
} else {
|
2022-04-01 17:58:27 +00:00
|
|
|
char const* begin_color = "";
|
|
|
|
char const* end_color = "\033[0m";
|
2018-11-29 02:45:23 +00:00
|
|
|
|
2020-01-04 10:38:02 +00:00
|
|
|
if (st.st_mode & S_ISVTX)
|
|
|
|
begin_color = "\033[42;30;1m";
|
2020-01-05 11:49:25 +00:00
|
|
|
else if (st.st_mode & S_ISUID)
|
|
|
|
begin_color = "\033[41;1m";
|
2020-11-02 06:31:17 +00:00
|
|
|
else if (st.st_mode & S_ISGID)
|
|
|
|
begin_color = "\033[43;1m";
|
2020-01-04 10:38:02 +00:00
|
|
|
else if (S_ISLNK(st.st_mode))
|
2018-11-29 02:45:23 +00:00
|
|
|
begin_color = "\033[36;1m";
|
|
|
|
else if (S_ISDIR(st.st_mode))
|
|
|
|
begin_color = "\033[34;1m";
|
|
|
|
else if (st.st_mode & 0111)
|
|
|
|
begin_color = "\033[32;1m";
|
2019-02-14 16:30:58 +00:00
|
|
|
else if (S_ISSOCK(st.st_mode))
|
|
|
|
begin_color = "\033[35;1m";
|
2018-11-29 02:45:23 +00:00
|
|
|
else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
|
|
|
|
begin_color = "\033[33;1m";
|
2020-01-15 22:01:38 +00:00
|
|
|
printf("%s", begin_color);
|
2022-07-11 17:32:29 +00:00
|
|
|
nprinted = print_escaped(name);
|
2020-01-15 22:01:38 +00:00
|
|
|
printf("%s", end_color);
|
2018-11-17 00:04:00 +00:00
|
|
|
}
|
2018-11-29 02:45:23 +00:00
|
|
|
if (S_ISLNK(st.st_mode)) {
|
|
|
|
if (path_for_link_resolution) {
|
2023-02-08 20:08:01 +00:00
|
|
|
auto link_destination_or_error = Core::DeprecatedFile::read_link(path_for_link_resolution);
|
2022-03-23 15:36:03 +00:00
|
|
|
if (link_destination_or_error.is_error()) {
|
2020-06-16 19:01:50 +00:00
|
|
|
perror("readlink");
|
2020-04-14 15:39:56 +00:00
|
|
|
} else {
|
2022-07-11 17:32:29 +00:00
|
|
|
nprinted += printf(" -> ") + print_escaped(link_destination_or_error.value());
|
2020-04-14 15:39:56 +00:00
|
|
|
}
|
2018-11-29 02:45:23 +00:00
|
|
|
} else {
|
2020-11-11 19:44:30 +00:00
|
|
|
if (flag_classify)
|
|
|
|
nprinted += printf("@");
|
2018-11-29 02:45:23 +00:00
|
|
|
}
|
|
|
|
} else if (S_ISDIR(st.st_mode)) {
|
2020-11-11 19:44:30 +00:00
|
|
|
if (flag_classify)
|
|
|
|
nprinted += printf("/");
|
2018-11-29 02:45:23 +00:00
|
|
|
} else if (st.st_mode & 0111) {
|
2020-11-11 19:44:30 +00:00
|
|
|
if (flag_classify)
|
|
|
|
nprinted += printf("*");
|
2018-11-29 02:45:23 +00:00
|
|
|
}
|
2020-05-09 14:17:47 +00:00
|
|
|
|
|
|
|
if (!flag_disable_hyperlinks) {
|
|
|
|
printf("\033]8;;\033\\");
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:45:23 +00:00
|
|
|
return nprinted;
|
2018-11-17 00:04:00 +00:00
|
|
|
}
|
2018-10-27 23:17:48 +00:00
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
static bool print_filesystem_object(DeprecatedString const& path, DeprecatedString const& name, const struct stat& st)
|
2019-06-07 09:49:31 +00:00
|
|
|
{
|
2019-05-27 01:41:22 +00:00
|
|
|
if (flag_show_inode)
|
2022-12-04 18:02:33 +00:00
|
|
|
printf("%s ", DeprecatedString::formatted("{}", st.st_ino).characters());
|
2019-05-27 01:41:22 +00:00
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
printf("d");
|
|
|
|
else if (S_ISLNK(st.st_mode))
|
|
|
|
printf("l");
|
|
|
|
else if (S_ISBLK(st.st_mode))
|
|
|
|
printf("b");
|
|
|
|
else if (S_ISCHR(st.st_mode))
|
|
|
|
printf("c");
|
|
|
|
else if (S_ISFIFO(st.st_mode))
|
|
|
|
printf("f");
|
|
|
|
else if (S_ISSOCK(st.st_mode))
|
|
|
|
printf("s");
|
|
|
|
else if (S_ISREG(st.st_mode))
|
|
|
|
printf("-");
|
|
|
|
else
|
|
|
|
printf("?");
|
|
|
|
|
|
|
|
printf("%c%c%c%c%c%c%c%c",
|
|
|
|
st.st_mode & S_IRUSR ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWUSR ? 'w' : '-',
|
|
|
|
st.st_mode & S_ISUID ? 's' : (st.st_mode & S_IXUSR ? 'x' : '-'),
|
|
|
|
st.st_mode & S_IRGRP ? 'r' : '-',
|
|
|
|
st.st_mode & S_IWGRP ? 'w' : '-',
|
|
|
|
st.st_mode & S_ISGID ? 's' : (st.st_mode & S_IXGRP ? 'x' : '-'),
|
|
|
|
st.st_mode & S_IROTH ? 'r' : '-',
|
2019-06-07 09:49:31 +00:00
|
|
|
st.st_mode & S_IWOTH ? 'w' : '-');
|
2019-05-27 01:41:22 +00:00
|
|
|
|
|
|
|
if (st.st_mode & S_ISVTX)
|
|
|
|
printf("t");
|
|
|
|
else
|
|
|
|
printf("%c", st.st_mode & S_IXOTH ? 'x' : '-');
|
|
|
|
|
2021-01-29 22:05:02 +00:00
|
|
|
printf(" %3u", st.st_nlink);
|
2021-01-06 15:29:33 +00:00
|
|
|
|
2020-01-11 12:30:17 +00:00
|
|
|
auto username = users.get(st.st_uid);
|
|
|
|
if (!flag_print_numeric && username.has_value()) {
|
|
|
|
printf(" %7s", username.value().characters());
|
2019-06-01 11:23:35 +00:00
|
|
|
} else {
|
2020-01-09 20:43:21 +00:00
|
|
|
printf(" %7u", st.st_uid);
|
2019-06-01 11:23:35 +00:00
|
|
|
}
|
2020-11-10 13:05:45 +00:00
|
|
|
|
|
|
|
if (!flag_hide_group) {
|
|
|
|
auto groupname = groups.get(st.st_gid);
|
|
|
|
if (!flag_print_numeric && groupname.has_value()) {
|
|
|
|
printf(" %7s", groupname.value().characters());
|
|
|
|
} else {
|
|
|
|
printf(" %7u", st.st_gid);
|
|
|
|
}
|
2019-06-01 11:23:35 +00:00
|
|
|
}
|
2019-05-27 01:41:22 +00:00
|
|
|
|
2019-09-11 16:59:13 +00:00
|
|
|
if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
|
2019-10-19 18:52:54 +00:00
|
|
|
printf(" %4u,%4u ", major(st.st_rdev), minor(st.st_rdev));
|
2019-09-11 16:59:13 +00:00
|
|
|
} else {
|
|
|
|
if (flag_human_readable) {
|
2021-03-17 17:52:14 +00:00
|
|
|
printf(" %10s ", human_readable_size(st.st_size).characters());
|
2023-01-10 12:09:22 +00:00
|
|
|
} else if (flag_human_readable_si) {
|
|
|
|
printf(" %10s ", human_readable_size(st.st_size, AK::HumanReadableBasedOn::Base10).characters());
|
2019-09-11 16:59:13 +00:00
|
|
|
} else {
|
2021-05-01 22:00:52 +00:00
|
|
|
printf(" %10" PRIu64 " ", (uint64_t)st.st_size);
|
2019-09-11 16:59:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-27 01:41:22 +00:00
|
|
|
|
2022-12-06 01:12:49 +00:00
|
|
|
printf(" %s ", Core::DateTime::from_timestamp(st.st_mtime).to_deprecated_string().characters());
|
2019-05-27 01:41:22 +00:00
|
|
|
|
2020-05-10 15:48:41 +00:00
|
|
|
print_name(st, name, path.characters(), path.characters());
|
2019-05-27 01:41:22 +00:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static int do_file_system_object_long(char const* path)
|
2018-11-17 00:04:00 +00:00
|
|
|
{
|
2020-11-10 17:42:41 +00:00
|
|
|
if (flag_list_directories_only) {
|
2021-08-18 17:23:59 +00:00
|
|
|
struct stat stat {
|
|
|
|
};
|
2020-11-10 17:42:41 +00:00
|
|
|
int rc = lstat(path, &stat);
|
2021-08-18 17:23:59 +00:00
|
|
|
if (rc < 0)
|
2020-11-10 17:42:41 +00:00
|
|
|
perror("lstat");
|
|
|
|
if (print_filesystem_object(path, path, stat))
|
|
|
|
return 0;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flags = Core::DirIterator::SkipDots;
|
|
|
|
if (flag_show_dotfiles)
|
|
|
|
flags = Core::DirIterator::Flags::NoFlags;
|
|
|
|
if (flag_show_almost_all_dotfiles)
|
|
|
|
flags = Core::DirIterator::SkipParentAndBaseDir;
|
|
|
|
|
|
|
|
Core::DirIterator di(path, flags);
|
|
|
|
|
2019-08-20 19:37:25 +00:00
|
|
|
if (di.has_error()) {
|
2023-03-01 15:55:15 +00:00
|
|
|
auto error = di.error();
|
|
|
|
if (error.code() == ENOTDIR) {
|
2021-08-18 17:23:59 +00:00
|
|
|
struct stat stat {
|
|
|
|
};
|
2019-10-19 18:51:21 +00:00
|
|
|
int rc = lstat(path, &stat);
|
2021-08-18 17:23:59 +00:00
|
|
|
if (rc < 0)
|
2019-10-19 18:51:21 +00:00
|
|
|
perror("lstat");
|
|
|
|
if (print_filesystem_object(path, path, stat))
|
2019-05-27 12:13:25 +00:00
|
|
|
return 0;
|
|
|
|
return 2;
|
|
|
|
}
|
2023-03-01 15:55:15 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", path, strerror(di.error().code()));
|
2018-10-24 07:48:24 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-11-29 02:45:23 +00:00
|
|
|
|
2019-10-19 18:51:21 +00:00
|
|
|
Vector<FileMetadata> files;
|
|
|
|
while (di.has_next()) {
|
|
|
|
FileMetadata metadata;
|
|
|
|
metadata.name = di.next_path();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!metadata.name.is_empty());
|
2020-11-10 04:13:46 +00:00
|
|
|
|
2020-11-10 13:05:45 +00:00
|
|
|
if (metadata.name.ends_with('~') && flag_ignore_backups && metadata.name != path)
|
|
|
|
continue;
|
|
|
|
|
2019-08-20 19:37:25 +00:00
|
|
|
StringBuilder builder;
|
2022-07-11 19:53:29 +00:00
|
|
|
builder.append({ path, strlen(path) });
|
2019-08-20 19:37:25 +00:00
|
|
|
builder.append('/');
|
2019-10-19 18:51:21 +00:00
|
|
|
builder.append(metadata.name);
|
2022-12-06 01:12:49 +00:00
|
|
|
metadata.path = builder.to_deprecated_string();
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!metadata.path.is_null());
|
2019-10-19 18:51:21 +00:00
|
|
|
int rc = lstat(metadata.path.characters(), &metadata.stat);
|
2021-08-18 17:23:59 +00:00
|
|
|
if (rc < 0)
|
2019-10-19 18:51:21 +00:00
|
|
|
perror("lstat");
|
2021-08-18 17:23:59 +00:00
|
|
|
|
2019-10-19 18:51:21 +00:00
|
|
|
files.append(move(metadata));
|
|
|
|
}
|
|
|
|
|
2021-08-14 18:54:57 +00:00
|
|
|
quick_sort(files, filemetadata_comparator);
|
2019-10-19 18:51:21 +00:00
|
|
|
|
|
|
|
for (auto& file : files) {
|
|
|
|
if (!print_filesystem_object(file.path, file.name, file.stat))
|
2019-05-27 12:13:25 +00:00
|
|
|
return 2;
|
2018-11-29 02:45:23 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static bool print_filesystem_object_short(char const* path, char const* name, size_t* nprinted)
|
2019-06-07 09:49:31 +00:00
|
|
|
{
|
2019-05-26 17:52:51 +00:00
|
|
|
struct stat st;
|
|
|
|
int rc = lstat(path, &st);
|
|
|
|
if (rc == -1) {
|
|
|
|
printf("lstat(%s) failed: %s\n", path, strerror(errno));
|
2019-05-27 01:41:22 +00:00
|
|
|
return false;
|
2019-05-26 17:52:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 02:55:44 +00:00
|
|
|
if (flag_show_inode)
|
2022-12-04 18:02:33 +00:00
|
|
|
printf("%s ", DeprecatedString::formatted("{}", st.st_ino).characters());
|
2020-11-09 02:55:44 +00:00
|
|
|
|
2020-05-10 15:48:41 +00:00
|
|
|
*nprinted = print_name(st, name, nullptr, path);
|
2019-05-27 01:41:22 +00:00
|
|
|
return true;
|
2019-05-26 17:52:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
static bool print_names(char const* path, size_t longest_name, Vector<FileMetadata> const& files)
|
2021-04-16 16:56:08 +00:00
|
|
|
{
|
|
|
|
size_t printed_on_row = 0;
|
|
|
|
size_t nprinted = 0;
|
2021-08-14 18:54:57 +00:00
|
|
|
for (size_t i = 0; i < files.size(); ++i) {
|
|
|
|
auto& name = files[i].name;
|
2021-04-16 16:56:08 +00:00
|
|
|
StringBuilder builder;
|
2022-07-11 19:53:29 +00:00
|
|
|
builder.append({ path, strlen(path) });
|
2021-04-16 16:56:08 +00:00
|
|
|
builder.append('/');
|
|
|
|
builder.append(name);
|
2022-12-06 01:12:49 +00:00
|
|
|
if (!print_filesystem_object_short(builder.to_deprecated_string().characters(), name.characters(), &nprinted))
|
2021-04-16 16:56:08 +00:00
|
|
|
return 2;
|
|
|
|
int offset = 0;
|
|
|
|
if (terminal_columns > longest_name)
|
|
|
|
offset = terminal_columns % longest_name / (terminal_columns / longest_name);
|
|
|
|
|
|
|
|
// The offset must be at least 2 because:
|
|
|
|
// - With each file an additional char is printed e.g. '@','*'.
|
|
|
|
// - Each filename must be separated by a space.
|
|
|
|
size_t column_width = longest_name + max(offset, 2);
|
|
|
|
printed_on_row += column_width;
|
|
|
|
|
2021-04-17 20:43:45 +00:00
|
|
|
if (is_a_tty) {
|
2021-08-14 18:54:57 +00:00
|
|
|
for (size_t j = nprinted; i != (files.size() - 1) && j < column_width; ++j)
|
2021-04-17 20:43:45 +00:00
|
|
|
printf(" ");
|
|
|
|
}
|
2022-02-19 21:51:39 +00:00
|
|
|
if ((printed_on_row + column_width) >= terminal_columns || flag_force_newline) {
|
2021-04-16 16:56:08 +00:00
|
|
|
printf("\n");
|
|
|
|
printed_on_row = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return printed_on_row;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int do_file_system_object_short(char const* path)
|
2018-11-29 02:45:23 +00:00
|
|
|
{
|
2020-11-10 17:42:41 +00:00
|
|
|
if (flag_list_directories_only) {
|
|
|
|
size_t nprinted = 0;
|
2020-11-11 19:36:16 +00:00
|
|
|
bool status = print_filesystem_object_short(path, path, &nprinted);
|
|
|
|
printf("\n");
|
|
|
|
if (status)
|
2020-11-10 17:42:41 +00:00
|
|
|
return 0;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto flags = Core::DirIterator::SkipDots;
|
|
|
|
if (flag_show_dotfiles)
|
|
|
|
flags = Core::DirIterator::Flags::NoFlags;
|
|
|
|
if (flag_show_almost_all_dotfiles)
|
|
|
|
flags = Core::DirIterator::SkipParentAndBaseDir;
|
|
|
|
|
|
|
|
Core::DirIterator di(path, flags);
|
2019-05-27 07:26:54 +00:00
|
|
|
if (di.has_error()) {
|
2023-03-01 15:55:15 +00:00
|
|
|
auto error = di.error();
|
|
|
|
if (error.code() == ENOTDIR) {
|
2020-02-25 13:49:47 +00:00
|
|
|
size_t nprinted = 0;
|
2019-05-27 12:13:25 +00:00
|
|
|
bool status = print_filesystem_object_short(path, path, &nprinted);
|
2019-05-27 01:41:22 +00:00
|
|
|
printf("\n");
|
2019-05-27 13:04:23 +00:00
|
|
|
if (status)
|
|
|
|
return 0;
|
2019-05-27 12:13:25 +00:00
|
|
|
return 2;
|
2019-05-26 17:52:51 +00:00
|
|
|
}
|
2023-03-01 15:55:15 +00:00
|
|
|
fprintf(stderr, "%s: %s\n", path, strerror(di.error().code()));
|
2019-05-27 12:13:25 +00:00
|
|
|
return 1;
|
2018-11-29 02:45:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-14 18:54:57 +00:00
|
|
|
Vector<FileMetadata> files;
|
2019-12-09 16:45:40 +00:00
|
|
|
size_t longest_name = 0;
|
2019-05-27 07:26:54 +00:00
|
|
|
while (di.has_next()) {
|
2021-08-14 18:54:57 +00:00
|
|
|
FileMetadata metadata;
|
|
|
|
metadata.name = di.next_path();
|
2020-11-10 04:13:46 +00:00
|
|
|
|
2021-08-14 18:54:57 +00:00
|
|
|
if (metadata.name.ends_with('~') && flag_ignore_backups && metadata.name != path)
|
2020-11-10 13:05:45 +00:00
|
|
|
continue;
|
|
|
|
|
2021-08-14 18:54:57 +00:00
|
|
|
StringBuilder builder;
|
2022-07-11 19:53:29 +00:00
|
|
|
builder.append({ path, strlen(path) });
|
2021-08-14 18:54:57 +00:00
|
|
|
builder.append('/');
|
|
|
|
builder.append(metadata.name);
|
2022-12-06 01:12:49 +00:00
|
|
|
metadata.path = builder.to_deprecated_string();
|
2021-08-14 18:54:57 +00:00
|
|
|
VERIFY(!metadata.path.is_null());
|
|
|
|
int rc = lstat(metadata.path.characters(), &metadata.stat);
|
2021-08-18 17:23:59 +00:00
|
|
|
if (rc < 0)
|
2021-08-14 18:54:57 +00:00
|
|
|
perror("lstat");
|
|
|
|
|
|
|
|
files.append(metadata);
|
|
|
|
if (metadata.name.length() > longest_name)
|
|
|
|
longest_name = metadata.name.length();
|
2018-11-29 02:45:23 +00:00
|
|
|
}
|
2021-08-14 18:54:57 +00:00
|
|
|
quick_sort(files, filemetadata_comparator);
|
2018-11-29 02:45:23 +00:00
|
|
|
|
2021-08-14 18:54:57 +00:00
|
|
|
if (print_names(path, longest_name, files))
|
2019-07-21 07:26:47 +00:00
|
|
|
printf("\n");
|
2018-10-24 07:48:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2021-08-14 18:54:57 +00:00
|
|
|
|
|
|
|
bool filemetadata_comparator(FileMetadata& a, FileMetadata& b)
|
|
|
|
{
|
|
|
|
if (flag_sort_by_timestamp && (a.stat.st_mtime != b.stat.st_mtime))
|
|
|
|
return (a.stat.st_mtime > b.stat.st_mtime) ^ flag_reverse_sort;
|
|
|
|
return (a.name < b.name) ^ flag_reverse_sort;
|
|
|
|
}
|