summaryrefslogtreecommitdiff
path: root/lib/std/dir+osx.myr
diff options
context:
space:
mode:
Diffstat (limited to 'lib/std/dir+osx.myr')
-rw-r--r--lib/std/dir+osx.myr60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/std/dir+osx.myr b/lib/std/dir+osx.myr
new file mode 100644
index 0000000..28ef26b
--- /dev/null
+++ b/lib/std/dir+osx.myr
@@ -0,0 +1,60 @@
+use sys
+
+use "alloc.use"
+use "die.use"
+use "option.use"
+use "result.use"
+use "slcp.use"
+use "sldup.use"
+use "types.use"
+
+pkg std =
+ type dir = struct
+ fd : sys.fd
+ buf : byte[16384]
+ len : int64
+ off : int64
+ base : 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
+ -> `Fail "couldn't open directory"
+ ;;
+ dir = zalloc()
+ dir.fd = fd
+ -> `Ok dir
+}
+
+const dirread = {d
+ var len
+ var dent
+
+ if d.off >= d.len
+ len = sys.getdirentries64(d.fd, d.buf[:], &d.base)
+ if len <= 0
+ -> `None
+ ;;
+ d.len = len
+ d.off = 0
+ ;;
+
+ dent = &d.buf[d.off] castto(sys.dirent64#)
+ d.off += dent.reclen castto(int64)
+ -> `Some sldup(dent.name[:dent.namlen])
+}
+
+const dirclose = {d
+ sys.close(d.fd)
+ free(d)
+}
+