mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:27:45 +00:00
ls: Sort the output alphabetically
Also use StringBuilder to construct the full relative paths, to get rid of an unnecessary and suspicious-looking buffer.
This commit is contained in:
parent
029786e6b5
commit
9731bff44c
1 changed files with 25 additions and 14 deletions
|
@ -1,4 +1,6 @@
|
||||||
#include <AK/AKString.h>
|
#include <AK/AKString.h>
|
||||||
|
#include <AK/QuickSort.h>
|
||||||
|
#include <AK/StringBuilder.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/CDirIterator.h>
|
#include <LibCore/CDirIterator.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
@ -202,26 +204,33 @@ bool print_filesystem_object(const char* path, const char* name)
|
||||||
|
|
||||||
int do_file_system_object_long(const char* path)
|
int do_file_system_object_long(const char* path)
|
||||||
{
|
{
|
||||||
DIR* dirp = opendir(path);
|
CDirIterator di(path, !flag_show_dotfiles ? CDirIterator::SkipDots : CDirIterator::Flags::NoFlags);
|
||||||
if (!dirp) {
|
if (di.has_error()) {
|
||||||
if (errno == ENOTDIR) {
|
if (di.error() == ENOTDIR) {
|
||||||
if (print_filesystem_object(path, path))
|
if (print_filesystem_object(path, path))
|
||||||
return 0;
|
return 0;
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
perror("opendir");
|
fprintf(stderr, "CDirIterator: %s\n", di.error_string());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
char pathbuf[PATH_MAX];
|
|
||||||
|
|
||||||
while (auto* de = readdir(dirp)) {
|
Vector<String, 1024> names;
|
||||||
if (de->d_name[0] == '.' && !flag_show_dotfiles)
|
while (di.has_next())
|
||||||
|
names.append(di.next_path());
|
||||||
|
quick_sort(names.begin(), names.end(), [](auto& a, auto& b) { return a < b; });
|
||||||
|
|
||||||
|
for (auto& name : names) {
|
||||||
|
ASSERT(!name.is_empty());
|
||||||
|
if (name[0] == '.' && !flag_show_dotfiles)
|
||||||
continue;
|
continue;
|
||||||
sprintf(pathbuf, "%s/%s", path, de->d_name);
|
StringBuilder builder;
|
||||||
if (!print_filesystem_object(pathbuf, de->d_name))
|
builder.append(path);
|
||||||
|
builder.append('/');
|
||||||
|
builder.append(name);
|
||||||
|
if (!print_filesystem_object(builder.to_string().characters(), name.characters()))
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
closedir(dirp);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,15 +271,17 @@ int do_file_system_object_short(const char* path)
|
||||||
if (names.last().length() > longest_name)
|
if (names.last().length() > longest_name)
|
||||||
longest_name = name.length();
|
longest_name = name.length();
|
||||||
}
|
}
|
||||||
|
quick_sort(names.begin(), names.end(), [](auto& a, auto& b) { return a < b; });
|
||||||
|
|
||||||
int printed_on_row = 0;
|
int printed_on_row = 0;
|
||||||
int nprinted;
|
int nprinted;
|
||||||
for (int i = 0; i < names.size(); ++i) {
|
for (int i = 0; i < names.size(); ++i) {
|
||||||
auto& name = names[i];
|
auto& name = names[i];
|
||||||
char pathbuf[256];
|
StringBuilder builder;
|
||||||
sprintf(pathbuf, "%s/%s", path, name.characters());
|
builder.append(path);
|
||||||
|
builder.append('/');
|
||||||
if (!print_filesystem_object_short(pathbuf, name.characters(), &nprinted))
|
builder.append(name);
|
||||||
|
if (!print_filesystem_object_short(builder.to_string().characters(), name.characters(), &nprinted))
|
||||||
return 2;
|
return 2;
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
if (terminal_columns > longest_name)
|
if (terminal_columns > longest_name)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue