shithub: mc

ref: 0b7b8cf380ced404a763a81e86db439ee4915393
dir: /lib/std/syswrap-ss+plan9.myr/

View raw version
use sys

use "types"
use "errno"
use "cstrconv"

pkg std =
	const nanosleep	: (nsecs : uint64 -> errno)
	$noret const exit	: (status : int -> void)
	pkglocal const bgetcwd	: (buf : byte[:] -> errno)
;;

const nanosleep = {nsecs
	if sys.sleep((nsecs/1_000_000 : uint32)) < 0
		-> lasterr()
	;;
	-> 0
}

const bgetcwd = {buf
	var fd

	fd = sys.open(".", sys.Ordonly)
	if fd < 0
		-> (fd : errno)
	;;

	if sys.fd2path(fd, buf) == 0
		/*
		Because we don't return the size, the best we can do is
		assume that if the buffer is completely full, we have
		truncated it. Since we truncate at utf8 characters, we
		can have at most 3 bytes truncated (4 bytes will fit
		any utf8 char), and one byte for the nul terminator.
		*/
		if cstrlen(buf) + 5 == buf.len
			-> Erange
		else
			-> (cstrlen(buf) : errno)
		;;
	;;
	-> Emisc
}

const digitchars = "0123456789"
const exit	= {status
	var buf : byte[32]	/* big enough for exit status numbers */
	var n, i
	
	if status == 0
		sys.exits("")
	else
		status &= 255
		i = 100
		n = 0
		while i > 0
			if status >= i
				buf[n++] = digitchars[(status/i)%10]
			;;
			i /= 10
		;;
		sys.exits(buf[:n])
	;;
}