Skip to content

Commit f92935d

Browse files
committed
dd: fix skip=n processing. add test.
1 parent 745ec85 commit f92935d

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

file/src/dd.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,21 @@ fn copy_convert_file(config: &Config) -> Result<(), Box<dyn std::error::Error>>
205205
let mut ibuf = vec![0u8; config.ibs];
206206
let mut obuf = vec![0u8; config.obs];
207207

208+
// Skip the specified number of bytes
209+
let mut skipped = 0;
210+
while skipped < config.skip {
211+
let bytes_to_skip = std::cmp::min(config.skip - skipped, config.ibs);
212+
let n = ifile.read(&mut ibuf[..bytes_to_skip])?;
213+
if n == 0 {
214+
break;
215+
}
216+
skipped += n;
217+
}
218+
208219
let mut count = 0;
209-
let mut skip = config.skip;
210220
let mut seek = config.seek;
211221

212222
loop {
213-
if skip > 0 {
214-
let n = ifile.read(&mut ibuf)?;
215-
if n == 0 {
216-
break;
217-
}
218-
skip -= n;
219-
continue;
220-
}
221-
222223
if seek > 0 {
223224
let n = ifile.read(&mut ibuf)?;
224225
if n == 0 {

file/tests/dd-tests.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,28 @@ fn test_lcase_conversion() {
178178
expected_exit_code: 0,
179179
});
180180
}
181+
182+
#[test]
183+
fn test_skip_n() {
184+
let input_file_path = get_test_file_path("dd.ascii");
185+
186+
let mut input_file = File::open(input_file_path).expect("Unable to open input test file");
187+
let mut input_data = Vec::new();
188+
input_file
189+
.read_to_end(&mut input_data)
190+
.expect("Unable to read input test file");
191+
192+
let expected_output = b"world! {[ $&chars ]}\nAlas, poor Yorick, I knew him well.\n";
193+
194+
run_test_u8(TestPlanU8 {
195+
cmd: String::from("dd"),
196+
args: vec![
197+
String::from("ibs=1"),
198+
String::from("skip=7"), // Adjusting skip to 7 bytes to reach the correct output
199+
],
200+
stdin_data: input_data,
201+
expected_out: expected_output.to_vec(),
202+
expected_err: Vec::new(),
203+
expected_exit_code: 0,
204+
});
205+
}

0 commit comments

Comments
 (0)