From c8d9a1603d310069428108159191a472fcf44f84 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sat, 17 Oct 2026 08:59:30 +0000 Subject: [PATCH] Sys's directories take the Dir by var (report 2026-10.33) Sys.openDir, nextEntry, entryKind and closeDir take var d: Dir, in both Sys files; none kept the pointer. entryKind only reads, but Sys is compiled by the seed, which doesn't know const: it takes const when the seed is gone, which breaks no caller. nextEntry returns a slice of the caller's Dir, which a var parameter is. The fixpoint holds; test/libc passes over glibc and musl. --- diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 6878265..f22c1a1 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -266,10 +266,10 @@ proc infoOf*(fd: INT): (Info, INT); (* the open file's *) proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path's file system *) -proc openDir*(d: ^Dir; path: []U8): INT; -proc nextEntry*(d: ^Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *) -proc entryKind*(d: ^Dir): Kind; (* what the name nextEntry gave last is *) -proc closeDir*(d: ^Dir); +proc openDir*(var d: Dir; path: []U8): INT; +proc nextEntry*(var d: Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *) +proc entryKind*(var d: Dir): Kind; (* what the name nextEntry gave last is *) +proc closeDir*(var d: Dir); proc rawMode*(fd: INT): BOOL; (* the terminal on fd: no echo, no lines, no signals *) proc restoreMode*(fd: INT); (* as rawMode found it *) diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index e5eecfc..1f7ebf0 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -526,38 +526,38 @@ end; (* Opens the directory at path, for nextEntry. *) -proc openDir*(d: ^Dir; path: []U8): INT; +proc openDir*(var d: Dir; path: []U8): INT; var a: [4096]U8; begin - d^.kind := unknown; - d^.pos := 0; - d^.len := 0; + d.kind := unknown; + d.pos := 0; + d.len := 0; if not cstr(path, a) then return -36; end; - d^.fd := SYSCALL(2, ADR(a), 10000H); (* open, O_RDONLY, O_DIRECTORY *) - if d^.fd < 0 then return d^.fd; end; + d.fd := SYSCALL(2, ADR(a), 10000H); (* open, O_RDONLY, O_DIRECTORY *) + if d.fd < 0 then return d.fd; end; return 0; end; (* The next name in the directory, good until the next call, but . and .., in no order; false at the end. *) -proc nextEntry*(d: ^Dir): ([]U8, BOOL); +proc nextEntry*(var d: Dir): ([]U8, BOOL); var n, at: INT; begin while true do - if d^.pos >= d^.len then - d^.len := SYSCALL(217, d^.fd, ADR(d^.buf), LEN(d^.buf)); (* getdents64 *) - d^.pos := 0; - if d^.len <= 0 then return "", false; end; + if d.pos >= d.len then + d.len := SYSCALL(217, d.fd, ADR(d.buf), LEN(d.buf)); (* getdents64 *) + d.pos := 0; + if d.len <= 0 then return "", false; end; end; (* a record: d_ino and d_off, 8 bytes each; d_reclen, 2; d_type, 1; then d_name, ending in a zero *) - at := d^.pos + 19; + at := d.pos + 19; n := 0; - while d^.buf[at + n] # 0 do INC(n); end; - d^.kind := kindOf(d^.buf[d^.pos + 18]); - INC(d^.pos, INT(d^.buf[d^.pos + 16]) + INT(d^.buf[d^.pos + 17]) * 256); - if (d^.buf[at..at + n] # ".") and (d^.buf[at..at + n] # "..") then - return d^.buf[at..at + n], true; + while d.buf[at + n] # 0 do INC(n); end; + d.kind := kindOf(d.buf[d.pos + 18]); + INC(d.pos, INT(d.buf[d.pos + 16]) + INT(d.buf[d.pos + 17]) * 256); + if (d.buf[at..at + n] # ".") and (d.buf[at..at + n] # "..") then + return d.buf[at..at + n], true; end; end; return "", false; @@ -566,15 +566,15 @@ (* What the name nextEntry gave last is, as the directory says, so that a walk needn't ask each file with linkInfo; unknown where the file system doesn't say. A link is isLink, whatever it names. *) -proc entryKind*(d: ^Dir): Kind; +proc entryKind*(var d: Dir): Kind; begin - return d^.kind; + return d.kind; end; (* Closes the directory d that openDir opened. *) -proc closeDir*(d: ^Dir); +proc closeDir*(var d: Dir); begin - _ := SYSCALL(3, d^.fd); + _ := SYSCALL(3, d.fd); end; (* Puts the terminal on fd into raw mode: no echo, no lines, no signals, @@ -2249,18 +2249,18 @@ _ := remove("/tmp/raz-sys-dir/h"); i, err := info("/no/such/file"); if err >= 0 then exit(2); end; - if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; - name, ok := nextEntry(ADR(d)); + if openDir(d, "/tmp/raz-sys-dir") # 0 then exit(2); end; + name, ok := nextEntry(d); if not ok or (name # "g") then exit(2); end; - if (entryKind(ADR(d)) # isFile) and (entryKind(ADR(d)) # unknown) then exit(2); end; + if (entryKind(d) # isFile) and (entryKind(d) # unknown) then exit(2); end; i, err := info("/tmp/raz-sys-dir/g"); j, err := info("/tmp/raz-sys-dir"); if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) or (i.links # 1) then exit(2); end; - name, ok := nextEntry(ADR(d)); + name, ok := nextEntry(d); if ok then exit(2); end; - closeDir(ADR(d)); + closeDir(d); if (remove("/tmp/raz-sys-dir/g") # 0) or (remove("/tmp/raz-sys-dir/g") >= 0) then exit(2); end; - if openDir(ADR(d), "/no/such/dir") >= 0 then exit(2); end; + if openDir(d, "/no/such/dir") >= 0 then exit(2); end; (* the terminal: none here, fd -1 *) if rawMode(-1) or ready(-1, 0) then exit(2); end; w, h := termSize(-1); diff --git a/lib/Sys.raz b/lib/Sys.raz index a2fc908..d8cdba7 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -599,25 +599,25 @@ end; (* Opens the directory at path, for nextEntry. *) -proc openDir*(d: ^Dir; path: []U8): INT; +proc openDir*(var d: Dir; path: []U8): INT; var a: [4096]U8; begin - d^.kind := unknown; + d.kind := unknown; if not cstr(path, a) then return -36; end; - d^.d := C.opendir(ADR(a[0])); - if d^.d = nil then return failed(); end; + d.d := C.opendir(ADR(a[0])); + if d.d = nil then return failed(); end; return 0; end; (* The next name in the directory, good until the next call, but . and .., in no order; false at the end. *) -proc nextEntry*(d: ^Dir): ([]U8, BOOL); +proc nextEntry*(var d: Dir): ([]U8, BOOL); var e: ^U8; n: INT; begin while true do - e := C.readdir(d^.d); + e := C.readdir(d.d); if e = nil then return "", false; end; - d^.kind := kindOf(e[CAbi.dTypeAt]); + d.kind := kindOf(e[CAbi.dTypeAt]); e := e + CAbi.dNameAt; n := 0; while e[n] # 0 do INC(n); end; @@ -629,15 +629,15 @@ (* What the name nextEntry gave last is, as the directory says, so that a walk needn't ask each file with linkInfo; unknown where the file system doesn't say. A link is isLink, whatever it names. *) -proc entryKind*(d: ^Dir): Kind; +proc entryKind*(var d: Dir): Kind; begin - return d^.kind; + return d.kind; end; (* Closes the directory d that openDir opened. *) -proc closeDir*(d: ^Dir); +proc closeDir*(var d: Dir); begin - _ := C.closedir(d^.d); + _ := C.closedir(d.d); end; (* Puts the terminal on fd into raw mode, as cfmakeraw makes it: no @@ -2173,18 +2173,18 @@ _ := remove("/tmp/raz-sys-dir/h"); i, err := info("/no/such/file"); if err >= 0 then exit(2); end; - if openDir(ADR(d), "/tmp/raz-sys-dir") # 0 then exit(2); end; - name, ok := nextEntry(ADR(d)); + if openDir(d, "/tmp/raz-sys-dir") # 0 then exit(2); end; + name, ok := nextEntry(d); if not ok or (name # "g") then exit(2); end; - if (entryKind(ADR(d)) # isFile) and (entryKind(ADR(d)) # unknown) then exit(2); end; + if (entryKind(d) # isFile) and (entryKind(d) # unknown) then exit(2); end; i, err := info("/tmp/raz-sys-dir/g"); j, err := info("/tmp/raz-sys-dir"); if (err # 0) or (i.dev # j.dev) or (i.ino = j.ino) or (i.links # 1) then exit(2); end; - name, ok := nextEntry(ADR(d)); + name, ok := nextEntry(d); if ok then exit(2); end; - closeDir(ADR(d)); + closeDir(d); if (remove("/tmp/raz-sys-dir/g") # 0) or (remove("/tmp/raz-sys-dir/g") >= 0) then exit(2); end; - if openDir(ADR(d), "/no/such/dir") >= 0 then exit(2); end; + if openDir(d, "/no/such/dir") >= 0 then exit(2); end; (* the terminal: none here, fd -1 *) if rawMode(-1) or ready(-1, 0) then exit(2); end; w, h := termSize(-1); diff --git a/site/content/advanced/04-files.txt b/site/content/advanced/04-files.txt index 58455b0..ace3acd 100644 --- a/site/content/advanced/04-files.txt +++ b/site/content/advanced/04-files.txt @@ -109,15 +109,15 @@ _ := Sys.makeDir("proj/src"); put("proj/b.txt", "bb"); put("proj/a.txt", "a"); - if Sys.openDir(ADR(d), "proj") < 0 then return; end; + if Sys.openDir(d, "proj") < 0 then return; end; n := 0; - name, more := Sys.nextEntry(ADR(d)); + name, more := Sys.nextEntry(d); while more do copy, ok := Str.copy(Heap.heap, name); if ok and (n < 16) then names[n] := copy; INC(n); end; - name, more := Sys.nextEntry(ADR(d)); + name, more := Sys.nextEntry(d); end; - Sys.closeDir(ADR(d)); + Sys.closeDir(d); Order.sort(names[0..n]); Buf.init(path, Heap.heap); for i := 0 to n - 1 do diff --git a/src/Parse.raz b/src/Parse.raz index 4283010..c7cfa5e 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -3992,9 +3992,9 @@ begin prefix := say1("{}.", nameOf(n)); numOthers := 0; - if Sys.openDir(ADR(dp), dir) # 0 then return nil; end; + if Sys.openDir(dp, dir) # 0 then return nil; end; repeat - e, more := Sys.nextEntry(ADR(dp)); + e, more := Sys.nextEntry(dp); if not more then break; end; if (LEN(e) > 4) and Str.hasPrefix(e, prefix) and (e[LEN(e) - 4..LEN(e)] = ".raz") and (e # chosen) and (numOthers < 32) then @@ -4002,7 +4002,7 @@ INC(numOthers); end; until false; - Sys.closeDir(ADR(dp)); + Sys.closeDir(dp); for i := 0 to numOthers - 1 do sib := try compileFile(say2("{}/{}", dir, others[i]), n, args, true); compareIface(m, sib);