liblinux++

A hosted C++ runtime without any libc.
git clone git://henryandlizzy.uk/liblinux++
Log | Files | Refs

commit 6f3496ae521b5cb620b09de33f5c13030a02a417
parent a1233648a601a398356c9a3c3d9f63ed1a7a7e73
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Thu,  8 Jan 2026 21:05:01 +0000

Warn all the things

Diffstat:
MTupfile | 2+-
Mlinux.hpp | 11+++++------
Mls.cpp | 22+++++++++++-----------
Msleep.cpp | 11++++-------
Msyscalls.h | 2+-
Mtac.cpp | 5++---
6 files changed, 24 insertions(+), 29 deletions(-)

diff --git a/Tupfile b/Tupfile @@ -1,4 +1,4 @@ -CXXFLAGS = -std=c++23 -Werror -g -fno-exceptions -fno-rtti -fno-asynchronous-unwind-tables -fno-builtin -fno-pic -fno-pie -fno-stack-protector -fdiagnostics-color=always +CXXFLAGS = -std=c++23 -Wall -Werror -Wextra -Wpedantic -g -fno-exceptions -fno-rtti -fno-asynchronous-unwind-tables -fno-builtin -fno-pic -fno-pie -fno-stack-protector -fdiagnostics-color=always -nostdinc -nostdlibinc -nostdinc++ LDFLAGS = --gc-sections : |> ./errno.sh > %o |> errno.hpp diff --git a/linux.hpp b/linux.hpp @@ -130,7 +130,7 @@ struct c_str static_assert(arr[N-1] == '\0'); } private: - char const* str; + [[maybe_unused]] char const* str; }; [[noreturn]] @@ -155,14 +155,13 @@ extern syscall_result<file> openat(file fd, c_str name, int flags, int mode) noe extern syscall_result<void> close(file fd) noexcept; -struct linux_dirent64 { +struct [[gnu::packed]] linux_dirent64 { ino64_t d_ino; /* 64-bit inode number */ off64_t d_off; /* Not an offset; see getdents() */ unsigned short d_reclen; /* Size of this dirent */ unsigned char d_type; /* File type */ - char d_name[]; /* Filename (null-terminated) */ }; -extern syscall_result<size_t> getdents64(file dir, linux_dirent64 dirp[], size_t count) noexcept; +extern syscall_result<size_t> getdents64(file dir, span<char> buffer) noexcept; extern syscall_result<void*> mmap(void* addr, size_t len, unsigned long prot, unsigned long flags, file fd, size_t off) noexcept; extern syscall_result<void> munmap(void* addr, size_t len) noexcept; @@ -215,8 +214,8 @@ struct in_addr operator addr_ref() const { return {.fat_ptr = {(char const*)this, sizeof(*this)}}; } private: - uint16_t family; - uint8_t data[14]; + [[maybe_unused]] uint16_t family; + [[maybe_unused]] uint8_t data[14]; }; extern syscall_result<void> bind(file socket, addr_ref addr); diff --git a/ls.cpp b/ls.cpp @@ -14,26 +14,26 @@ int main() auto dir = openat(AT_FDCWD, ".", O_RDONLY, 0); assert(dir); - linux_dirent64 buf[4]; - span<char const> ios[4]; + alignas(linux_dirent64) char buf[256]; + span<char const> ios[sizeof(buf)/sizeof(linux_dirent64)]; for (;;) { unsigned n_io = 0; - auto res = getdents64(*dir, buf, sizeof(buf)); - ssize_t n = *res; + auto res = getdents64(*dir, buf); + size_t n = *res; if (!n) break; - char* const begin = reinterpret_cast<char*>(buf); - - for (ssize_t i = 0; i + sizeof(linux_dirent64) < n;) + for (size_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}; + auto e = reinterpret_cast<linux_dirent64*>(buf + i); + char* const name = buf + i + sizeof(linux_dirent64); + size_t len = strlen(name); + name[len++] = '\n'; + ios[n_io++] = span<char const>{name, len}; i += e->d_reclen; + break; } *write(stdout, ios); } diff --git a/sleep.cpp b/sleep.cpp @@ -2,14 +2,11 @@ bool parse_digit(char c, uint64_t& out) { - switch (c) - { - case '0' ... '9': - out = c - '0'; - return true; - default: + if (c < '0' or '9' < c) return false; - } + + out = c - '0'; + return true; } bool parse_u64(char const* p, uint64_t& out) diff --git a/syscalls.h b/syscalls.h @@ -3,7 +3,7 @@ extern_alias _Z8unlinkat4file5c_str12unlink_flags SYS(unlinkat) extern getdents64 -extern_alias _Z10getdents644fileP14linux_dirent64m +extern_alias _Z10getdents644file4spanIcE SYS(getdents64) extern read diff --git a/tac.cpp b/tac.cpp @@ -37,15 +37,14 @@ int main() while (len == cap) { - auto m = mmap(buf + cap, cap ?: 0x1000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, {}, 0); + auto m = mmap(buf + cap, cap ? cap : 0x1000, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, {}, 0); if (!m) return 1; - cap += cap ?: 0x1000; + cap += cap ? cap : 0x1000; len += rd(stdin, buf + len, buf + cap); } -reverse: char const* end = buf + len; char const* str = end;