Files
ziran/std/stdio_linux.zi
T
waozi dc2b2dc312 Give std's size_t libc parameters pointer-sized types
getcwd, readlink, read, write, bcopy, mempcpy, poll and curl_ws_recv took u64
or s64 for size_t, ssize_t and nfds_t values. On wasm32 that linked with a
function signature mismatch and trapped when called. A check now reads std's
bindings for these functions, and another builds file_tree_linux for wasm32.
2026-11-07 02:51:07 -03:00

52 lines
1.8 KiB
Ziran

// The standard streams of a native Linux program: text to standard output
// or standard error, and the whole of standard input.
//
// if !ok { PrintError("levels/approved.json is missing\n"); return 1 }
// input := ReadStandardInput()
using Vectors :: #import "vec"
#scope_file
libc :: #system_library "libc";
WriteRaw :: (fd: s32, input: *u8, count: usize) -> isize #foreign libc "write";
ReadRaw :: (fd: s32, output: *u8, count: usize) -> isize #foreign libc "read";
ErrnoRaw :: () -> *s32 #foreign libc "__errno_location";
Send :: (fd: s32, text: string) -> bool {
chunk: [65536]u8
written: s64 = 0
while written < text.count {
size: s64 = 0
while size < 65536 && written + size < text.count { chunk[size] = text[written + size]; size += 1 }
sent: s64 = 0
while sent < size {
count := cast(s64)WriteRaw(fd, *chunk[sent], cast(usize)(size - sent))
if count < 0 && <<ErrnoRaw() == 4 { continue }
if count <= 0 { return false }
sent += count
}
written += size
}
return true
}
#scope_export
// Writes TEXT to standard output, unbuffered; false when it could not.
WriteOutput :: (text: string) -> bool { return Send(1, text) }
// Writes TEXT to standard error; false when it could not.
PrintError :: (text: string) -> bool { return Send(2, text) }
// Everything on standard input until its end.
ReadStandardInput :: () -> Text {
bytes: Vec(u8)
chunk: [65536]u8
while true {
count := cast(s64)ReadRaw(0, *chunk[0], 65536)
if count < 0 && <<ErrnoRaw() == 4 { continue }
if count <= 0 { break }
index: s64 = 0
while index < count { VecPush(bytes, chunk[index]); index += 1 }
}
return BuilderFinish(bytes)
}