ref: d8056a53dcdc39260dad1afacfca22e93facf509
parent: edc5aff343b2b1bf33379c8614a1a51b75c9d993
author: Ori Bernstein <[email protected]>
date: Thu Jun 19 10:25:26 EDT 2014
Add directory listing under Linux.
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -8,6 +8,7 @@
cmp.myr \
dial.myr \
die.myr \
+ dir.myr \
endian.myr \
env.myr \
execvp.myr \
--- /dev/null
+++ b/libstd/dir-linux.myr
@@ -1,0 +1,61 @@
+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
+ ;;
+
+ 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 = getdents64(d.fd, d.buf[:])
+ if len <= 0
+ -> `None
+ ;;
+ d.len = len
+ d.off = 0
+ ;;
+
+ dent = &d.buf[d.off] castto(dirent64#)
+ namelen = (dent.reclen - 20) castto(size)
+ d.off += dent.reclen castto(int64)
+ -> `Some sldup(dent.name[:namelen])
+}
+
+const dirclose = {d
+ close(d.fd)
+ free(d)
+}
+