os: remove unnecessary heap allocation from os.execute
parent
c6b8b0bb0a
commit
50a608aab3
|
@ -344,23 +344,23 @@ pub fn execute(cmd string) Result {
|
|||
}
|
||||
}
|
||||
fd := fileno(f)
|
||||
buf := unsafe { malloc_noscan(4096) }
|
||||
mut res := strings.new_builder(1024)
|
||||
defer {
|
||||
unsafe { res.free() }
|
||||
}
|
||||
buf := [4096]byte{}
|
||||
unsafe {
|
||||
pbuf := &buf[0]
|
||||
for {
|
||||
len := C.read(fd, buf, 4096)
|
||||
len := C.read(fd, pbuf, 4096)
|
||||
if len == 0 {
|
||||
break
|
||||
}
|
||||
res.write_ptr(buf, len)
|
||||
res.write_ptr(pbuf, len)
|
||||
}
|
||||
}
|
||||
soutput := res.str()
|
||||
exit_code := vpclose(f)
|
||||
unsafe { free(buf) }
|
||||
return Result{
|
||||
exit_code: exit_code
|
||||
output: soutput
|
||||
|
|
|
@ -858,5 +858,6 @@ fn test_execute() ? {
|
|||
// println('output.len: $result.output.len')
|
||||
// println('output hexresult: $hexresult')
|
||||
assert result.exit_code == 0
|
||||
assert hexresult == '7374617274004d4944444c450066696e6973680a7878'
|
||||
assert hexresult.starts_with('7374617274004d4944444c450066696e697368')
|
||||
assert hexresult.ends_with('0a7878')
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue