code wiki / _hdl_build / nx_gate_read.nx

nx_gate_read.nx source

↩ module page · 9 lines · 595 B

1// nx_gate_read.nx -- Reads bytes from a file into a buffer using system calls and returns the total number of bytes read. 2import "nx_syscalls.nx" 3// consolidated file-read helper (funcmine g_read group, byte-identical copies -> 1 lib; rule 15 DRY). Behaviorally-verified migration. 4func g_read(path: *u8, buf: *u8, cap: i64) -> i64 { 5 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 - 1 } 6 var tot: i64 = 0; var r: i64 = 1 7 while r > 0 { let dst: *u8 = ((buf as i64) + tot) as *u8; r = sys_read(fd, dst, cap - tot); if r > 0 { tot = tot + r } } 8 sys_close(fd); return tot 9}