From 9a7af682b722f031b0d7c77e6f8cd42c1b3790ef Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Sun, 22 Jan 2023 10:24:21 -0500 Subject: [PATCH] Add `readlink` applet --- README.md | 1 + src/cmd/mod.rs | 5 +++- src/cmd/readlink/mod.rs | 62 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/cmd/readlink/mod.rs diff --git a/README.md b/README.md index 96f6e1e..3d998c1 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ code between applets, making for an overall smaller binary. - printenv - pwd - readlink +- realpath - rev - rm - rmdir diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 454a0d8..9d11db0 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -35,6 +35,7 @@ mod nologin; mod nproc; mod printenv; mod pwd; +mod readlink; mod realpath; mod rev; mod rm; @@ -93,6 +94,7 @@ pub fn get(name: &str) -> Option> { "nproc" => Some(Box::new(nproc::Nproc::default())), "printenv" => Some(Box::new(printenv::Printenv::default())), "pwd" => Some(Box::new(pwd::Pwd::default())), + "readlink" => Some(Box::new(readlink::Readlink::default())), "realpath" => Some(Box::new(realpath::Realpath::default())), "rev" => Some(Box::new(rev::Rev::default())), "rm" => Some(Box::new(rm::Rm::default())), @@ -109,7 +111,7 @@ pub fn get(name: &str) -> Option> { } } -pub static COMMANDS: [&str; 39] = [ +pub static COMMANDS: [&str; 40] = [ "base32", "base64", "basename", @@ -137,6 +139,7 @@ pub static COMMANDS: [&str; 39] = [ "nproc", "printenv", "pwd", + "readlink", "realpath", "rev", "rm", diff --git a/src/cmd/readlink/mod.rs b/src/cmd/readlink/mod.rs new file mode 100644 index 0000000..ace6c2c --- /dev/null +++ b/src/cmd/readlink/mod.rs @@ -0,0 +1,62 @@ +use crate::Cmd; +use clap::{Arg, ArgAction, ArgMatches, Command, ValueHint}; +use std::{fs, io, path::PathBuf}; + +#[derive(Debug, Default)] +pub struct Readlink; + +impl Cmd for Readlink { + fn cli(&self) -> clap::Command { + Command::new("readlink") + .about("Print symbolic link target or canonical file name") + .version(env!("CARGO_PKG_VERSION")) + .author("Nathan Fisher") + .args([ + Arg::new("path") + .value_name("PATH") + .value_hint(ValueHint::AnyPath) + .num_args(1..) + .required(true), + Arg::new("canon") + .help("Canonicalize path") + .short('f') + .visible_short_alias('c') + .long("canonicalize") + .action(ArgAction::SetTrue), + Arg::new("newline") + .help("Do not print the terminating newline.") + .short('n') + .long("no-newline") + .action(ArgAction::SetTrue), + ]) + } + + fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box> { + let Some(matches) = matches else { + return Err(io::Error::new(io::ErrorKind::Other, "no input").into()); + }; + if let Some(paths) = matches.get_many::("path") { + let paths: Vec<_> = paths.collect(); + let len = paths.len(); + if matches.get_flag("newline") && len > 1 { + eprintln!("readlink: ignoring --no-newline with multiple arguments"); + } + for p in paths { + let path = if matches.get_flag("canon") { + PathBuf::from(p).canonicalize()? + } else { + fs::read_link(p)? + }; + print!("{}", path.display()); + if !matches.get_flag("newline") || len > 1 { + println!(); + } + } + } + Ok(()) + } + + fn path(&self) -> Option { + Some(crate::Path::UsrBin) + } +}