summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-06-19 14:25:26 -0400
committerOri Bernstein <ori@eigenstate.org>2014-06-19 14:25:26 -0400
commit5fc8fe7bec594a98c7a2b9fbdfc5f7fb4f11c7c9 (patch)
treedb96a03a4f4e8d83c7ec6dbc81664295dcc56eb2
parent314b2c1651a2c946a3d9e3ccdd9fff739e603b2b (diff)
downloadmc-5fc8fe7bec594a98c7a2b9fbdfc5f7fb4f11c7c9.tar.gz
Add directory listing under Linux.
-rw-r--r--libstd/Makefile1
-rw-r--r--libstd/dir-linux.myr61
-rw-r--r--libstd/dir-osx.myr0
3 files changed, 62 insertions, 0 deletions
diff --git a/libstd/Makefile b/libstd/Makefile
index 277ba68..731fd5e 100644
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -8,6 +8,7 @@ MYRSRC= \
cmp.myr \
dial.myr \
die.myr \
+ dir.myr \
endian.myr \
env.myr \
execvp.myr \
diff --git a/libstd/dir-linux.myr b/libstd/dir-linux.myr
new file mode 100644
index 0000000..389d160
--- /dev/null
+++ b/libstd/dir-linux.myr
@@ -0,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)
+}
+
diff --git a/libstd/dir-osx.myr b/libstd/dir-osx.myr
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/libstd/dir-osx.myr