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