I've been trying to write a shell in Rust that links directly to the libc
library. I've used a Vec<String>
to hold the arguments to be passed to execvp()
, but it seems that my conversion to char **
has not been successful. Upon execution, all the parameters became null strings.
Here's the piece of code involved.
fn safe_execvp(path: String, argv: Vec<String>) -> Result<(), i32> {
unsafe {
let c_path = CString::new(path.as_str()).unwrap();
let mut c_argv_vec = Vec::new();
for arg in &argv {
let c_arg = CString::new(arg.as_str()).unwrap().as_ptr();
c_argv_vec.push(c_arg);
}
c_argv_vec.push(std::ptr::null());
match execvp(c_file.as_ptr(), c_argv_vec.as_ptr()) {
num => Err(num),
}
}
}
execvp
is the C library function defined as fn execvp(file: *const i8, argv: *const*const i8) -> i32;
.
I'm not sure what I've done wrong. Is it because the memory for the arguments were released before calling execvp()
?
CString
s in a vector. – V2