Compare commits

...

5 Commits

Author SHA1 Message Date
DTB
b7bc1f16ad
swab(1): use the getopt error message 2024-07-08 14:34:42 -06:00
ca6865688a
swab(1): updates getopt usage 2024-07-08 13:23:23 -06:00
DTB
1fd768057c
swab(1): don't accept positional arguments 2024-07-08 11:45:01 -06:00
DTB
35d54d84b0
swab(1): don't use the getopt error message 2024-07-08 11:31:35 -06:00
DTB
a141b95293
swab(1): remove -f 2024-07-08 11:30:21 -06:00
2 changed files with 13 additions and 14 deletions

View File

@ -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

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2024 DTB <trinity@trinity.moe>
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
@ -18,7 +19,7 @@
use std::{
env::args,
io::{ stdin, stdout, Error, ErrorKind, Read, Write },
io::{ stdin, stdout, Error, Read, Write },
process::ExitCode,
vec::Vec
};
@ -38,7 +39,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,24 +49,26 @@ 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>() {
Ok(w) if w % 2 == 0 => { wordsize = w; () },
match opt.arg().unwrap().parse::<usize>() {
Ok(w) if w % 2 == 0 => { wordsize = w; },
_ => { 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 +86,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)
}
}