mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
os.exec: return ?string instead of string
This commit is contained in:

committed by
Alexander Medvednikov

parent
1470b3da11
commit
28147c0930
22
vlib/os/os.v
22
vlib/os/os.v
@ -336,21 +336,33 @@ fn popen(path string) *FILE {
|
||||
}
|
||||
}
|
||||
|
||||
fn pclose(f *FILE) int {
|
||||
$if windows {
|
||||
return C._pclose(f)
|
||||
}
|
||||
$else {
|
||||
return C.pclose(f)
|
||||
}
|
||||
}
|
||||
|
||||
// exec starts the specified command, waits for it to complete, and returns its output.
|
||||
pub fn exec(_cmd string) string {
|
||||
pub fn exec(_cmd string) ?string {
|
||||
cmd := '$_cmd 2>&1'
|
||||
f := popen(cmd)
|
||||
if isnil(f) {
|
||||
// TODO optional or error code
|
||||
println('popen $cmd failed')
|
||||
return ''
|
||||
return error('popen $cmd failed')
|
||||
}
|
||||
buf := [1000]byte
|
||||
mut res := ''
|
||||
for C.fgets(buf, 1000, f) != 0 {
|
||||
res += tos(buf, strlen(buf))
|
||||
}
|
||||
return res.trim_space()
|
||||
res = res.trim_space()
|
||||
status_code := pclose(f)/256
|
||||
if status_code != 0 {
|
||||
return error(res)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// `getenv` returns the value of the environment variable named by the key.
|
||||
|
Reference in New Issue
Block a user