Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an initial implementation of xargs #121

Merged
merged 1 commit into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ authors = ["uutils developers"]

[dependencies]
chrono = "0.4"
clap = "2.34"
glob = "0.3"
walkdir = "2.3"
tempfile = "3"
Expand All @@ -29,6 +30,10 @@ serial_test = "0.5"
name = "find"
path = "src/find/main.rs"

[[bin]]
name = "xargs"
path = "src/xargs/main.rs"

[[bin]]
name = "testing-commandline"
path = "src/testing/commandline/main.rs"
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ extern crate walkdir;
extern crate tempfile;

pub mod find;
pub mod xargs;
89 changes: 69 additions & 20 deletions src/testing/commandline/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@

use std::env;
use std::fs::{self, File, OpenOptions};
use std::io::Write;
use std::io::{stdin, stdout, Read, Write};
use std::path::PathBuf;

fn usage() -> ! {
println!("Simple command-line app just used for testing -exec flags!");
std::process::exit(2);
}

enum ExitWith {
Failure,
UrgentFailure,
#[cfg(unix)]
Signal,
}

#[derive(Default)]
struct Config {
exit_with_failure: bool,
destination_dir: String,
exit_with: Option<ExitWith>,
print_stdin: bool,
no_print_cwd: bool,
destination_dir: Option<String>,
}

fn open_file(destination_dir: &str) -> File {
Expand All @@ -39,20 +48,61 @@ fn open_file(destination_dir: &str) -> File {
}
}

fn write_content(mut f: impl Write, config: &Config, args: &[String]) {
if !config.no_print_cwd {
writeln!(f, "cwd={}", env::current_dir().unwrap().to_string_lossy())
.expect("failed to write to file");
}

if config.print_stdin {
let mut s = String::new();
stdin()
.read_to_string(&mut s)
.expect("failed to read from stdin");
writeln!(f, "stdin={}", s.trim()).expect("failed to write to file");
}

writeln!(f, "args=").expect("failed to write to file");

// first two args are going to be the path to this executable and
// the destination_dir we want to write to. Don't write either of those
// as they'll be non-deterministic.
for arg in &args[2..] {
writeln!(f, "{}", arg).expect("failed to write to file");
}
}

fn main() {
let args = env::args().collect::<Vec<String>>();
if args.len() < 2 || args[1] == "-h" || args[1] == "--help" {
usage();
}
let mut config = Config {
destination_dir: args[1].clone(),
destination_dir: if args[1] != "-" {
Some(args[1].clone())
} else {
None
},
..Default::default()
};
for arg in &args[2..] {
if arg.starts_with("--") {
match arg.as_ref() {
"--exit_with_failure" => {
config.exit_with_failure = true;
config.exit_with = Some(ExitWith::Failure);
}
"--exit_with_urgent_failure" => {
config.exit_with = Some(ExitWith::UrgentFailure);
}
#[cfg(unix)]
"--exit_with_signal" => {
config.exit_with = Some(ExitWith::Signal);
}
"--no_print_cwd" => {
config.no_print_cwd = true;
}
"--print_stdin" => {
config.print_stdin = true;
}
_ => {
usage();
Expand All @@ -61,20 +111,19 @@ fn main() {
}
}

{
let mut f = open_file(&config.destination_dir);
// first two args are going to be the path to this executable and
// the destination_dir we want to write to. Don't write either of those
// as they'll be non-deterministic.
f.write_fmt(format_args!(
"cwd={}\nargs=\n",
env::current_dir().unwrap().to_string_lossy()
))
.expect("failed to write to file");
for arg in &args[2..] {
f.write_fmt(format_args!("{}\n", arg))
.expect("failed to write to file");
}
if let Some(destination_dir) = &config.destination_dir {
write_content(open_file(destination_dir), &config, &args);
} else {
write_content(stdout(), &config, &args);
}

match config.exit_with {
None => std::process::exit(0),
Some(ExitWith::Failure) => std::process::exit(2),
Some(ExitWith::UrgentFailure) => std::process::exit(255),
#[cfg(unix)]
Some(ExitWith::Signal) => unsafe {
uucore::libc::raise(uucore::libc::SIGINT);
},
}
std::process::exit(if config.exit_with_failure { 2 } else { 0 });
}
12 changes: 12 additions & 0 deletions src/xargs/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2021 Collabora, Ltd.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

fn main() {
let args = std::env::args().collect::<Vec<String>>();
std::process::exit(findutils::xargs::xargs_main(
&args.iter().map(|s| s.as_ref()).collect::<Vec<&str>>(),
))
}
Loading