2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-01-11 15:30:17 +03:00
|
|
|
#include <AK/HashMap.h>
|
2020-08-15 21:05:46 +03:00
|
|
|
#include <AK/NumberFormat.h>
|
2019-08-20 22:37:25 +03:00
|
|
|
#include <AK/QuickSort.h>
|
2019-10-19 21:51:21 +03:00
|
|
|
#include <AK/String.h>
|
2019-08-20 22:37:25 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-05-17 14:02:48 +03:00
|
|
|
#include <AK/Utf8View.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <AK/Vector.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-11 22:05:08 +03:00
|
|
|
#include <LibCore/DateTime.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/DirIterator.h>
|
2020-06-16 22:01:50 +03:00
|
|
|
#include <LibCore/File.h>
|
2020-01-16 01:01:38 +03:00
|
|
|
#include <ctype.h>
|
2018-11-29 05:45:23 +03:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2019-02-08 04:38:21 +03:00
|
|
|
#include <fcntl.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2018-11-29 05:45:23 +03:00
|
|
|
#include <sys/ioctl.h>
|
2019-01-31 07:05:57 +03:00
|
|
|
#include <sys/stat.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2018-10-24 10:48:24 +03:00
|
|
|
|
2019-05-27 15:41:59 +03:00
|
|
|
static int do_file_system_object_long(const char* path);
|
|
|
|
static int do_file_system_object_short(const char* path);
|
2018-11-29 05:45:23 +03:00
|
|
|
|
2020-11-11 22:44:30 +03:00
|
|
|
static bool flag_classify = false;
|
2020-06-08 09:36:51 +03:00
|
|
|
static bool flag_colorize = false;
|
2018-11-29 05:45:23 +03:00
|
|
|
static bool flag_long = false;
|
|
|
|
static bool flag_show_dotfiles = false;
|
2020-11-10 07:13:46 +03:00
|
|
|
static bool flag_show_almost_all_dotfiles = false;
|
2020-11-10 16:05:45 +03:00
|
|
|
static bool flag_ignore_backups = false;
|
2020-11-10 20:42:41 +03:00
|
|
|
static bool flag_list_directories_only = false;
|
2019-01-31 19:34:24 +03:00
|
|
|
static bool flag_show_inode = false;
|
2019-06-01 14:23:35 +03:00
|
|
|
static bool flag_print_numeric = false;
|
2020-11-10 16:05:45 +03:00
|
|
|
static bool flag_hide_group = false;
|
2019-09-11 19:59:13 +03:00
|
|
|
static bool flag_human_readable = false;
|
2019-10-19 21:51:21 +03:00
|
|
|
static bool flag_sort_by_timestamp = false;
|
|
|
|
static bool flag_reverse_sort = false;
|
2020-05-09 17:17:47 +03:00
|
|
|
static bool flag_disable_hyperlinks = false;
|
2018-11-17 03:04:00 +03:00
|
|
|
|
2019-12-09 19:45:40 +03:00
|
|
|
static size_t terminal_rows = 0;
|
|
|
|
static size_t terminal_columns = 0;
|
2019-08-20 22:05:05 +03:00
|
|
|
static bool output_is_terminal = false;
|
|
|
|
|
2020-01-11 15:30:17 +03:00
|
|
|
static HashMap<uid_t, String> users;
|
|
|
|
static HashMap<gid_t, String> groups;
|
|
|
|
|
2018-11-09 12:18:04 +03:00
|
|
|
int main(int argc, char** argv)
|
2018-10-24 10:48:24 +03:00
|
|
|
{
|
2020-01-13 03:12:07 +03:00
|
|
|
if (pledge("stdio rpath tty", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-08-20 22:05:05 +03: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;
|
|
|
|
}
|
2020-06-08 09:36:51 +03:00
|
|
|
if (!isatty(STDOUT_FILENO)) {
|
|
|
|
flag_disable_hyperlinks = true;
|
|
|
|
} else {
|
|
|
|
flag_colorize = true;
|
|
|
|
}
|
2019-08-20 22:05:05 +03:00
|
|
|
|
2020-01-13 03:12:07 +03:00
|
|
|
if (pledge("stdio rpath", nullptr) < 0) {
|
|
|
|
perror("pledge");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-27 20:25:36 +03:00
|
|
|
Vector<const char*> paths;
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 18:22:58 +03:00
|
|
|
args_parser.set_general_help("List files in a directory.");
|
2020-01-27 20:25:36 +03:00
|
|
|
args_parser.add_option(flag_show_dotfiles, "Show dotfiles", "all", 'a');
|
2020-11-10 07:13:46 +03:00
|
|
|
args_parser.add_option(flag_show_almost_all_dotfiles, "Do not list implied . and .. directories", nullptr, 'A');
|
2020-11-10 16:05:45 +03:00
|
|
|
args_parser.add_option(flag_ignore_backups, "Do not list implied entries ending with ~", "--ignore-backups", 'B');
|
2020-11-10 20:42:41 +03:00
|
|
|
args_parser.add_option(flag_list_directories_only, "List directories themselves, not their contents", "directory", 'd');
|
2020-01-27 20:25:36 +03: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 22:44:30 +03:00
|
|
|
args_parser.add_option(flag_classify, "Append a file type indicator to entries", "classify", 'F');
|
2020-01-27 20:25:36 +03: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 16:05:45 +03:00
|
|
|
args_parser.add_option(flag_hide_group, "In long format, do not show group information", nullptr, 'o');
|
2020-01-27 20:25:36 +03:00
|
|
|
args_parser.add_option(flag_human_readable, "Print human-readable sizes", "human-readable", 'h');
|
2020-05-09 17:17:47 +03:00
|
|
|
args_parser.add_option(flag_disable_hyperlinks, "Disable hyperlinks", "no-hyperlinks", 'K');
|
2020-02-02 14:34:39 +03:00
|
|
|
args_parser.add_positional_argument(paths, "Directory to list", "path", Core::ArgsParser::Required::No);
|
2020-01-27 20:25:36 +03:00
|
|
|
args_parser.parse(argc, argv);
|
2018-11-29 05:45:23 +03:00
|
|
|
|
2020-11-10 07:13:46 +03:00
|
|
|
if (flag_show_almost_all_dotfiles)
|
|
|
|
flag_show_dotfiles = true;
|
|
|
|
|
2020-01-11 15:30:17 +03: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();
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:49:31 +03:00
|
|
|
auto do_file_system_object = [&](const char* path) {
|
2019-05-27 16:04:23 +03:00
|
|
|
if (flag_long)
|
|
|
|
return do_file_system_object_long(path);
|
|
|
|
return do_file_system_object_short(path);
|
|
|
|
};
|
|
|
|
|
2019-06-22 17:13:47 +03:00
|
|
|
int status = 0;
|
2020-01-27 20:25:36 +03:00
|
|
|
if (paths.is_empty()) {
|
2019-05-27 16:04:23 +03:00
|
|
|
status = do_file_system_object(".");
|
2020-01-27 20:25:36 +03:00
|
|
|
} else if (paths.size() == 1) {
|
|
|
|
status = do_file_system_object(paths[0]);
|
2019-05-27 15:30:09 +03:00
|
|
|
} else {
|
2020-01-27 20:25:36 +03:00
|
|
|
for (auto& path : paths) {
|
|
|
|
status = do_file_system_object(path);
|
2019-05-27 15:30:09 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-27 15:41:59 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2020-08-11 00:48:37 +03:00
|
|
|
static int print_escaped(const char* name)
|
2020-01-16 01:01:38 +03:00
|
|
|
{
|
|
|
|
int printed = 0;
|
|
|
|
|
2020-05-17 14:02:48 +03:00
|
|
|
Utf8View utf8_name(name);
|
|
|
|
if (utf8_name.validate()) {
|
|
|
|
printf("%s", name);
|
2020-10-20 18:47:34 +03:00
|
|
|
return utf8_name.length();
|
2020-05-17 14:02:48 +03:00
|
|
|
}
|
|
|
|
|
2020-01-16 01:01:38 +03:00
|
|
|
for (int i = 0; name[i] != '\0'; i++) {
|
|
|
|
if (isprint(name[i])) {
|
|
|
|
putchar(name[i]);
|
|
|
|
printed++;
|
|
|
|
} else {
|
|
|
|
printed += printf("\\%03d", name[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return printed;
|
|
|
|
}
|
|
|
|
|
2020-05-09 17:17:47 +03:00
|
|
|
static String& hostname()
|
|
|
|
{
|
|
|
|
static String s_hostname;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-11 00:48:37 +03:00
|
|
|
static size_t print_name(const struct stat& st, const String& name, const char* path_for_link_resolution, const char* path_for_hyperlink)
|
2018-11-29 05:45:23 +03:00
|
|
|
{
|
2020-05-09 17:17:47 +03:00
|
|
|
if (!flag_disable_hyperlinks) {
|
2021-01-03 21:14:57 +03:00
|
|
|
auto full_path = Core::File::real_path_for(path_for_hyperlink);
|
|
|
|
if (!full_path.is_null()) {
|
|
|
|
out("\033]8;;file://{}{}\033\\", hostname(), full_path);
|
2020-05-09 17:17:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
size_t nprinted = 0;
|
2020-01-16 01:01:38 +03:00
|
|
|
|
2019-08-20 22:05:05 +03:00
|
|
|
if (!flag_colorize || !output_is_terminal) {
|
2020-01-16 01:01:38 +03:00
|
|
|
nprinted = printf("%s", name.characters());
|
2018-11-29 05:45:23 +03:00
|
|
|
} else {
|
|
|
|
const char* begin_color = "";
|
|
|
|
const char* end_color = "\033[0m";
|
|
|
|
|
2020-01-04 13:38:02 +03:00
|
|
|
if (st.st_mode & S_ISVTX)
|
|
|
|
begin_color = "\033[42;30;1m";
|
2020-01-05 14:49:25 +03:00
|
|
|
else if (st.st_mode & S_ISUID)
|
|
|
|
begin_color = "\033[41;1m";
|
2020-11-02 09:31:17 +03:00
|
|
|
else if (st.st_mode & S_ISGID)
|
|
|
|
begin_color = "\033[43;1m";
|
2020-01-04 13:38:02 +03:00
|
|
|
else if (S_ISLNK(st.st_mode))
|
2018-11-29 05:45:23 +03: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 19:30:58 +03:00
|
|
|
else if (S_ISSOCK(st.st_mode))
|
|
|
|
begin_color = "\033[35;1m";
|
2018-11-29 05:45:23 +03:00
|
|
|
else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
|
|
|
|
begin_color = "\033[33;1m";
|
2020-01-16 01:01:38 +03:00
|
|
|
printf("%s", begin_color);
|
|
|
|
nprinted = print_escaped(name.characters());
|
|
|
|
printf("%s", end_color);
|
2018-11-17 03:04:00 +03:00
|
|
|
}
|
2018-11-29 05:45:23 +03:00
|
|
|
if (S_ISLNK(st.st_mode)) {
|
|
|
|
if (path_for_link_resolution) {
|
2020-06-16 22:01:50 +03:00
|
|
|
auto link_destination = Core::File::read_link(path_for_link_resolution);
|
|
|
|
if (link_destination.is_null()) {
|
|
|
|
perror("readlink");
|
2020-04-14 18:39:56 +03:00
|
|
|
} else {
|
2020-06-16 22:01:50 +03:00
|
|
|
nprinted += printf(" -> ") + print_escaped(link_destination.characters());
|
2020-04-14 18:39:56 +03:00
|
|
|
}
|
2018-11-29 05:45:23 +03:00
|
|
|
} else {
|
2020-11-11 22:44:30 +03:00
|
|
|
if (flag_classify)
|
|
|
|
nprinted += printf("@");
|
2018-11-29 05:45:23 +03:00
|
|
|
}
|
|
|
|
} else if (S_ISDIR(st.st_mode)) {
|
2020-11-11 22:44:30 +03:00
|
|
|
if (flag_classify)
|
|
|
|
nprinted += printf("/");
|
2018-11-29 05:45:23 +03:00
|
|
|
} else if (st.st_mode & 0111) {
|
2020-11-11 22:44:30 +03:00
|
|
|
if (flag_classify)
|
|
|
|
nprinted += printf("*");
|
2018-11-29 05:45:23 +03:00
|
|
|
}
|
2020-05-09 17:17:47 +03:00
|
|
|
|
|
|
|
if (!flag_disable_hyperlinks) {
|
|
|
|
printf("\033]8;;\033\\");
|
|
|
|
}
|
|
|
|
|
2018-11-29 05:45:23 +03:00
|
|
|
return nprinted;
|
2018-11-17 03:04:00 +03:00
|
|
|
}
|
2018-10-28 02:17:48 +03:00
|
|
|
|
2020-08-11 00:48:37 +03:00
|
|
|
static bool print_filesystem_object(const String& path, const String& name, const struct stat& st)
|
2019-06-07 12:49:31 +03:00
|
|
|
{
|
2019-05-27 04:41:22 +03:00
|
|
|
if (flag_show_inode)
|
|
|
|
printf("%08u ", st.st_ino);
|
|
|
|
|
|
|
|
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 12:49:31 +03:00
|
|
|
st.st_mode & S_IWOTH ? 'w' : '-');
|
2019-05-27 04:41:22 +03:00
|
|
|
|
|
|
|
if (st.st_mode & S_ISVTX)
|
|
|
|
printf("t");
|
|
|
|
else
|
|
|
|
printf("%c", st.st_mode & S_IXOTH ? 'x' : '-');
|
|
|
|
|
2021-01-30 01:05:02 +03:00
|
|
|
printf(" %3u", st.st_nlink);
|
2021-01-06 18:29:33 +03:00
|
|
|
|
2020-01-11 15:30:17 +03:00
|
|
|
auto username = users.get(st.st_uid);
|
|
|
|
if (!flag_print_numeric && username.has_value()) {
|
|
|
|
printf(" %7s", username.value().characters());
|
2019-06-01 14:23:35 +03:00
|
|
|
} else {
|
2020-01-09 23:43:21 +03:00
|
|
|
printf(" %7u", st.st_uid);
|
2019-06-01 14:23:35 +03:00
|
|
|
}
|
2020-11-10 16:05:45 +03: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 14:23:35 +03:00
|
|
|
}
|
2019-05-27 04:41:22 +03:00
|
|
|
|
2019-09-11 19:59:13 +03:00
|
|
|
if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
|
2019-10-19 21:52:54 +03:00
|
|
|
printf(" %4u,%4u ", major(st.st_rdev), minor(st.st_rdev));
|
2019-09-11 19:59:13 +03:00
|
|
|
} else {
|
|
|
|
if (flag_human_readable) {
|
|
|
|
printf(" %10s ", human_readable_size((size_t)st.st_size).characters());
|
|
|
|
} else {
|
2020-05-23 16:27:33 +03:00
|
|
|
printf(" %10zd ", st.st_size);
|
2019-09-11 19:59:13 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-27 04:41:22 +03:00
|
|
|
|
2020-02-11 22:05:08 +03:00
|
|
|
printf(" %s ", Core::DateTime::from_timestamp(st.st_mtime).to_string().characters());
|
2019-05-27 04:41:22 +03:00
|
|
|
|
2020-05-10 18:48:41 +03:00
|
|
|
print_name(st, name, path.characters(), path.characters());
|
2019-05-27 04:41:22 +03:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-11 00:48:37 +03:00
|
|
|
static int do_file_system_object_long(const char* path)
|
2018-11-17 03:04:00 +03:00
|
|
|
{
|
2020-11-10 20:42:41 +03:00
|
|
|
if (flag_list_directories_only) {
|
|
|
|
struct stat stat;
|
|
|
|
int rc = lstat(path, &stat);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("lstat");
|
|
|
|
memset(&stat, 0, sizeof(stat));
|
|
|
|
}
|
|
|
|
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 22:37:25 +03:00
|
|
|
if (di.has_error()) {
|
|
|
|
if (di.error() == ENOTDIR) {
|
2019-10-19 21:51:21 +03:00
|
|
|
struct stat stat;
|
|
|
|
int rc = lstat(path, &stat);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("lstat");
|
|
|
|
memset(&stat, 0, sizeof(stat));
|
|
|
|
}
|
|
|
|
if (print_filesystem_object(path, path, stat))
|
2019-05-27 15:13:25 +03:00
|
|
|
return 0;
|
|
|
|
return 2;
|
|
|
|
}
|
2020-01-15 14:07:25 +03:00
|
|
|
fprintf(stderr, "%s: %s\n", path, di.error_string());
|
2018-10-24 10:48:24 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2018-11-29 05:45:23 +03:00
|
|
|
|
2019-10-19 21:51:21 +03:00
|
|
|
struct FileMetadata {
|
|
|
|
String name;
|
|
|
|
String path;
|
|
|
|
struct stat stat;
|
|
|
|
};
|
2019-08-20 22:37:25 +03:00
|
|
|
|
2019-10-19 21:51:21 +03:00
|
|
|
Vector<FileMetadata> files;
|
|
|
|
while (di.has_next()) {
|
|
|
|
FileMetadata metadata;
|
|
|
|
metadata.name = di.next_path();
|
|
|
|
ASSERT(!metadata.name.is_empty());
|
2020-11-10 07:13:46 +03:00
|
|
|
|
2020-11-10 16:05:45 +03:00
|
|
|
if (metadata.name.ends_with('~') && flag_ignore_backups && metadata.name != path)
|
|
|
|
continue;
|
|
|
|
|
2019-08-20 22:37:25 +03:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(path);
|
|
|
|
builder.append('/');
|
2019-10-19 21:51:21 +03:00
|
|
|
builder.append(metadata.name);
|
|
|
|
metadata.path = builder.to_string();
|
|
|
|
ASSERT(!metadata.path.is_null());
|
|
|
|
int rc = lstat(metadata.path.characters(), &metadata.stat);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("lstat");
|
|
|
|
memset(&metadata.stat, 0, sizeof(metadata.stat));
|
|
|
|
}
|
|
|
|
files.append(move(metadata));
|
|
|
|
}
|
|
|
|
|
2020-03-03 18:01:37 +03:00
|
|
|
quick_sort(files, [](auto& a, auto& b) {
|
2019-10-19 21:51:21 +03:00
|
|
|
if (flag_sort_by_timestamp) {
|
|
|
|
if (flag_reverse_sort)
|
|
|
|
return a.stat.st_mtime > b.stat.st_mtime;
|
|
|
|
return a.stat.st_mtime < b.stat.st_mtime;
|
|
|
|
}
|
|
|
|
// Fine, sort by name then!
|
|
|
|
if (flag_reverse_sort)
|
|
|
|
return a.name > b.name;
|
|
|
|
return a.name < b.name;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto& file : files) {
|
|
|
|
if (!print_filesystem_object(file.path, file.name, file.stat))
|
2019-05-27 15:13:25 +03:00
|
|
|
return 2;
|
2018-11-29 05:45:23 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-11 00:48:37 +03:00
|
|
|
static bool print_filesystem_object_short(const char* path, const char* name, size_t* nprinted)
|
2019-06-07 12:49:31 +03:00
|
|
|
{
|
2019-05-26 20:52:51 +03:00
|
|
|
struct stat st;
|
|
|
|
int rc = lstat(path, &st);
|
|
|
|
if (rc == -1) {
|
|
|
|
printf("lstat(%s) failed: %s\n", path, strerror(errno));
|
2019-05-27 04:41:22 +03:00
|
|
|
return false;
|
2019-05-26 20:52:51 +03:00
|
|
|
}
|
|
|
|
|
2020-11-09 05:55:44 +03:00
|
|
|
if (flag_show_inode)
|
|
|
|
printf("%08u ", st.st_ino);
|
|
|
|
|
2020-05-10 18:48:41 +03:00
|
|
|
*nprinted = print_name(st, name, nullptr, path);
|
2019-05-27 04:41:22 +03:00
|
|
|
return true;
|
2019-05-26 20:52:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-27 15:41:59 +03:00
|
|
|
int do_file_system_object_short(const char* path)
|
2018-11-29 05:45:23 +03:00
|
|
|
{
|
2020-11-10 20:42:41 +03:00
|
|
|
if (flag_list_directories_only) {
|
|
|
|
size_t nprinted = 0;
|
2020-11-11 22:36:16 +03:00
|
|
|
bool status = print_filesystem_object_short(path, path, &nprinted);
|
|
|
|
printf("\n");
|
|
|
|
if (status)
|
2020-11-10 20:42:41 +03: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 10:26:54 +03:00
|
|
|
if (di.has_error()) {
|
|
|
|
if (di.error() == ENOTDIR) {
|
2020-02-25 16:49:47 +03:00
|
|
|
size_t nprinted = 0;
|
2019-05-27 15:13:25 +03:00
|
|
|
bool status = print_filesystem_object_short(path, path, &nprinted);
|
2019-05-27 04:41:22 +03:00
|
|
|
printf("\n");
|
2019-05-27 16:04:23 +03:00
|
|
|
if (status)
|
|
|
|
return 0;
|
2019-05-27 15:13:25 +03:00
|
|
|
return 2;
|
2019-05-26 20:52:51 +03:00
|
|
|
}
|
2020-01-15 14:07:25 +03:00
|
|
|
fprintf(stderr, "%s: %s\n", path, di.error_string());
|
2019-05-27 15:13:25 +03:00
|
|
|
return 1;
|
2018-11-29 05:45:23 +03:00
|
|
|
}
|
|
|
|
|
2019-10-19 21:51:21 +03:00
|
|
|
Vector<String> names;
|
2019-12-09 19:45:40 +03:00
|
|
|
size_t longest_name = 0;
|
2019-05-27 10:26:54 +03:00
|
|
|
while (di.has_next()) {
|
|
|
|
String name = di.next_path();
|
2020-11-10 07:13:46 +03:00
|
|
|
|
2020-11-10 16:05:45 +03:00
|
|
|
if (name.ends_with('~') && flag_ignore_backups && name != path)
|
|
|
|
continue;
|
|
|
|
|
2019-05-27 10:26:54 +03:00
|
|
|
names.append(name);
|
2018-11-29 05:45:23 +03:00
|
|
|
if (names.last().length() > longest_name)
|
2019-05-27 10:26:54 +03:00
|
|
|
longest_name = name.length();
|
2018-11-29 05:45:23 +03:00
|
|
|
}
|
2020-03-03 18:01:37 +03:00
|
|
|
quick_sort(names);
|
2018-11-29 05:45:23 +03:00
|
|
|
|
2019-12-09 19:45:40 +03:00
|
|
|
size_t printed_on_row = 0;
|
2020-02-25 16:49:47 +03:00
|
|
|
size_t nprinted = 0;
|
|
|
|
for (size_t i = 0; i < names.size(); ++i) {
|
2018-11-29 05:45:23 +03:00
|
|
|
auto& name = names[i];
|
2019-08-20 22:37:25 +03:00
|
|
|
StringBuilder builder;
|
|
|
|
builder.append(path);
|
|
|
|
builder.append('/');
|
|
|
|
builder.append(name);
|
|
|
|
if (!print_filesystem_object_short(builder.to_string().characters(), name.characters(), &nprinted))
|
2019-05-27 04:41:22 +03:00
|
|
|
return 2;
|
2019-08-20 20:59:55 +03:00
|
|
|
int offset = 0;
|
2019-08-20 22:05:05 +03:00
|
|
|
if (terminal_columns > longest_name)
|
|
|
|
offset = terminal_columns % longest_name / (terminal_columns / longest_name);
|
|
|
|
|
|
|
|
// The offset must be at least 2 because:
|
2020-01-13 03:12:07 +03:00
|
|
|
// - With each file an additional char is printed e.g. '@','*'.
|
2019-08-20 22:05:05 +03:00
|
|
|
// - Each filename must be separated by a space.
|
2020-08-28 13:56:38 +03:00
|
|
|
size_t column_width = longest_name + max(offset, 2);
|
2018-11-29 05:45:23 +03:00
|
|
|
printed_on_row += column_width;
|
2018-10-28 02:17:48 +03:00
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
for (size_t j = nprinted; i != (names.size() - 1) && j < column_width; ++j)
|
2018-11-29 05:45:23 +03:00
|
|
|
printf(" ");
|
2019-08-20 22:05:05 +03:00
|
|
|
if ((printed_on_row + column_width) >= terminal_columns) {
|
2019-08-19 17:37:21 +03:00
|
|
|
printf("\n");
|
2018-11-29 05:45:23 +03:00
|
|
|
printed_on_row = 0;
|
2018-10-28 16:11:51 +03:00
|
|
|
}
|
2018-10-24 13:43:52 +03:00
|
|
|
}
|
2019-07-21 10:26:47 +03:00
|
|
|
if (printed_on_row)
|
|
|
|
printf("\n");
|
2018-10-24 10:48:24 +03:00
|
|
|
return 0;
|
|
|
|
}
|