interplane.Get Omega
Read in your language

sovereign-intelligence

Front door to Drake Stapleton's sovereign stack.

Brought by drakestapleton

branch main Branches Tags History

sovereign-intelligence / harness / examples / tmp_files.om

81 lines · 1844 bytes Raw Blame History
1note tmp_files: make a directory and a file relative to the current directory, read it back, clean up
2note run it in an empty scratch directory. Linux AArch64 values: O_WRONLY|O_CREAT|O_EXCL = 193,
3note mode 0600 = 384, 0700 = 448, st_size at byte 48 of the 128-byte stat structure
4note ---- output helpers: a function works on a global by naming it (a global is never an argument)
5global ob uchar 32
6note outb: write ob[off .. off+n) to stdout; a short write continues, 0 or an error exits 90
7note bound: every pass writes at least one byte and n <= 32, so at most 32 passes
8fn outb off ulong n ulong to void
9e = add off n
10p = off
11while lt p e bound 32
12k = write 1 ob p sub e p
13if le k 0
14exit 90
15end
16p = add p ulong k
17end
18end
19note putu: v in decimal, then a newline. A ulong has at most 20 digits, so 20 passes
20fn putu v ulong to void
21i = ulong 31
22put ob 31 10
23n = v
24while true bound 20
25i = sub i 1
26put ob i uchar add rem n 10 48
27n = div n 10
28if eq n 0
29break
30end
31end
32outb i sub 32 i
33end
34note putl: a signed value; -(v+1)+1 avoids negating the minimum, which would trap 1
35fn putl v long to void
36if lt v 0
37put ob 0 45
38outb 0 1
39putu add ulong neg add v 1 1
40ret
41end
42putu ulong v
43end
44text dname "scratch"
45text fname "scratch/f.txt"
46text body "omega\n"
47global rb uchar 16
48global sb uchar 128
49fn main to int
50r = makedir dname 0 len dname 448
51putl r
52fd = fopen fname 0 len fname 193 384
53if lt fd 0
54exit 93
55end
56w = write int fd body 0 len body
57putl w
58c = fclose int fd
59putl c
60fd2 = fopen fname 0 len fname 0 0
61if lt fd2 0
62exit 94
63end
64k = read int fd2 rb 0 16
65putl k
66c2 = fclose int fd2
67s = stat fname 0 len fname sb 0 128
68putl s
69sz = or uint get sb 48 shl uint get sb 49 8
70putu ulong sz
71if gt k 0
72x = write 1 rb 0 ulong k
73end
74u = unlink fname 0 len fname
75putl u
76d = rmdir dname 0 len dname
77putl d
78gone = fopen fname 0 len fname 0 0
79putl gone
80ret 0
81end