commit 3422713679c8528a5feedb1b170c1a55adb1ab0f
parent dc63fe778af6488f6b12a78b6b41a4beaaa07663
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Tue, 23 Dec 2025 15:30:54 +0000
convert paste to .cpp and handle pointer syscall results
Diffstat:
| M | linux.hpp | | | 5 | ++++- |
| D | paste.c | | | 57 | --------------------------------------------------------- |
| A | paste.cpp | | | 60 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 64 insertions(+), 58 deletions(-)
diff --git a/linux.hpp b/linux.hpp
@@ -89,7 +89,10 @@ struct syscall_result
{
if (!*this)
__builtin_trap();
- return static_cast<T>(val);
+ if constexpr (__is_pointer(T))
+ return reinterpret_cast<T>(val);
+ else
+ return static_cast<T>(val);
}
errno_t err() const
{
diff --git a/paste.c b/paste.c
@@ -1,57 +0,0 @@
-#include "linux.h"
-
-struct input
-{
- int fd;
- unsigned short start, end;
- char buf[0x1000 - 8];
-};
-
-extern struct input mappable[];
-static int n_inputs;
-static char getc(struct input* in)
-{
- if (in->start == in->end)
- {
- in->start = 0;
- in->end = 0;
- int n = read(in->fd, in->buf, 0x1000-8);
- if (n <= 0)
- return 0;
- in->end = n;
- }
- return in->buf[in->start++];
-}
-
-int main(int argc, char* argv[])
-{
- for (int i = 1; i < argc; ++i)
- {
- int fd = openat(AT_FDCWD, argv[i], 0, 0);
- if (get_errno(fd))
- return 1;
- intptr_t m = mmap(mappable + n_inputs++, sizeof(struct input), PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
- if (get_errno(m))
- return 2;
- ((struct input*)m)->fd = fd;
- }
-
- for (;;)
- {
- for (int i = 0; i < n_inputs; ++i)
- {
- struct input* in = mappable + i;
-
- for (;;)
- {
- char c = getc(in);
- if (!c)
- return 0;
- if (c == '\n')
- break;
- write(1, &c, 1);
- }
- write(1, i + 1 == n_inputs ? "\n" : "\t", 1);
- }
- }
-}
diff --git a/paste.cpp b/paste.cpp
@@ -0,0 +1,60 @@
+#include "linux.hpp"
+
+struct input
+{
+ file fd;
+ unsigned short start, end;
+ char buf[0x1000 - 8];
+};
+
+extern struct input mappable[];
+static int n_inputs;
+static char getc(struct input* in)
+{
+ if (in->start == in->end)
+ {
+ in->start = 0;
+ in->end = 0;
+ auto res = read(in->fd, in->buf, 0x1000-8);
+ if (!res)
+ exit(1);
+ auto n = *res;
+ if (n <= 0)
+ return 0;
+ in->end = n;
+ }
+ return in->buf[in->start++];
+}
+
+int main(int argc, char* argv[])
+{
+ for (int i = 1; i < argc; ++i)
+ {
+ auto fd = openat(AT_FDCWD, argv[i], 0, 0);
+ if (!fd)
+ return 1;
+ auto m = mmap(mappable + n_inputs++, sizeof(struct input), PROT_READ | PROT_WRITE, MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, {}, 0);
+ if (!m)
+ return 2;
+ static_cast<input*>(*m)->fd = *fd;
+ }
+
+ for (;;)
+ {
+ for (int i = 0; i < n_inputs; ++i)
+ {
+ input* in = mappable + i;
+
+ for (;;)
+ {
+ char c = getc(in);
+ if (!c)
+ return 0;
+ if (c == '\n')
+ break;
+ write(stdout, &c, 1);
+ }
+ write(stdout, i + 1 == n_inputs ? "\n" : "\t", 1);
+ }
+ }
+}