mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:37:44 +00:00
ls: Add -t and -r flags
-t: Sort by modification time (default is to sort by name) -r: Reverse sort order Note that these only apply when used together with -l (long mode.)
This commit is contained in:
parent
96f9e6a64f
commit
8a3fabffb3
1 changed files with 61 additions and 27 deletions
|
@ -1,5 +1,5 @@
|
||||||
#include <AK/String.h>
|
|
||||||
#include <AK/QuickSort.h>
|
#include <AK/QuickSort.h>
|
||||||
|
#include <AK/String.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/CDirIterator.h>
|
#include <LibCore/CDirIterator.h>
|
||||||
|
@ -25,6 +25,8 @@ static bool flag_show_dotfiles = false;
|
||||||
static bool flag_show_inode = false;
|
static bool flag_show_inode = false;
|
||||||
static bool flag_print_numeric = false;
|
static bool flag_print_numeric = false;
|
||||||
static bool flag_human_readable = false;
|
static bool flag_human_readable = false;
|
||||||
|
static bool flag_sort_by_timestamp = false;
|
||||||
|
static bool flag_reverse_sort = false;
|
||||||
|
|
||||||
static int terminal_rows = 0;
|
static int terminal_rows = 0;
|
||||||
static int terminal_columns = 0;
|
static int terminal_columns = 0;
|
||||||
|
@ -40,7 +42,7 @@ int main(int argc, char** argv)
|
||||||
output_is_terminal = true;
|
output_is_terminal = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char* valid_option_characters = "laiGnh";
|
static const char* valid_option_characters = "ltraiGnh";
|
||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt(argc, argv, valid_option_characters)) != -1) {
|
while ((opt = getopt(argc, argv, valid_option_characters)) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
|
@ -50,6 +52,12 @@ int main(int argc, char** argv)
|
||||||
case 'l':
|
case 'l':
|
||||||
flag_long = true;
|
flag_long = true;
|
||||||
break;
|
break;
|
||||||
|
case 't':
|
||||||
|
flag_sort_by_timestamp = true;
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
flag_reverse_sort = true;
|
||||||
|
break;
|
||||||
case 'G':
|
case 'G':
|
||||||
flag_colorize = false;
|
flag_colorize = false;
|
||||||
break;
|
break;
|
||||||
|
@ -88,11 +96,11 @@ int main(int argc, char** argv)
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
int print_name(struct stat& st, const char* name, const char* path_for_link_resolution = nullptr)
|
int print_name(const struct stat& st, const String& name, const char* path_for_link_resolution = nullptr)
|
||||||
{
|
{
|
||||||
int nprinted = strlen(name);
|
int nprinted = name.length();
|
||||||
if (!flag_colorize || !output_is_terminal) {
|
if (!flag_colorize || !output_is_terminal) {
|
||||||
printf("%s", name);
|
printf("%s", name.characters());
|
||||||
} else {
|
} else {
|
||||||
const char* begin_color = "";
|
const char* begin_color = "";
|
||||||
const char* end_color = "\033[0m";
|
const char* end_color = "\033[0m";
|
||||||
|
@ -107,11 +115,11 @@ int print_name(struct stat& st, const char* name, const char* path_for_link_reso
|
||||||
begin_color = "\033[35;1m";
|
begin_color = "\033[35;1m";
|
||||||
else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
|
else if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
|
||||||
begin_color = "\033[33;1m";
|
begin_color = "\033[33;1m";
|
||||||
printf("%s%s%s", begin_color, name, end_color);
|
printf("%s%s%s", begin_color, name.characters(), end_color);
|
||||||
}
|
}
|
||||||
if (S_ISLNK(st.st_mode)) {
|
if (S_ISLNK(st.st_mode)) {
|
||||||
if (path_for_link_resolution) {
|
if (path_for_link_resolution) {
|
||||||
char linkbuf[256];
|
char linkbuf[PATH_MAX];
|
||||||
ssize_t nread = readlink(path_for_link_resolution, linkbuf, sizeof(linkbuf));
|
ssize_t nread = readlink(path_for_link_resolution, linkbuf, sizeof(linkbuf));
|
||||||
if (nread < 0)
|
if (nread < 0)
|
||||||
perror("readlink failed");
|
perror("readlink failed");
|
||||||
|
@ -147,15 +155,8 @@ static String human_readable_size(size_t size)
|
||||||
return number_string_with_one_decimal((float)size / (float)GB, "G");
|
return number_string_with_one_decimal((float)size / (float)GB, "G");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool print_filesystem_object(const char* path, const char* name)
|
bool print_filesystem_object(const String& path, const String& name, const struct stat& st)
|
||||||
{
|
{
|
||||||
struct stat st;
|
|
||||||
int rc = lstat(path, &st);
|
|
||||||
if (rc == -1) {
|
|
||||||
printf("lstat(%s) failed: %s\n", path, strerror(errno));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (flag_show_inode)
|
if (flag_show_inode)
|
||||||
printf("%08u ", st.st_ino);
|
printf("%08u ", st.st_ino);
|
||||||
|
|
||||||
|
@ -224,7 +225,7 @@ bool print_filesystem_object(const char* path, const char* name)
|
||||||
tm->tm_min,
|
tm->tm_min,
|
||||||
tm->tm_sec);
|
tm->tm_sec);
|
||||||
|
|
||||||
print_name(st, name, path);
|
print_name(st, name, path.characters());
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
return true;
|
return true;
|
||||||
|
@ -235,7 +236,13 @@ int do_file_system_object_long(const char* path)
|
||||||
CDirIterator di(path, !flag_show_dotfiles ? CDirIterator::SkipDots : CDirIterator::Flags::NoFlags);
|
CDirIterator di(path, !flag_show_dotfiles ? CDirIterator::SkipDots : CDirIterator::Flags::NoFlags);
|
||||||
if (di.has_error()) {
|
if (di.has_error()) {
|
||||||
if (di.error() == ENOTDIR) {
|
if (di.error() == ENOTDIR) {
|
||||||
if (print_filesystem_object(path, path))
|
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 0;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
@ -243,20 +250,47 @@ int do_file_system_object_long(const char* path)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<String, 1024> names;
|
struct FileMetadata {
|
||||||
while (di.has_next())
|
String name;
|
||||||
names.append(di.next_path());
|
String path;
|
||||||
quick_sort(names.begin(), names.end(), [](auto& a, auto& b) { return a < b; });
|
struct stat stat;
|
||||||
|
};
|
||||||
|
|
||||||
for (auto& name : names) {
|
Vector<FileMetadata> files;
|
||||||
ASSERT(!name.is_empty());
|
while (di.has_next()) {
|
||||||
if (name[0] == '.' && !flag_show_dotfiles)
|
FileMetadata metadata;
|
||||||
|
metadata.name = di.next_path();
|
||||||
|
ASSERT(!metadata.name.is_empty());
|
||||||
|
if (metadata.name[0] == '.' && !flag_show_dotfiles)
|
||||||
continue;
|
continue;
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
builder.append(path);
|
builder.append(path);
|
||||||
builder.append('/');
|
builder.append('/');
|
||||||
builder.append(name);
|
builder.append(metadata.name);
|
||||||
if (!print_filesystem_object(builder.to_string().characters(), name.characters()))
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
quick_sort(files.begin(), files.end(), [](auto& a, auto& b) {
|
||||||
|
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))
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -291,7 +325,7 @@ int do_file_system_object_short(const char* path)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector<String, 1024> names;
|
Vector<String> names;
|
||||||
int longest_name = 0;
|
int longest_name = 0;
|
||||||
while (di.has_next()) {
|
while (di.has_next()) {
|
||||||
String name = di.next_path();
|
String name = di.next_path();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue