shithub: mc

ref: aa7e3e0a6facd876fea2235ccb6f1ee27e016d3a
dir: /libstd/slurp.myr/

View raw version
use "alloc.use"
use "die.use"
use "error.use"
use "extremum.use"
use "fmt.use"
use "sys.use"
use "types.use"

pkg std =
	const slurp : (path : byte[:] -> error(byte[:], byte[:]))
;;

const Bufinc = 4096

const slurp = {path
	var fd
	var n
	var len
	var buf

	fd = open(path, Ordonly, 0o777)
	if fd < 0
		-> `Failure "Could not open file"
	;;

	len = 0
	buf = slalloc(Bufinc)
	while true
		n = read(fd, buf[len:])
		if n == 0
			goto done
		;;
		len += n
		buf = slgrow(buf, len + Bufinc)
	;;
:done
	-> `Success buf[:len]
}