-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from jgarzik/hacking
add util: nice
- Loading branch information
Showing
4 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String>, | ||
} | ||
|
||
fn exec_util(util: &str, util_args: Vec<String>) -> io::Result<()> { | ||
Err(Command::new(util) | ||
.args(util_args) | ||
.stdin(Stdio::inherit()) | ||
.stdout(Stdio::inherit()) | ||
.stderr(Stdio::inherit()) | ||
.exec()) | ||
} | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// 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(()) | ||
} |