Skip to content

Commit b753358

Browse files
committed
nice: use Command::exec() instead of libc::execvp()
Signed-off-by: Etienne Cordonnier <ecordonnier@snap.com>
1 parent aaa0610 commit b753358

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

src/uu/nice/src/nice.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
// spell-checker:ignore (ToDO) getpriority execvp setpriority nstr PRIO cstrs ENOENT
6+
// spell-checker:ignore (ToDO) getpriority setpriority nstr PRIO
77

88
use clap::{Arg, ArgAction, Command};
9-
use libc::{PRIO_PROCESS, c_char, c_int, execvp};
10-
use std::ffi::{CString, OsString};
11-
use std::io::{Error, Write};
12-
use std::ptr;
9+
use libc::PRIO_PROCESS;
10+
use std::ffi::OsString;
11+
use std::io::{Error, ErrorKind, Write};
12+
use std::os::unix::process::CommandExt;
13+
use std::process;
1314

1415
use uucore::translate;
1516
use uucore::{
@@ -156,21 +157,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
156157
}
157158
}
158159

159-
let cstrs: Vec<CString> = matches
160-
.get_many::<String>(options::COMMAND)
161-
.unwrap()
162-
.map(|x| CString::new(x.as_bytes()).unwrap())
163-
.collect();
160+
let mut cmd_iter = matches.get_many::<String>(options::COMMAND).unwrap();
161+
let cmd = cmd_iter.next().unwrap();
162+
let args: Vec<&String> = cmd_iter.collect();
164163

165-
let mut args: Vec<*const c_char> = cstrs.iter().map(|s| s.as_ptr()).collect();
166-
args.push(ptr::null::<c_char>());
167-
unsafe {
168-
execvp(args[0], args.as_mut_ptr());
169-
}
164+
let err = process::Command::new(cmd).args(args).exec();
170165

171-
show_error!("execvp: {}", Error::last_os_error());
166+
show_error!("{}: {}", cmd, err);
172167

173-
let exit_code = if Error::last_os_error().raw_os_error().unwrap() as c_int == libc::ENOENT {
168+
let exit_code = if err.kind() == ErrorKind::NotFound {
174169
127
175170
} else {
176171
126

0 commit comments

Comments
 (0)