From ef786f9a75cc5a7e191dc604abd5314f9573dd0a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 28 Dec 2020 17:55:50 +0200 Subject: [PATCH] os: add os.execvp/2 --- vlib/os/os_c.v | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/vlib/os/os_c.v b/vlib/os/os_c.v index 4e84bf9ced..cd74f92f8e 100644 --- a/vlib/os/os_c.v +++ b/vlib/os/os_c.v @@ -22,6 +22,8 @@ fn C.fdopen(int, string) voidptr fn C.CopyFile(&u32, &u32, int) int +fn C.execvp(file charptr, argv &charptr) int + // fn C.proc_pidpath(int, byteptr, int) int struct C.stat { st_size int @@ -866,3 +868,21 @@ pub fn create(path string) ?File { is_opened: true } } + +// 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 +// the child process will take control over execution. +pub fn execvp(cmdpath string, args []string) ? { + mut cargs := []charptr{} + cargs << cmdpath.str + for i in 0 .. args.len { + cargs << args[i].str + } + cargs << charptr(0) + res := C.execvp(cmdpath.str, cargs.data) + if res == -1 { + return error(posix_get_error_msg(C.errno)) + } +}