mm(1): adds ioerr(), usage(), and oser() functions
This commit is contained in:
parent
fbacfecce8
commit
dd39aeff02
51
src/mm.rs
51
src/mm.rs
@ -20,9 +20,9 @@
|
|||||||
use std::{
|
use std::{
|
||||||
env::args,
|
env::args,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{ stdin, stdout, stderr, BufWriter, Read, Write },
|
io::{ Error, BufWriter, Read, Write, stderr, stdin, stdout },
|
||||||
os::fd::{ AsRawFd, FromRawFd },
|
os::fd::{ AsRawFd, FromRawFd },
|
||||||
process::{ exit, ExitCode },
|
process::{ ExitCode, exit},
|
||||||
};
|
};
|
||||||
|
|
||||||
extern crate getopt;
|
extern crate getopt;
|
||||||
@ -47,15 +47,29 @@ use ArgMode::*;
|
|||||||
|
|
||||||
enum ArgMode { In, Out }
|
enum ArgMode { In, Out }
|
||||||
|
|
||||||
|
fn ioerr(argv0: &String, e: Error) -> ExitCode {
|
||||||
|
eprintln!("{}: {}", argv0, e.strerror());
|
||||||
|
ExitCode::from(EX_IOERR as u8)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")]
|
||||||
|
fn oserr(argv0: &String, e: Error) -> ExitCode {
|
||||||
|
eprintln!("{}: {}", argv0, e.strerror());
|
||||||
|
ExitCode::from(EX_OSERR as u8)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(argv0: &String) -> ExitCode {
|
||||||
|
eprintln!("Usage: {} [-aetu] [-i input] [-o output]", argv0);
|
||||||
|
ExitCode::from(EX_USAGE as u8)
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
let argv = args().collect::<Vec<_>>();
|
let argv = args().collect::<Vec<_>>();
|
||||||
let usage = format!("Usage: {} [-aetu] [-i input] [-o output]", argv[0]);
|
|
||||||
|
|
||||||
#[cfg(target_os="openbsd")] {
|
#[cfg(target_os="openbsd")] {
|
||||||
let promises = Promises::new("cpath rpath stdio unveil wpath");
|
let promises = Promises::new("cpath rpath stdio unveil wpath");
|
||||||
if let Err(e) = pledge(Some(promises), None) {
|
if let Err(e) = pledge(Some(promises), None) {
|
||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
return oserr(&argv[0], e);
|
||||||
return ExitCode::from(EX_OSERR as u8);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,8 +99,7 @@ fn main() -> ExitCode {
|
|||||||
mode = Some(Out); /* latest argument == -o */
|
mode = Some(Out); /* latest argument == -o */
|
||||||
},
|
},
|
||||||
Err(_) | Ok(_) => {
|
Err(_) | Ok(_) => {
|
||||||
eprintln!("{}", usage);
|
return usage(&argv[0]);
|
||||||
return ExitCode::from(EX_USAGE as u8);
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -111,8 +124,7 @@ fn main() -> ExitCode {
|
|||||||
let perms = UnveilPerms::new(vec!['r']);
|
let perms = UnveilPerms::new(vec!['r']);
|
||||||
|
|
||||||
if let Err(e) = unveil(Some(&input), Some(perms)) {
|
if let Err(e) = unveil(Some(&input), Some(perms)) {
|
||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
return oserr(&argv[0], e);
|
||||||
return ExitCode::from(EX_OSERR as u8);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,20 +132,17 @@ fn main() -> ExitCode {
|
|||||||
let perms = UnveilPerms::new(vec!['c', 'w']);
|
let perms = UnveilPerms::new(vec!['c', 'w']);
|
||||||
|
|
||||||
if let Err(e) = unveil(Some(&output), Some(perms)) {
|
if let Err(e) = unveil(Some(&output), Some(perms)) {
|
||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
return oserr(&argv[0], e);
|
||||||
return ExitCode::from(EX_OSERR as u8);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Err(e) = unveil(None, None) {
|
if let Err(e) = unveil(None, None) {
|
||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
return oserr(&argv[0], e);
|
||||||
return ExitCode::from(EX_OSERR as u8);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ins.is_empty() && outs.is_empty() && argv.len() > optind {
|
if ins.is_empty() && outs.is_empty() && argv.len() > optind {
|
||||||
eprintln!("Usage: {}", usage);
|
return usage(&argv[0]);
|
||||||
return ExitCode::from(EX_USAGE as u8);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* use stdin if no inputs are specified */
|
/* use stdin if no inputs are specified */
|
||||||
@ -153,7 +162,7 @@ fn main() -> ExitCode {
|
|||||||
match File::open(file) {
|
match File::open(file) {
|
||||||
Ok(f) => f,
|
Ok(f) => f,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("{}: {}: {}", argv[0], file, e.strerror());
|
let _ = ioerr(&argv[0], e);
|
||||||
exit(EX_IOERR);
|
exit(EX_IOERR);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -180,7 +189,7 @@ fn main() -> ExitCode {
|
|||||||
match options {
|
match options {
|
||||||
Ok(f) => return f,
|
Ok(f) => return f,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("{}: {}: {}", argv[0], file, e.strerror());
|
let _ = ioerr(&argv[0], e);
|
||||||
exit(EX_IOERR);
|
exit(EX_IOERR);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@ -205,21 +214,19 @@ fn main() -> ExitCode {
|
|||||||
for file in inputs {
|
for file in inputs {
|
||||||
for byte in file.bytes().map(|b| {
|
for byte in file.bytes().map(|b| {
|
||||||
b.unwrap_or_else(|e| {
|
b.unwrap_or_else(|e| {
|
||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
let _ = ioerr(&argv[0], e);
|
||||||
exit(EX_IOERR);
|
exit(EX_IOERR);
|
||||||
})
|
})
|
||||||
}) {
|
}) {
|
||||||
for out in &mut outputs {
|
for out in &mut outputs {
|
||||||
if let Err(e) = out.write(&[byte]) {
|
if let Err(e) = out.write(&[byte]) {
|
||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
return ioerr(&argv[0], e);
|
||||||
return ExitCode::from(EX_IOERR as u8);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if u {
|
if u {
|
||||||
/* immediately flush the output for -u */
|
/* immediately flush the output for -u */
|
||||||
if let Err(e) = out.flush() {
|
if let Err(e) = out.flush() {
|
||||||
eprintln!("{}: {}", argv[0], e.strerror());
|
return ioerr(&argv[0], e);
|
||||||
return ExitCode::from(EX_IOERR as u8);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user