Add mkfifo applet

This commit is contained in:
Nathan Fisher 2023-01-20 23:53:28 -05:00
parent 091bf713bd
commit e9e9c53f19
3 changed files with 77 additions and 1 deletions

View File

@ -34,6 +34,7 @@ code between applets, making for an overall smaller binary.
- head - head
- hostname - hostname
- link - link
- mkfifo
- mountpoint - mountpoint
- nologin - nologin
- nproc - nproc

72
src/cmd/mkfifo/mod.rs Normal file
View File

@ -0,0 +1,72 @@
use super::Cmd;
use crate::mode::Parser;
use clap::{Arg, ArgAction, ArgMatches, Command};
use std::{error::Error, ffi::CString, io};
#[derive(Debug, Default)]
pub struct MkFifo;
impl Cmd for MkFifo {
fn cli(&self) -> clap::Command {
Command::new("mkfifo")
.about("make FIFO special files")
.author("Nathan Fisher")
.version(env!("CARGO_PKG_VERSION"))
.args([
Arg::new("mode")
.help(
"Set the file permission bits of the newly-created \
FIFO to the specified mode value.",
)
.long_help(
"Set the file permission bits of the newly-created \
FIFO to the specified mode value. The mode option-argument \
shall be the same as the mode operand defined for the \
chmod utility. In the symbolic_mode strings, the op \
characters '+' and '-' shall be interpreted relative to \
an assumed initial mode of a=rw.",
)
.short('m')
.long("mode")
.value_name("MODE")
.num_args(1),
Arg::new("verbose")
.help("print a diagnostic for every pipe created")
.short('v')
.long("verbose")
.action(ArgAction::SetTrue),
Arg::new("file").num_args(1..).required(true),
])
}
fn run(&self, matches: Option<&ArgMatches>) -> Result<(), Box<dyn Error>> {
let Some(matches) = matches else {
return Err(io::Error::new(io::ErrorKind::Other, "no input").into());
};
let mode = if let Some(m) = matches.get_one::<String>("mode") {
Parser::new(0o666).parse(m)?
} else {
0o666
};
if let Some(files) = matches.get_many::<String>("file") {
for f in files {
let fname = CString::new(f.as_str())?;
let res = unsafe { libc::mkfifo(fname.as_ptr(), mode) };
if res != 0 {
return Err(io::Error::last_os_error().into());
}
if matches.get_flag("verbose") {
println!(
"{}: created pipe '{f}' with mode {mode:o}",
self.cli().get_name()
);
}
}
}
Ok(())
}
fn path(&self) -> Option<crate::Path> {
Some(crate::Path::UsrBin)
}
}

View File

@ -25,6 +25,7 @@ mod hostname;
mod link; mod link;
mod ln; mod ln;
mod ls; mod ls;
mod mkfifo;
mod mountpoint; mod mountpoint;
mod mv; mod mv;
mod nologin; mod nologin;
@ -77,6 +78,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
"groups" => Some(Box::new(groups::Groups::default())), "groups" => Some(Box::new(groups::Groups::default())),
"head" => Some(Box::new(head::Head::default())), "head" => Some(Box::new(head::Head::default())),
"link" => Some(Box::new(link::Link::default())), "link" => Some(Box::new(link::Link::default())),
"mkfifo" => Some(Box::new(mkfifo::MkFifo::default())),
"mountpoint" => Some(Box::new(mountpoint::Mountpoint::default())), "mountpoint" => Some(Box::new(mountpoint::Mountpoint::default())),
"nologin" => Some(Box::new(nologin::Nologin::default())), "nologin" => Some(Box::new(nologin::Nologin::default())),
"nproc" => Some(Box::new(nproc::Nproc::default())), "nproc" => Some(Box::new(nproc::Nproc::default())),
@ -96,7 +98,7 @@ pub fn get(name: &str) -> Option<Box<dyn Cmd>> {
} }
} }
pub static COMMANDS: [&str; 32] = [ pub static COMMANDS: [&str; 33] = [
"base32", "base32",
"base64", "base64",
"basename", "basename",
@ -114,6 +116,7 @@ pub static COMMANDS: [&str; 32] = [
"head", "head",
"hostname", "hostname",
"link", "link",
"mkfifo",
"mountpoint", "mountpoint",
"nologin", "nologin",
"nproc", "nproc",