1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: add os.execve/3 (extracted from unix_spawn_process); use os.execvp/2 in v.util.launch_tool/3

This commit is contained in:
Delyan Angelov
2020-12-30 17:57:01 +02:00
parent c943c8a16e
commit b4f02adc32
3 changed files with 32 additions and 18 deletions

View File

@@ -869,7 +869,7 @@ pub fn create(path string) ?File {
}
}
// execvp - loads and executes a new child process, in place of the current process.
// execvp - loads and executes a new child process, *in place* of the current process.
// The child process executable is located in `cmdpath`.
// The arguments, that will be passed to it are in `args`.
// NB: this function will NOT return when successfull, since
@@ -883,6 +883,32 @@ pub fn execvp(cmdpath string, args []string) ? {
cargs << charptr(0)
res := C.execvp(charptr(cmdpath.str), cargs.data)
if res == -1 {
return error(posix_get_error_msg(C.errno))
return error_with_code(posix_get_error_msg(C.errno), C.errno)
}
}
// execve - loads and executes a new child process, *in place* of the current process.
// The child process executable is located in `cmdpath`.
// The arguments, that will be passed to it are in `args`.
// You can pass environment variables to through `envs`.
// NB: this function will NOT return when successfull, since
// the child process will take control over execution.
pub fn execve(cmdpath string, args []string, envs []string) ? {
mut cargv := []charptr{}
mut cenvs := []charptr{}
cargv << charptr(cmdpath.str)
for i in 0 .. args.len {
cargv << charptr(args[i].str)
}
for i in 0 .. envs.len {
cenvs << charptr(envs[i].str)
}
cargv << charptr(0)
cenvs << charptr(0)
res := C.execve(charptr(cmdpath.str), cargv.data, cenvs.data)
// NB: normally execve does not return at all.
// If it returns, then something went wrong...
if res == -1 {
return error_with_code(posix_get_error_msg(C.errno), C.errno)
}
}