Skip to content

Commit

Permalink
Merge pull request #23 from jgarzik/hacking
Browse files Browse the repository at this point in the history
add util: nice
  • Loading branch information
jgarzik authored Mar 3, 2024
2 parents 2e9f2d0 + 1a87267 commit 49bd108
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ https://github.com/jgarzik/posixutils
- [ ] more
- [ ] mv
- [ ] newgrp
- [ ] nice
- [x] nice
- [ ] nl
- [ ] nm (Development)
- [ ] nohup
Expand Down
5 changes: 5 additions & 0 deletions process/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

62 changes: 62 additions & 0 deletions process/src/nice.rs
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(())
}

0 comments on commit 49bd108

Please sign in to comment.