shithub: mc

Download patch

ref: 5fc8fe7bec594a98c7a2b9fbdfc5f7fb4f11c7c9
parent: 314b2c1651a2c946a3d9e3ccdd9fff739e603b2b
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)
+}
+