summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-06-19 20:56:49 -0400
committerOri Bernstein <ori@eigenstate.org>2014-06-19 20:56:49 -0400
commitea7ee26b95296d6a2e4f842448b885fdc1f5c41c (patch)
tree728fe1280309145b726f124bf85589b8d16440b1
parentc464f4b8cc56ae10e8f053efc6ec8b7e9989d4df (diff)
downloadmc-ea7ee26b95296d6a2e4f842448b885fdc1f5c41c.tar.gz
Add support for directory listing under OSX
-rw-r--r--libstd/dir-osx.myr60
-rw-r--r--libstd/sys-osx.myr2
2 files changed, 61 insertions, 1 deletions
diff --git a/libstd/dir-osx.myr b/libstd/dir-osx.myr
index e69de29..ff48d16 100644
--- a/libstd/dir-osx.myr
+++ b/libstd/dir-osx.myr
@@ -0,0 +1,60 @@
+use "alloc.use"
+use "die.use"
+use "option.use"
+use "result.use"
+use "slcp.use"
+use "sldup.use"
+use "sys.use"
+use "types.use"
+
+pkg std =
+ type dir = struct
+ fd : 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 = open(p, Ordonly | Odir)
+ if fd < 0
+ -> `Fail "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 = getdirentries64(d.fd, d.buf[:], &d.base)
+ if len <= 0
+ -> `None
+ ;;
+ d.len = len
+ d.off = 0
+ ;;
+
+ dent = &d.buf[d.off] castto(dirent64#)
+ d.off += dent.reclen castto(int64)
+ -> `Some sldup(dent.name[:dent.namlen])
+}
+
+const dirclose = {d
+ close(d.fd)
+ free(d)
+}
+
diff --git a/libstd/sys-osx.myr b/libstd/sys-osx.myr
index 61f4ec5..22f895e 100644
--- a/libstd/sys-osx.myr
+++ b/libstd/sys-osx.myr
@@ -99,7 +99,7 @@ pkg std =
reclen : uint16 /* length of this record */
namlen : uint16 /* length of string in d_name */
typeid : uint8 /* file type, see below */
- name : byte[1024]
+ name : byte[0]
;;
/* open options */