diff --git a/Cargo.lock b/Cargo.lock index 4250504..b3e2f19 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -355,6 +355,7 @@ version = "0.1.0" dependencies = [ "clap", "gettext-rs", + "libc", "plib", ] diff --git a/README.md b/README.md index 49250bd..51d91ea 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ https://github.com/jgarzik/posixutils - [ ] more - [ ] mv - [ ] newgrp - - [ ] nice + - [x] nice - [ ] nl - [ ] nm (Development) - [ ] nohup diff --git a/process/Cargo.toml b/process/Cargo.toml index bf04803..e256ddd 100644 --- a/process/Cargo.toml +++ b/process/Cargo.toml @@ -7,8 +7,13 @@ edition = "2021" plib = { path = "../plib" } clap = { version = "4", features = ["derive"] } gettext-rs = { version = "0.7", features = ["gettext-system"] } +libc = "0.2" [[bin]] name = "env" path = "src/env.rs" +[[bin]] +name = "nice" +path = "src/nice.rs" + diff --git a/process/src/nice.rs b/process/src/nice.rs new file mode 100644 index 0000000..6406d85 --- /dev/null +++ b/process/src/nice.rs @@ -0,0 +1,62 @@ +// +// Copyright (c) 2024 Jeff Garzik +// +// This file is part of the posixutils-rs project covered under +// the MIT License. For the full license text, please see the LICENSE +// file in the root directory of this project. +// SPDX-License-Identifier: MIT +// + +extern crate clap; +extern crate libc; +extern crate plib; + +use clap::Parser; +use gettextrs::{bind_textdomain_codeset, textdomain}; +use plib::PROJECT_NAME; +use std::io; +use std::os::unix::process::CommandExt; +use std::process::{Command, Stdio}; + +/// nice - invoke a utility with an altered nice value +#[derive(Parser, Debug)] +#[command(author, version, about, long_about)] +struct Args { + /// A positive or negative decimal integer which shall have the same effect on the execution of the utility as if the utility had called the nice() function with the numeric value of the increment option-argument. + #[arg(short, long, default_value_t=10, value_parser = clap::value_parser!(i32).range(-30..30))] + niceval: i32, + + /// utility to invoke + util: String, + + /// utility arguments + util_args: Vec, +} + +fn exec_util(util: &str, util_args: Vec) -> io::Result<()> { + Err(Command::new(util) + .args(util_args) + .stdin(Stdio::inherit()) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .exec()) +} + +fn main() -> Result<(), Box> { + // parse command line arguments + let args = Args::parse(); + + textdomain(PROJECT_NAME)?; + bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?; + + let res = unsafe { libc::nice(args.niceval) }; + if res < 0 { + let e = io::Error::from_raw_os_error(res); + eprintln!("nice: {}", e); + return Err(Box::new(e)); + } + + exec_util(&args.util, args.util_args)?; + + Ok(()) +}