mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
cksum: make the output formatting match GNU
This commit is contained in:
parent
231987b109
commit
037073d852
1 changed files with 24 additions and 13 deletions
|
@ -111,6 +111,7 @@ struct Options {
|
||||||
digest: Box<dyn Digest + 'static>,
|
digest: Box<dyn Digest + 'static>,
|
||||||
output_bits: usize,
|
output_bits: usize,
|
||||||
untagged: bool,
|
untagged: bool,
|
||||||
|
length: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculate checksum
|
/// Calculate checksum
|
||||||
|
@ -174,8 +175,13 @@ where
|
||||||
(ALGORITHM_OPTIONS_CRC, true) => println!("{sum} {sz}"),
|
(ALGORITHM_OPTIONS_CRC, true) => println!("{sum} {sz}"),
|
||||||
(ALGORITHM_OPTIONS_CRC, false) => println!("{sum} {sz} {}", filename.display()),
|
(ALGORITHM_OPTIONS_CRC, false) => println!("{sum} {sz} {}", filename.display()),
|
||||||
(ALGORITHM_OPTIONS_BLAKE2B, _) if !options.untagged => {
|
(ALGORITHM_OPTIONS_BLAKE2B, _) if !options.untagged => {
|
||||||
|
if let Some(length) = options.length {
|
||||||
|
// Multiply by 8 here, as we want to print the length in bits.
|
||||||
|
println!("BLAKE2b-{} ({}) = {sum}", length * 8, filename.display());
|
||||||
|
} else {
|
||||||
println!("BLAKE2b ({}) = {sum}", filename.display());
|
println!("BLAKE2b ({}) = {sum}", filename.display());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if options.untagged {
|
if options.untagged {
|
||||||
println!("{sum} {}", filename.display());
|
println!("{sum} {}", filename.display());
|
||||||
|
@ -244,6 +250,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
|
|
||||||
let input_length = matches.get_one::<usize>(options::LENGTH);
|
let input_length = matches.get_one::<usize>(options::LENGTH);
|
||||||
let length = if let Some(length) = input_length {
|
let length = if let Some(length) = input_length {
|
||||||
|
|
||||||
|
if length % 8 != 0 {
|
||||||
|
// GNU's implementation seem to use these quotation marks
|
||||||
|
// in their error messages, so we do the same.
|
||||||
|
uucore::show_error!("invalid length: \u{2018}{length}\u{2019}");
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidInput,
|
||||||
|
"length is not a multiple of 8",
|
||||||
|
)
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
|
||||||
if algo_name != ALGORITHM_OPTIONS_BLAKE2B {
|
if algo_name != ALGORITHM_OPTIONS_BLAKE2B {
|
||||||
return Err(io::Error::new(
|
return Err(io::Error::new(
|
||||||
io::ErrorKind::InvalidInput,
|
io::ErrorKind::InvalidInput,
|
||||||
|
@ -252,16 +270,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
.into());
|
.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
if length % 8 != 0 {
|
// Divide by 8, as our blake2b implementation expects bytes
|
||||||
return Err(io::Error::new(
|
// instead of bits.
|
||||||
io::ErrorKind::InvalidInput,
|
|
||||||
// TODO: Fix formatting for this error message
|
|
||||||
// GNU has the executable path/name on both lines
|
|
||||||
format!("invalid length {length}\nlength is not a multiple of 8"),
|
|
||||||
)
|
|
||||||
.into());
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(length / 8)
|
Some(length / 8)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -273,6 +283,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
algo_name: name,
|
algo_name: name,
|
||||||
digest: algo,
|
digest: algo,
|
||||||
output_bits: bits,
|
output_bits: bits,
|
||||||
|
length,
|
||||||
untagged: matches.get_flag(options::UNTAGGED),
|
untagged: matches.get_flag(options::UNTAGGED),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue