Compare commits
No commits in common. "376247ba8afcb1db3fd19e853f048d48fff9652c" and "48df90e63652b1e4ae5c3b6ef277ac6677d20d8d" have entirely different histories.
376247ba8a
...
48df90e636
54
apps/ls.cpp
54
apps/ls.cpp
@ -1,18 +1,12 @@
|
|||||||
#include <os/ArgumentParser.h>
|
#include <os/ArgumentParser.h>
|
||||||
#include <os/Directory.h>
|
#include <os/Directory.h>
|
||||||
#include <os/File.h>
|
#include <os/File.h>
|
||||||
#include <os/FileSystem.h>
|
|
||||||
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
|
|
||||||
Result<int> luna_main(int argc, char** argv)
|
Result<int> luna_main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
StringView pathname;
|
StringView pathname;
|
||||||
bool show_all { false };
|
bool show_all { false };
|
||||||
bool show_almost_all { false };
|
bool show_almost_all { false };
|
||||||
bool long_list { false };
|
|
||||||
|
|
||||||
os::ArgumentParser parser;
|
os::ArgumentParser parser;
|
||||||
parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv);
|
parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv);
|
||||||
@ -20,51 +14,19 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
parser.add_positional_argument(pathname, "directory"_sv, "."_sv);
|
parser.add_positional_argument(pathname, "directory"_sv, "."_sv);
|
||||||
parser.add_switch_argument(show_all, 'a', "all"_sv, "also list hidden files (whose filename begins with a dot)"_sv);
|
parser.add_switch_argument(show_all, 'a', "all"_sv, "also list hidden files (whose filename begins with a dot)"_sv);
|
||||||
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv, "list all files except '.' and '..'"_sv);
|
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv, "list all files except '.' and '..'"_sv);
|
||||||
parser.add_switch_argument(long_list, 'l', ""_sv, "use a long listing format"_sv);
|
|
||||||
parser.parse(argc, argv);
|
parser.parse(argc, argv);
|
||||||
|
|
||||||
Vector<String> files;
|
auto dir = TRY(os::Directory::open(pathname));
|
||||||
int dirfd = AT_FDCWD;
|
|
||||||
SharedPtr<os::Directory> dir;
|
|
||||||
|
|
||||||
if (os::FileSystem::is_directory(pathname))
|
auto filter = os::Directory::Filter::Hidden;
|
||||||
{
|
if (show_almost_all) filter = os::Directory::Filter::ParentAndBase;
|
||||||
dir = TRY(os::Directory::open(pathname));
|
else if (show_all)
|
||||||
dirfd = dir->fd();
|
filter = os::Directory::Filter::None;
|
||||||
|
|
||||||
auto filter = os::Directory::Filter::Hidden;
|
auto files = TRY(dir->list(filter));
|
||||||
if (show_almost_all) filter = os::Directory::Filter::ParentAndBase;
|
|
||||||
else if (show_all)
|
|
||||||
filter = os::Directory::Filter::None;
|
|
||||||
|
|
||||||
files = TRY(dir->list(filter));
|
auto list = TRY(String::join(files, " "_sv));
|
||||||
}
|
os::println("%s", list.chars());
|
||||||
else
|
|
||||||
{
|
|
||||||
auto str = TRY(String::from_string_view(pathname));
|
|
||||||
TRY(files.try_append(move(str)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!long_list)
|
|
||||||
{
|
|
||||||
auto list = TRY(String::join(files, " "_sv));
|
|
||||||
os::println("%s", list.chars());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (const auto& file : files)
|
|
||||||
{
|
|
||||||
struct stat st;
|
|
||||||
if (fstatat(dirfd, file.chars(), &st, 0) < 0)
|
|
||||||
{
|
|
||||||
perror(file.chars());
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
os::println("%6o %u %4u %4u %10lu %s", st.st_mode, st.st_nlink, st.st_uid, st.st_gid, st.st_size,
|
|
||||||
file.chars());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,9 @@
|
|||||||
#define S_IFDIR 040000
|
#define S_IFDIR 040000
|
||||||
#define S_IFCHR 050000
|
#define S_IFCHR 050000
|
||||||
|
|
||||||
#define __CHECK_TYPE(mode, type) (((mode)&S_IFMT) == type)
|
#define S_ISREG(mode) ((mode)&S_IFMT == S_IFREG)
|
||||||
|
#define S_ISDIR(mode) ((mode)&S_IFMT == S_IFDIR)
|
||||||
#define S_ISREG(mode) __CHECK_TYPE(mode, S_IFREG)
|
#define S_ISCHR(mode) ((mode)&S_IFMT == S_IFCHR)
|
||||||
#define S_ISDIR(mode) __CHECK_TYPE(mode, S_IFDIR)
|
|
||||||
#define S_ISCHR(mode) __CHECK_TYPE(mode, S_IFCHR)
|
|
||||||
|
|
||||||
#define S_IRWXU 0700
|
#define S_IRWXU 0700
|
||||||
#define S_IRUSR 0400
|
#define S_IRUSR 0400
|
||||||
|
@ -29,9 +29,6 @@ extern "C"
|
|||||||
/* Retrieve information about a file. */
|
/* Retrieve information about a file. */
|
||||||
int fstat(int fd, struct stat* st);
|
int fstat(int fd, struct stat* st);
|
||||||
|
|
||||||
/* Retrieve information about a file. */
|
|
||||||
int fstatat(int dirfd, const char* path, struct stat* st, int flags);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -29,10 +29,4 @@ extern "C"
|
|||||||
long rc = syscall(SYS_fstatat, fd, "", st, AT_EMPTY_PATH);
|
long rc = syscall(SYS_fstatat, fd, "", st, AT_EMPTY_PATH);
|
||||||
__errno_return(rc, int);
|
__errno_return(rc, int);
|
||||||
}
|
}
|
||||||
|
|
||||||
int fstatat(int dirfd, const char* path, struct stat* st, int flags)
|
|
||||||
{
|
|
||||||
long rc = syscall(SYS_fstatat, dirfd, path, st, flags);
|
|
||||||
__errno_return(rc, int);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -41,10 +41,6 @@ class String
|
|||||||
static Result<String> vformat(StringView fmt, va_list ap);
|
static Result<String> vformat(StringView fmt, va_list ap);
|
||||||
|
|
||||||
static Result<String> from_cstring(const char* str);
|
static Result<String> from_cstring(const char* str);
|
||||||
static Result<String> from_string_view(StringView str)
|
|
||||||
{
|
|
||||||
return from_cstring(str.chars());
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* chars() const
|
const char* chars() const
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user