shithub: mc

ref: e101ebf1f80607cca8ee31fee308b50025c96dab
dir: /libstd/execvp.myr/

View raw version
use "sys.use"
use "fmt.use"
use "option.use"
use "strsplit.use"
use "alloc.use"
use "env.use"

pkg std = 
	const execvp	: (cmd : byte[:], args : byte[:][:] -> int64)
	const execvpe	: (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
;;

const execvp = {cmd, args
	var path, cmdlen
	var split
	var buf : byte[512]

	match getenv("PATH")
	| `Some p:	path = p
	| `None:	path = "/usr/local/bin:/bin:/usr/bin"
	;;

	split = strsplit(path, ":")
	for s in split
		cmdlen = bfmt(buf[:], "%s/%s", s, cmd)
		execv(buf[:cmdlen], args)
	;;
	slfree(split)
	-> -1
}

const execvpe = {cmd, args, env
	var path, cmdlen
	var split
	var buf : byte[512]

	match getenv("PATH")
	| `Some p:	path = p
	| `None:	path = "/usr/local/bin:/bin:/usr/bin"
	;;

	split = strsplit(path, ":")
	for s in split
		cmdlen = bfmt(buf[:], "%s/%s", s, cmd)
		execve(buf[:cmdlen], args, env)
	;;
	slfree(split)
	-> -1
}