-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.sid
More file actions
50 lines (40 loc) · 1.56 KB
/
Copy pathtest.sid
File metadata and controls
50 lines (40 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Read a file and print its contents using C standard I/O via the FFI.
#
# Parse the stdio header at compile time and bake the CFuncSig values into the
# compiled output. lib_name is derived from the filename stem ("stdio").
# load_scope @! then immediately unpacks the resulting struct into scope, so
# every function becomes directly addressable as a label.
"/usr/include/stdio.h" c_load_header @!
load_scope @!
"/usr/include/stdlib.h" c_load_header @!
load_scope @!
# Link the actual shared library at runtime under the name "stdio" (matching
# the stem used above). The list form lets us load libc.so.6 but register it
# under a different key.
["libc.so.6" "stdio"] c_link_lib !
["libc.so.6" "stdlib"] c_link_lib !
# Open README.md for reading
"README.md" "r" fopen! clone!
# declare and clone buffer size
4096 clone!
# Allocate and clone the string buffer
malloc! types.str ptr_cast! clone!
# Reorganize so the clones for cleanup are at the bottom of the stack
# (file and buf on bottom, then file+buf_size+buf
($1 $5 $4 $3 $2)!
# After that we can build the loop body we actually want
($1 $2 $3 fgets!)
# Condition: clone so the null-arm can drop the spare; the any-arm consumes the
# original. To call printf(fmt, [args]) we first wrap the result in a list,
# push the format string, then swap with ($2 $1)! to get [fmt [result]] order.
(
clone! {
$types.null: (drop! false),
$types.any: ([$1] "%s" ($2 $1)! printf! 0 eq! not!),
} match!
)
do_while!
# Cleanup: stack values should be in the right order so just run
free!
fclose!
0 eq! assert! # Check retval from fclose