commit 2abb614c632657f830b07459434c8e9bb231141c
parent 4926e536c96ec95b47c4457adc16a182842ce862
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Tue, 23 Dec 2025 15:47:26 +0000
Convert ls to .cpp
Diffstat:
| M | linux.hpp | | | 9 | +++++---- |
| D | ls.c | | | 42 | ------------------------------------------ |
| A | ls.cpp | | | 42 | ++++++++++++++++++++++++++++++++++++++++++ |
| M | x86_64.s | | | 3 | ++- |
4 files changed, 49 insertions(+), 47 deletions(-)
diff --git a/linux.hpp b/linux.hpp
@@ -44,9 +44,7 @@ enum {
template <typename T>
struct span
{
- T* data;
- size_t len;
-
+ span() = default;
inline span(T* p, size_t n)
: data{p}
, len{n}
@@ -56,6 +54,9 @@ struct span
inline span(T (&arr)[N])
: span{arr, N}
{}
+private:
+ T* data = nullptr;
+ size_t len = 0;
};
struct file
@@ -150,7 +151,7 @@ struct linux_dirent64 {
unsigned char d_type; /* File type */
char d_name[]; /* Filename (null-terminated) */
};
-extern syscall_result<ssize_t> getdents64(file dir, linux_dirent64 dirp[], size_t count);
+extern syscall_result<size_t> getdents64(file dir, linux_dirent64 dirp[], size_t count);
extern syscall_result<void*> mmap(void* addr, unsigned long len, unsigned long prot, unsigned long flags, file fd, unsigned long off);
diff --git a/ls.c b/ls.c
@@ -1,42 +0,0 @@
-#include "linux.h"
-
-#define assert(x) if (!(x)) __builtin_trap()
-
-size_t strlen(char const* p)
-{
- char const* const begin = p;
- while (*p)
- ++p;
-
- return p - begin;
-}
-
-int main()
-{
- int fd = openat(AT_FDCWD, ".", O_RDONLY, 0);
- assert(fd >= 0);
-
- struct linux_dirent64 buf[4];
- struct iovecc ios[4];
- for (;;)
- {
- unsigned n_io = 0;
- ssize_t n = getdents64(fd, buf, sizeof(buf));
- assert (n >= 0);
-
- if (!n)
- break;
-
- char* const begin = (void*)buf;
-
- for (ssize_t i = 0; i + sizeof(struct linux_dirent64) < n;)
- {
- struct linux_dirent64* e = (void*)(begin + i);
- size_t len = strlen(e->d_name);
- e->d_name[len++] = '\n';
- ios[n_io++] = (struct iovecc){e->d_name, len};
- i += e->d_reclen;
- }
- writev(1, ios, n_io);
- }
-}
diff --git a/ls.cpp b/ls.cpp
@@ -0,0 +1,42 @@
+#include "linux.hpp"
+
+#define assert(x) if (!(x)) __builtin_trap()
+
+size_t strlen(char const* p)
+{
+ char const* const begin = p;
+ while (*p)
+ ++p;
+
+ return p - begin;
+}
+
+int main()
+{
+ auto dir = openat(AT_FDCWD, ".", O_RDONLY, 0);
+ assert(dir);
+
+ linux_dirent64 buf[4];
+ span<char const> ios[4];
+ for (;;)
+ {
+ unsigned n_io = 0;
+ auto res = getdents64(*dir, buf, sizeof(buf));
+ ssize_t n = *res;
+
+ if (!n)
+ break;
+
+ char* const begin = reinterpret_cast<char*>(buf);
+
+ for (ssize_t i = 0; i + sizeof(linux_dirent64) < n;)
+ {
+ auto e = reinterpret_cast<linux_dirent64*>(begin + i);
+ size_t len = strlen(e->d_name);
+ e->d_name[len++] = '\n';
+ ios[n_io++] = span<char const>{e->d_name, len};
+ i += e->d_reclen;
+ }
+ write(stdout, ios);
+ }
+}
diff --git a/x86_64.s b/x86_64.s
@@ -62,7 +62,7 @@ extern_alias _Z4mmapPvmmm4filem
_syscall 9 syscall6
extern writev
-extern_alias _Z5writei4spanIKS_IKcEE
+extern_alias _Z5write4file4spanIKS0_IKcEE
_syscall 20 syscall3
extern nanosleep
@@ -74,6 +74,7 @@ extern_alias _Z4exiti
_syscall 60 syscall3
extern getdents64
+extern_alias _Z10getdents644fileP14linux_dirent64y
_syscall 217 syscall3
extern openat