Compare commits

..

No commits in common. "b7bc1f16ad4047e6eeb1d48c3d1066b69b0b5d3a" and "bf06e91be5c2779b9f40309a821569c85f29e45c" have entirely different histories.

2 changed files with 14 additions and 13 deletions

View File

@ -11,6 +11,7 @@ swab \(en swap bytes
.SH SYNOPSIS .SH SYNOPSIS
swab swab
.RB [ -f ]
.RB [ -w\ word_size ] .RB [ -w\ word_size ]
.\" .\"
.SH DESCRIPTION .SH DESCRIPTION
@ -19,6 +20,8 @@ Swap the latter and former halves of a block of bytes.
.\" .\"
.SH OPTIONS .SH OPTIONS
.IP \fB-f\fP
Ignore SIGINT signal.
.IP \fB-w\fP\ \fIword_size\fP .IP \fB-w\fP\ \fIword_size\fP
Configures the word size; that is, the size in bytes of the block size on which 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 to operate. The default word size is 2. The word size must be cleanly divisible

View File

@ -1,6 +1,5 @@
/* /*
* Copyright (c) 2024 DTB <trinity@trinity.moe> * Copyright (c) 2024 DTB <trinity@trinity.moe>
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: AGPL-3.0-or-later * SPDX-License-Identifier: AGPL-3.0-or-later
* *
* This program is free software: you can redistribute it and/or modify it under * This program is free software: you can redistribute it and/or modify it under
@ -19,7 +18,7 @@
use std::{ use std::{
env::args, env::args,
io::{ stdin, stdout, Error, Read, Write }, io::{ stdin, stdout, Error, ErrorKind, Read, Write },
process::ExitCode, process::ExitCode,
vec::Vec vec::Vec
}; };
@ -39,7 +38,7 @@ fn oserr(s: &str, e: Error) -> ExitCode {
} }
fn usage(s: &str) -> ExitCode { fn usage(s: &str) -> ExitCode {
eprintln!("Usage: {} [-w word_size]", s); eprintln!("Usage: {} [-f] [-w word_size]", s);
ExitCode::from(EX_USAGE as u8) ExitCode::from(EX_USAGE as u8)
} }
@ -49,26 +48,24 @@ fn main() -> ExitCode {
let mut input = stdin(); let mut input = stdin();
let mut output = stdout().lock(); let mut output = stdout().lock();
let mut optind: usize = 1; // argv[0] let mut force = false;
let mut wordsize: usize = 2; // Equivalent to dd(1p). let mut wordsize: usize = 2;
while let Some(opt) = argv.getopt("w:") { while let Some(opt) = argv.getopt("fw:") {
match opt.opt() { match opt.opt() {
Ok("f") => force = true,
Ok("w") => { Ok("w") => {
match opt.arg().unwrap().parse::<usize>() { if let Some(arg) = opt.arg() {
Ok(w) if w % 2 == 0 => { wordsize = w; }, match arg.parse::<usize>() {
Ok(w) if w % 2 == 0 => { wordsize = w; () },
_ => { return usage(&argv[0]); }, _ => { return usage(&argv[0]); },
} }
optind = opt.ind(); }
}, },
_ => { return usage(&argv[0]); } _ => { return usage(&argv[0]); }
} }
} }
if optind < argv.len() {
return usage(&argv[0]);
}
buf.resize(wordsize, 0); buf.resize(wordsize, 0);
loop { loop {
@ -86,6 +83,7 @@ fn main() -> ExitCode {
break oserr(&argv[0], e) break oserr(&argv[0], e)
} }
}, },
Err(e) if e.kind() == ErrorKind::Interrupted && force => continue,
Err(e) => break oserr(&argv[0], e) Err(e) => break oserr(&argv[0], e)
} }
} }