Skip to content

Commit

Permalink
dd: treat arg as bytes if it contains 'B'
Browse files Browse the repository at this point in the history
  • Loading branch information
matrixhead committed Mar 9, 2024
1 parent b34d410 commit 1819cde
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/uu/dd/src/parseargs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl Parser {

fn parse_n(val: &str) -> Result<Num, ParseError> {
let n = parse_bytes_with_opt_multiplier(val)?;
Ok(if val.ends_with('B') {
Ok(if val.contains('B') {
Num::Bytes(n)
} else {
Num::Blocks(n)
Expand Down Expand Up @@ -631,8 +631,9 @@ fn conversion_mode(
#[cfg(test)]
mod tests {

use crate::parseargs::parse_bytes_with_opt_multiplier;

use crate::parseargs::{parse_bytes_with_opt_multiplier, Parser};
use crate::Num;
use std::matches;
const BIG: &str = "9999999999999999999999999999999999999999999999999999999999999";

#[test]
Expand All @@ -659,4 +660,13 @@ mod tests {
2 * 2 * (3 * 2) // (1 * 2) * (2 * 1) * (3 * 2)
);
}
#[test]
fn test_parse_n() {
for arg in ["1x8x4", "1c", "123b", "123w"] {
assert!(matches!(Parser::parse_n(arg), Ok(Num::Blocks(_))));
}
for arg in ["1Bx8x4", "2Bx8", "2Bx8B", "2x8B"] {
assert!(matches!(Parser::parse_n(arg), Ok(Num::Bytes(_))));
}
}
}
30 changes: 30 additions & 0 deletions tests/by-util/test_dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,36 @@ fn test_bytes_suffix() {
.stdout_only("\0\0\0abcdef");
}

#[test]
// the recursive nature of the suffix allows any string with a 'B' in it treated as bytes.
fn test_bytes_suffix_recursive() {
new_ucmd!()
.args(&["count=2Bx2", "status=none"])
.pipe_in("abcdef")
.succeeds()
.stdout_only("abcd");
new_ucmd!()
.args(&["skip=2Bx2", "status=none"])
.pipe_in("abcdef")
.succeeds()
.stdout_only("ef");
new_ucmd!()
.args(&["iseek=2Bx2", "status=none"])
.pipe_in("abcdef")
.succeeds()
.stdout_only("ef");
new_ucmd!()
.args(&["seek=2Bx2", "status=none"])
.pipe_in("abcdef")
.succeeds()
.stdout_only("\0\0\0\0abcdef");
new_ucmd!()
.args(&["oseek=2Bx2", "status=none"])
.pipe_in("abcdef")
.succeeds()
.stdout_only("\0\0\0\0abcdef");
}

/// Test for "conv=sync" with a slow reader.
#[cfg(not(windows))]
#[test]
Expand Down

0 comments on commit 1819cde

Please sign in to comment.