shithub: mc

ref: 9a2785cb9f3cd47fbe06371d4fc982d5f0ae7120
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
}