shithub: mc

ref: 974475bcfe52a488d9f5e83e474b6ca3670d5f6a
dir: /lib/std/getcwd.myr/

View raw version
use "alloc"
use "errno"
use "extremum"
use "syswrap"
use "syswrap-ss"

pkg std =
	const getcwd : (-> byte[:])
;;

const getcwd = {
	var len, n, buf

	len = 128
	while true
		buf = std.slalloc(len)
		n = bgetcwd(buf)
		if n >= 0
			/* n is the length of the nul terminated c string */
			-> buf[:n]
		elif n != Erange
			std.slfree(buf)
			-> ""
		else
			len *= 2
		;;
	;;
	/* unreachable; shut up return without value analysis */
	-> ""
}