Compare commits
3 Commits
bf06e91be5
...
1fd768057c
Author | SHA1 | Date | |
---|---|---|---|
1fd768057c | |||
35d54d84b0 | |||
a141b95293 |
@ -11,7 +11,6 @@ swab \(en swap bytes
|
||||
.SH SYNOPSIS
|
||||
|
||||
swab
|
||||
.RB [ -f ]
|
||||
.RB [ -w\ word_size ]
|
||||
.\"
|
||||
.SH DESCRIPTION
|
||||
@ -20,8 +19,6 @@ Swap the latter and former halves of a block of bytes.
|
||||
.\"
|
||||
.SH OPTIONS
|
||||
|
||||
.IP \fB-f\fP
|
||||
Ignore SIGINT signal.
|
||||
.IP \fB-w\fP\ \fIword_size\fP
|
||||
Configures the word size; that is, the size in bytes of the block size on which
|
||||
to operate. The default word size is 2. The word size must be cleanly divisible
|
||||
|
17
src/swab.rs
17
src/swab.rs
@ -18,7 +18,7 @@
|
||||
|
||||
use std::{
|
||||
env::args,
|
||||
io::{ stdin, stdout, Error, ErrorKind, Read, Write },
|
||||
io::{ stdin, stdout, Error, Read, Write },
|
||||
process::ExitCode,
|
||||
vec::Vec
|
||||
};
|
||||
@ -38,7 +38,7 @@ fn oserr(s: &str, e: Error) -> ExitCode {
|
||||
}
|
||||
|
||||
fn usage(s: &str) -> ExitCode {
|
||||
eprintln!("Usage: {} [-f] [-w word_size]", s);
|
||||
eprintln!("Usage: {} [-w word_size]", s);
|
||||
ExitCode::from(EX_USAGE as u8)
|
||||
}
|
||||
|
||||
@ -48,12 +48,11 @@ fn main() -> ExitCode {
|
||||
let mut input = stdin();
|
||||
let mut output = stdout().lock();
|
||||
|
||||
let mut force = false;
|
||||
let mut wordsize: usize = 2;
|
||||
let mut optind: usize = 1; // argv[0]
|
||||
let mut wordsize: usize = 2; // Equivalent to dd(1p).
|
||||
|
||||
while let Some(opt) = argv.getopt("fw:") {
|
||||
while let Some(opt) = argv.getopt(":w:") {
|
||||
match opt.opt() {
|
||||
Ok("f") => force = true,
|
||||
Ok("w") => {
|
||||
if let Some(arg) = opt.arg() {
|
||||
match arg.parse::<usize>() {
|
||||
@ -61,11 +60,16 @@ fn main() -> ExitCode {
|
||||
_ => { return usage(&argv[0]); },
|
||||
}
|
||||
}
|
||||
optind = opt.ind();
|
||||
},
|
||||
_ => { return usage(&argv[0]); }
|
||||
}
|
||||
}
|
||||
|
||||
if optind < argv.len() {
|
||||
return usage(&argv[0]);
|
||||
}
|
||||
|
||||
buf.resize(wordsize, 0);
|
||||
|
||||
loop {
|
||||
@ -83,7 +87,6 @@ fn main() -> ExitCode {
|
||||
break oserr(&argv[0], e)
|
||||
}
|
||||
},
|
||||
Err(e) if e.kind() == ErrorKind::Interrupted && force => continue,
|
||||
Err(e) => break oserr(&argv[0], e)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user