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

os: remove unnecessary unsafes

This commit is contained in:
Alexander Medvednikov 2022-07-06 07:07:48 +03:00
parent d3090de02e
commit 819b6f475a
2 changed files with 42 additions and 48 deletions

View File

@ -949,26 +949,24 @@ pub fn open_append(path string) ?File {
// Note: this function will NOT return when successfull, since // Note: this function will NOT return when successfull, since
// the child process will take control over execution. // the child process will take control over execution.
pub fn execvp(cmdpath string, cmdargs []string) ? { pub fn execvp(cmdpath string, cmdargs []string) ? {
unsafe { mut cargs := []&char{}
mut cargs := []&char{} cargs << &char(cmdpath.str)
cargs << &char(cmdpath.str) for i in 0 .. cmdargs.len {
for i in 0 .. cmdargs.len { cargs << &char(cmdargs[i].str)
cargs << &char(cmdargs[i].str)
}
cargs << &char(0)
mut res := int(0)
$if windows {
res = C._execvp(&char(cmdpath.str), cargs.data)
} $else {
res = C.execvp(&char(cmdpath.str), cargs.data)
}
if res == -1 {
return error_with_code(posix_get_error_msg(C.errno), C.errno)
}
// just in case C._execvp returned ... that happens on windows ...
exit(res)
} }
cargs << &char(0)
mut res := int(0)
$if windows {
res = C._execvp(&char(cmdpath.str), cargs.data)
} $else {
res = C.execvp(&char(cmdpath.str), cargs.data)
}
if res == -1 {
return error_with_code(posix_get_error_msg(C.errno), C.errno)
}
// just in case C._execvp returned ... that happens on windows ...
exit(res)
} }
// execve - loads and executes a new child process, *in place* of the current process. // execve - loads and executes a new child process, *in place* of the current process.
@ -978,29 +976,27 @@ pub fn execvp(cmdpath string, cmdargs []string) ? {
// Note: this function will NOT return when successfull, since // Note: this function will NOT return when successfull, since
// the child process will take control over execution. // the child process will take control over execution.
pub fn execve(cmdpath string, cmdargs []string, envs []string) ? { pub fn execve(cmdpath string, cmdargs []string, envs []string) ? {
unsafe { mut cargv := []&char{}
mut cargv := []&char{} mut cenvs := []&char{}
mut cenvs := []&char{} cargv << &char(cmdpath.str)
cargv << &char(cmdpath.str) for i in 0 .. cmdargs.len {
for i in 0 .. cmdargs.len { cargv << &char(cmdargs[i].str)
cargv << &char(cmdargs[i].str) }
} for i in 0 .. envs.len {
for i in 0 .. envs.len { cenvs << &char(envs[i].str)
cenvs << &char(envs[i].str) }
} cargv << &char(0)
cargv << &char(0) cenvs << &char(0)
cenvs << &char(0) mut res := int(0)
mut res := int(0) $if windows {
$if windows { res = C._execve(&char(cmdpath.str), cargv.data, cenvs.data)
res = C._execve(&char(cmdpath.str), cargv.data, cenvs.data) } $else {
} $else { res = C.execve(&char(cmdpath.str), cargv.data, cenvs.data)
res = C.execve(&char(cmdpath.str), cargv.data, cenvs.data) }
} // Note: normally execve does not return at all.
// Note: normally execve does not return at all. // If it returns, then something went wrong...
// If it returns, then something went wrong... if res == -1 {
if res == -1 { return error_with_code(posix_get_error_msg(C.errno), C.errno)
return error_with_code(posix_get_error_msg(C.errno), C.errno)
}
} }
} }

View File

@ -138,13 +138,11 @@ pub fn (pool &PoolProcessor) get_results<T>() []T {
// get_results_ref - get a list of type safe results in the main thread. // get_results_ref - get a list of type safe results in the main thread.
pub fn (pool &PoolProcessor) get_results_ref<T>() []&T { pub fn (pool &PoolProcessor) get_results_ref<T>() []&T {
unsafe { mut res := []&T{cap: pool.results.len}
mut res := []&T{cap: pool.results.len} for i in 0 .. pool.results.len {
for i in 0 .. pool.results.len { res << &T(pool.results[i])
res << &T(pool.results[i])
}
return res
} }
return res
} }
// set_shared_context - can be called during the setup so that you can // set_shared_context - can be called during the setup so that you can