shithub: mc

Download patch

ref: b0c3ab86f225f819ab5c0ccdc01a768780d92c88
parent: 0a249c24c7dd73fbc47ea3eeb097da814f54bfe0
author: Ori Bernstein <[email protected]>
date: Mon Nov 5 10:43:40 EST 2012

Add support for 'slurp' function.

--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -7,6 +7,7 @@
     fmt.myr \
     option.myr \
     rand.myr \
+    slurp.myr \
     sys.myr \
     types.myr \
     utf.myr \
@@ -31,7 +32,7 @@
 	cp start-$(SYS).s start.s
 
 test: libstd.a test.myr
-	../myrbuild/myrbuild -b test -I. test.myr
+	../myrbuild/myrbuild -C../6/6m -M../muse/muse -b test -I. test.myr
 
 
 $(MYRLIB): $(MYRSRC) $(ASMSRC)
--- /dev/null
+++ b/libstd/slurp.myr
@@ -1,0 +1,38 @@
+use "types.use"
+use "alloc.use"
+use "die.use"
+use "sys.use"
+use "extremum.use"
+
+pkg std =
+	const slurp : (path : byte[:] -> byte[:])
+;;
+
+const Bufinc = 4096
+
+const slurp = {path
+	var fd
+	var n
+	var len
+	var buf
+
+	fd = open(path, Ordonly, 0o777)
+	if fd < 0
+		die("Could not open file")
+	;;
+
+	len = 0
+	buf = slalloc(Bufinc)
+	while true
+		n = read(fd, buf[len:])
+		if n < 0
+			die("Error reading file")
+		elif n == 0
+			goto done
+		;;
+		len += n
+		buf = slgrow(buf, len + Bufinc)
+	;;
+:done
+	-> buf[:len]
+}