2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2023-07-15 10:33:22 +00:00
|
|
|
* Copyright (c) 2020, Shannon Booth <shannon@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-03-09 07:40:41 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-11 19:05:08 +00:00
|
|
|
#include <LibCore/DateTime.h>
|
2021-12-02 22:22:51 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-02-27 20:45:06 +00:00
|
|
|
#include <grp.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <pwd.h>
|
2021-11-26 15:55:45 +00:00
|
|
|
#include <sys/sysmacros.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <time.h>
|
2019-02-27 20:45:06 +00:00
|
|
|
|
2021-12-02 22:22:51 +00:00
|
|
|
static ErrorOr<int> stat(StringView file, bool should_follow_links)
|
2019-02-27 20:45:06 +00:00
|
|
|
{
|
2021-12-02 22:22:51 +00:00
|
|
|
auto st = TRY(should_follow_links ? Core::System::stat(file) : Core::System::lstat(file));
|
|
|
|
outln(" File: {}", file);
|
2023-09-30 05:15:50 +00:00
|
|
|
outln(" Device: {}", st.st_dev);
|
2021-12-02 22:22:51 +00:00
|
|
|
outln(" Inode: {}", st.st_ino);
|
2019-02-27 20:45:06 +00:00
|
|
|
if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
|
2021-12-02 22:22:51 +00:00
|
|
|
outln(" Device: {},{}", major(st.st_rdev), minor(st.st_rdev));
|
2019-02-27 20:45:06 +00:00
|
|
|
else
|
2021-12-02 22:22:51 +00:00
|
|
|
outln(" Size: {}", st.st_size);
|
|
|
|
outln(" Links: {}", st.st_nlink);
|
|
|
|
outln(" Blocks: {}", st.st_blocks);
|
|
|
|
out(" UID: {}", st.st_uid);
|
2019-02-27 20:45:06 +00:00
|
|
|
if (auto* pwd = getpwuid(st.st_uid)) {
|
2021-12-02 22:22:51 +00:00
|
|
|
out(" ({})", pwd->pw_name);
|
2019-02-27 20:45:06 +00:00
|
|
|
}
|
2021-12-02 22:22:51 +00:00
|
|
|
outln("");
|
|
|
|
out(" GID: {}", st.st_gid);
|
2019-02-27 20:45:06 +00:00
|
|
|
if (auto* grp = getgrgid(st.st_gid)) {
|
2021-12-02 22:22:51 +00:00
|
|
|
out(" ({})", grp->gr_name);
|
2019-02-27 20:45:06 +00:00
|
|
|
}
|
2021-12-02 22:22:51 +00:00
|
|
|
outln("");
|
|
|
|
out(" Mode: ({:o}/", st.st_mode);
|
2019-02-27 20:45:06 +00:00
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode))
|
2021-12-02 22:22:51 +00:00
|
|
|
out("d");
|
2019-02-27 20:45:06 +00:00
|
|
|
else if (S_ISLNK(st.st_mode))
|
2021-12-02 22:22:51 +00:00
|
|
|
out("l");
|
2019-02-27 20:45:06 +00:00
|
|
|
else if (S_ISBLK(st.st_mode))
|
2021-12-02 22:22:51 +00:00
|
|
|
out("b");
|
2019-02-27 20:45:06 +00:00
|
|
|
else if (S_ISCHR(st.st_mode))
|
2021-12-02 22:22:51 +00:00
|
|
|
out("c");
|
2019-02-27 20:45:06 +00:00
|
|
|
else if (S_ISFIFO(st.st_mode))
|
2021-12-02 22:22:51 +00:00
|
|
|
out("f");
|
2019-02-27 20:45:06 +00:00
|
|
|
else if (S_ISSOCK(st.st_mode))
|
2021-12-02 22:22:51 +00:00
|
|
|
out("s");
|
2019-02-27 20:45:06 +00:00
|
|
|
else if (S_ISREG(st.st_mode))
|
2021-12-02 22:22:51 +00:00
|
|
|
out("-");
|
2019-02-27 20:45:06 +00:00
|
|
|
else
|
2021-12-02 22:22:51 +00:00
|
|
|
out("?");
|
2019-02-27 20:45:06 +00:00
|
|
|
|
2021-12-02 22:22:51 +00:00
|
|
|
out("{:c}{:c}{:c}{:c}{:c}{:c}{:c}{:c}",
|
2019-02-27 20:45:06 +00:00
|
|
|
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-02-27 20:45:06 +00:00
|
|
|
|
|
|
|
if (st.st_mode & S_ISVTX)
|
2021-12-02 22:22:51 +00:00
|
|
|
out("t");
|
2019-02-27 20:45:06 +00:00
|
|
|
else
|
2021-12-02 22:22:51 +00:00
|
|
|
out("{:c}", st.st_mode & S_IXOTH ? 'x' : '-');
|
2019-02-27 20:45:06 +00:00
|
|
|
|
2021-12-02 22:22:51 +00:00
|
|
|
outln(")");
|
2019-02-27 20:45:06 +00:00
|
|
|
|
2022-11-22 19:44:12 +00:00
|
|
|
auto print_time = [](timespec t) {
|
2022-12-06 01:12:49 +00:00
|
|
|
outln("{}.{:09}", Core::DateTime::from_timestamp(t.tv_sec).to_deprecated_string(), t.tv_nsec);
|
2019-02-27 20:45:06 +00:00
|
|
|
};
|
|
|
|
|
2021-12-02 22:22:51 +00:00
|
|
|
out("Accessed: ");
|
2022-11-22 19:44:12 +00:00
|
|
|
print_time(st.st_atim);
|
2021-12-02 22:22:51 +00:00
|
|
|
out("Modified: ");
|
2022-11-22 19:44:12 +00:00
|
|
|
print_time(st.st_mtim);
|
2021-12-02 22:22:51 +00:00
|
|
|
out(" Changed: ");
|
2022-11-22 19:44:12 +00:00
|
|
|
print_time(st.st_ctim);
|
2019-02-27 20:45:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-09 07:40:41 +00:00
|
|
|
|
2021-12-02 22:22:51 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-03-09 07:40:41 +00:00
|
|
|
{
|
2022-04-03 23:16:06 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-03-09 07:40:41 +00:00
|
|
|
|
|
|
|
bool should_follow_links = false;
|
2021-12-02 22:22:51 +00:00
|
|
|
Vector<StringView> files;
|
2020-03-09 07:40:41 +00:00
|
|
|
|
|
|
|
auto args_parser = Core::ArgsParser();
|
|
|
|
args_parser.add_option(should_follow_links, "Follow links to files", nullptr, 'L');
|
|
|
|
args_parser.add_positional_argument(files, "File(s) to stat", "file", Core::ArgsParser::Required::Yes);
|
2021-12-02 22:22:51 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-03-09 07:40:41 +00:00
|
|
|
|
2021-12-02 22:22:51 +00:00
|
|
|
bool had_error = false;
|
|
|
|
for (auto& file : files) {
|
2022-01-04 20:17:36 +00:00
|
|
|
auto r = stat(file, should_follow_links);
|
|
|
|
if (r.is_error()) {
|
|
|
|
had_error = true;
|
|
|
|
warnln("stat: cannot stat '{}': {}", file, strerror(r.error().code()));
|
|
|
|
}
|
2021-12-02 22:22:51 +00:00
|
|
|
}
|
2020-03-09 07:40:41 +00:00
|
|
|
|
2021-12-02 22:22:51 +00:00
|
|
|
return had_error;
|
2020-03-09 07:40:41 +00:00
|
|
|
}
|