code wiki / _hdl_build / nx_sweep_dir.nx
nx_sweep_dir.nx source
↩ module page · 58 lines · 2517 B
1// nx_sweep_dir.nx -- enumerate .nx files in a dir, emit "<dir>/<name>" one per line to stdout
2// (or an outfile via argv[2]). Single-responsibility enumerator (rule 9) so list-based tools like
3// nx_map_remove_func can run FULL-SCALE over a whole tree with no context round-trip. license_tier: ORIGINAL
4import "nx_syscalls.nx"
5import "nx_deploy_lib.nx"
6const SW_MAGIC_4096: i64 = 4096
7
8const SW_DBUF: i64 = 262144
9
10func sw_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
11func sw_reclen(rec: *u8) -> i64 { return (rec[16] as i64) | ((rec[17] as i64) << 8) }
12func sw_ends_nx(nm: *u8) -> i64 {
13 let n: i64 = sw_slen(nm)
14 if n < 3 { return 0 }
15 if nm[n-3] != (46 as u8) { return 0 }
16 if nm[n-2] != (110 as u8) { return 0 }
17 if nm[n-1] != (120 as u8) { return 0 }
18 return 1
19}
20
21func main(argc: i64, argv: *i64) -> i64 {
22 if argc < 2 { let u: *u8 = "usage: nx_sweep_dir <dir> [outfile]\n" as *u8; sys_write(2, u, sw_slen(u)); sys_exit(2); return 2 }
23 let dir: *u8 = argv[1] as *u8
24 if argc >= 3 { let ofd: i64 = sys_openat_wr(argv[2] as *u8, 0x1a4); if ofd >= 0 { sys_dup3(ofd, 1, 0) } }
25 let dl: i64 = sw_slen(dir)
26 let dfd: i64 = sys_openat_rd(dir)
27 if dfd < 0 { let e: *u8 = "NX-SWEEP-DIR dir-unreadable\n" as *u8; sys_write(2, e, sw_slen(e)); sys_exit(3); return 3 }
28 let dbuf: *u8 = sys_mmap(SW_DBUF)
29 let line: *u8 = sys_mmap(SW_MAGIC_4096)
30 var count: i64 = 0
31 var go: i64 = 1
32 while go == 1 {
33 let dn: i64 = sys_getdents64(dfd, dbuf, SW_DBUF)
34 if dn <= 0 { go = 0 } else {
35 var off: i64 = 0
36 while off < dn {
37 let rec: *u8 = ((dbuf as i64) + off) as *u8
38 let rl: i64 = sw_reclen(rec)
39 let ty: i64 = rec[18] as i64
40 let nm: *u8 = ((rec as i64) + 19) as *u8
41 if ty != 4 { if sw_ends_nx(nm) == 1 {
42 var w: i64 = 0
43 var i: i64 = 0
44 while i < dl { line[w] = dir[i]; w = w + 1; i = i + 1 }
45 line[w] = 47 as u8; w = w + 1
46 var j: i64 = 0
47 while nm[j] != (0 as u8) { line[w] = nm[j]; w = w + 1; j = j + 1 }
48 line[w] = 10 as u8; w = w + 1
49 sys_write(1, line, w)
50 count = count + 1
51 } }
52 off = off + rl
53 }
54 }
55 }
56 let d: *u8 = "NX-SWEEP-DIR done\n" as *u8; sys_write(2, d, sw_slen(d))
57 return 0
58}