optimizations #161

Open
emma wants to merge 44 commits from optimizations into main
2 changed files with 22 additions and 22 deletions
Showing only changes of commit f104598164 - Show all commits

View File

@ -47,20 +47,14 @@ use ArgMode::*;
enum ArgMode { In, Out } enum ArgMode { In, Out }
fn ioerr(argv0: &String, e: Error) -> ExitCode { fn err(argv0: &String, e: Error, code: Option<u8>) -> ExitCode {
eprintln!("{}: {}", argv0, e.strerror()); eprintln!("{}: {}", argv0, e.strerror());
ExitCode::from(EX_IOERR as u8) ExitCode::from(code.unwrap_or(1 /* unknown error */))
}
#[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 { fn usage(argv0: &String) -> ExitCode {
eprintln!("Usage: {} [-aetu] [-i input] [-o output]", argv0); eprintln!("Usage: {} [-aetu] [-i input] [-o output]", argv0);
ExitCode::from(EX_USAGE as u8) ExitCode::from(EX_USAGE)
} }
fn main() -> ExitCode { fn main() -> ExitCode {
@ -69,7 +63,7 @@ fn main() -> ExitCode {
#[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) {
return oserr(&argv[0], e); return err(&argv[0], e, Some(EX_OSERR));
} }
} }
@ -124,7 +118,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)) {
return oserr(&argv[0], e); return err(&argv[0], e, Some(EX_OSERR));
} }
} }
@ -132,12 +126,12 @@ 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)) {
return oserr(&argv[0], e); return err(&argv[0], e, Some(EX_OSERR));
} }
} }
if let Err(e) = unveil(None, None) { if let Err(e) = unveil(None, None) {
return oserr(&argv[0], e); return err(&argv[0], e, Some(EX_OSERR));
} }
} }
@ -162,8 +156,8 @@ fn main() -> ExitCode {
match File::open(file) { match File::open(file) {
Ok(f) => f, Ok(f) => f,
Err(e) => { Err(e) => {
let _ = ioerr(&argv[0], e); let _ = err(&(argv[0].clone() + ": " + file), e, None);
exit(EX_IOERR); exit(EX_IOERR.into());
}, },
} }
}).collect::<Vec<_>>(); }).collect::<Vec<_>>();
@ -189,8 +183,8 @@ fn main() -> ExitCode {
match options { match options {
Ok(f) => return f, Ok(f) => return f,
Err(e) => { Err(e) => {
let _ = ioerr(&argv[0], e); let _ = err(&(argv[0].clone() + ": " + file), e, None);
exit(EX_IOERR); exit(EX_IOERR.into());
}, },
}; };
}).collect::<Vec<_>>(); }).collect::<Vec<_>>();
@ -214,19 +208,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| {
let _ = ioerr(&argv[0], e); let _ = err(&argv[0], e, None);
exit(EX_IOERR); exit(EX_IOERR.into());
}) })
}) { }) {
for out in &mut outputs { for out in &mut outputs {
if let Err(e) = out.write(&[byte]) { if let Err(e) = out.write(&[byte]) {
return ioerr(&argv[0], e); return err(&argv[0], e, Some(EX_IOERR));
} }
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() {
return ioerr(&argv[0], e); return err(&argv[0], e, Some(EX_IOERR));
} }
} }
} }

View File

@ -6,7 +6,7 @@
# notice are preserved. This file is offered as-is, without any warranty. # notice are preserved. This file is offered as-is, without any warranty.
.PHONY: mm_tests .PHONY: mm_tests
mm_tests: mm_args mm_help mm_stderr mm_remaining mm_tests: mm_args mm_help mm_stderr mm_remaining mm_remaining_options
.PHONY: mm_none .PHONY: mm_none
mm_none: $(BIN)/mm mm_none: $(BIN)/mm
@ -32,3 +32,9 @@ mm_remaining: $(BIN)/mm
test "$$($(BIN)/mm -i README COPYING)" = "$$(cat README COPYING)" test "$$($(BIN)/mm -i README COPYING)" = "$$(cat README COPYING)"
$(BIN)/mm -i README -o /tmp/mm_test0 /tmp/mm_test1 $(BIN)/mm -i README -o /tmp/mm_test0 /tmp/mm_test1
diff /tmp/mm_test0 /tmp/mm_test1 diff /tmp/mm_test0 /tmp/mm_test1
.PHONY: mm_remaining_options
# check to make sure mm -i with trailing arguments interprets -o as one
mm_remaining_options:
! $(BIN)/mm -i README COPYING -o - 2>&1 | cut -d: -f2 \
| xargs test " -o" =