2026-01-08 13:15:27 +01:00
|
|
|
import ../stdlib/stdlib.sl
|
|
|
|
|
import ../stdlib/io.sl
|
2025-12-18 11:32:35 +01:00
|
|
|
|
|
|
|
|
: main
|
2026-01-08 13:15:27 +01:00
|
|
|
"/tmp/l2_read_file_test.txt"
|
|
|
|
|
"read_file works\n"
|
|
|
|
|
write_file drop
|
|
|
|
|
|
|
|
|
|
"/tmp/l2_read_file_test.txt" # (addr len)
|
2025-12-19 20:23:26 +01:00
|
|
|
read_file # (file_addr file_len)
|
2025-12-18 11:32:35 +01:00
|
|
|
dup 0 > if # if file_len > 0, success
|
|
|
|
|
write_buf # print file contents (file_len file_addr)
|
|
|
|
|
0
|
|
|
|
|
exit
|
2026-01-03 09:04:50 +00:00
|
|
|
end
|
2025-12-18 11:32:35 +01:00
|
|
|
dup -2 == if # open() failed
|
|
|
|
|
drop
|
|
|
|
|
"open() failed: errno=" puts
|
2025-12-19 20:23:26 +01:00
|
|
|
swap puti cr
|
2025-12-18 11:32:35 +01:00
|
|
|
exit
|
2026-01-03 09:04:50 +00:00
|
|
|
end
|
2025-12-18 11:32:35 +01:00
|
|
|
dup -1 == if # fstat() failed
|
|
|
|
|
drop
|
|
|
|
|
"fstat() failed: errno=" puts
|
2025-12-19 20:23:26 +01:00
|
|
|
swap puti cr
|
2025-12-18 11:32:35 +01:00
|
|
|
exit
|
2026-01-03 09:04:50 +00:00
|
|
|
end
|
2025-12-18 11:32:35 +01:00
|
|
|
dup -3 == if # mmap() failed
|
|
|
|
|
drop
|
|
|
|
|
"mmap() failed" puts
|
|
|
|
|
exit
|
2026-01-03 09:04:50 +00:00
|
|
|
end
|
2025-12-18 11:32:35 +01:00
|
|
|
"unknown read_file failure" puts
|
|
|
|
|
dup # file_len file_len file_addr
|
|
|
|
|
exit # Exit with returned file_len as the program exit code (debug)
|
|
|
|
|
;
|