code wiki / _hdl_build / test_chdir.nx
test_chdir.nx source
↩ module page · 20 lines · 1174 B
1// test_chdir.nx -- isolate whether the new sys_chdir(80) wrapper actually changes cwd. Creates /tmp/cdt/m,
2// chdirs to /tmp/cdt, then opens "m" RELATIVE. If it reads -> cwd changed -> sys_chdir works. expect_exit:0
3import "nx_syscalls.nx"
4func p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
5func pn(v: i64) -> i64 {
6 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}
7 let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1}
8 while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0
9}
10func main() -> i64 {
11 sys_mkdir("/tmp/cdt" as *u8, 0x1ed)
12 let fd: i64 = sys_openat_wr("/tmp/cdt/m" as *u8, 0x1a4); sys_write(fd,"HI" as *u8,2); sys_close(fd)
13 let r: i64 = sys_chdir("/tmp/cdt" as *u8)
14 p("chdir_rc=" as *u8); pn(r); p("\n" as *u8)
15 let box: *i64=sys_mmap(16) as *i64; box[0]=0
16 let d: *u8=sys_read_file("m" as *u8, box)
17 if (d as i64)!=0 { p("REL-OPEN-OK cwd-CHANGED len=" as *u8); pn(box[0]); p("\n" as *u8) }
18 else { p("REL-OPEN-FAIL cwd-NOT-changed\n" as *u8) }
19 sys_exit(0); return 0
20}