summaryrefslogtreecommitdiff
path: root/lib/std/dir+netbsd.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/dir+netbsd.myr')
-rw-r--r--lib/std/dir+netbsd.myr65
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/std/dir+netbsd.myr b/lib/std/dir+netbsd.myr
new file mode 100644
index 0000000..274175d
--- /dev/null
+++ b/lib/std/dir+netbsd.myr
@@ -0,0 +1,65 @@
+use sys
+
+use "alloc"
+use "die"
+use "memops"
+use "option"
+use "result"
+use "slcp"
+use "sldup"
+use "types"
+
+pkg std =
+ type dir = struct
+ fd : sys.fd
+ buf : byte[16384]
+ len : int64
+ off : int64
+ ;;
+
+ const diropen : (p : byte[:] -> std.result(dir#, byte[:]))
+ const dirread : (d : dir# -> std.option(byte[:]))
+ const dirclose : (d : dir# -> void)
+;;
+
+const diropen = {p
+ var fd
+ var dir
+
+ fd = sys.open(p, sys.Ordonly | sys.Odir)
+ if fd < 0
+ -> `Err "couldn't open directory"
+ ;;
+ dir = zalloc()
+ dir.fd = fd
+ -> `Ok dir
+}
+
+const dirread = {d
+ var len
+ var dent
+ var namelen
+
+ if d.off >= d.len
+ len = sys.getdents(d.fd, d.buf[:])
+ if len <= 0
+ -> `None
+ ;;
+ d.len = len
+ d.off = 0
+ ;;
+
+ dent = (&d.buf[d.off] : sys.dirent#)
+ namelen = 0
+ d.off += (dent.reclen : int64)
+ while dent.name[namelen] != 0
+ namelen++
+ ;;
+ -> `Some sldup(dent.name[:namelen])
+}
+
+const dirclose = {d
+ sys.close(d.fd)
+ free(d)
+}
+