aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorRafał Miłecki <rafal@milecki.pl>2021-03-24 16:01:42 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2021-04-13 21:35:57 +0200
commit4eb46e1be6d88eaf077252ce93127ebf00aa8ef2 (patch)
treec393cd49d6c017fbde105101f29ed760c053696a /coreutils
parent7b5cbfd6d624e19ac296089b3d675b20bda429a4 (diff)
downloadbusybox-w32-4eb46e1be6d88eaf077252ce93127ebf00aa8ef2.tar.gz
busybox-w32-4eb46e1be6d88eaf077252ce93127ebf00aa8ef2.tar.bz2
busybox-w32-4eb46e1be6d88eaf077252ce93127ebf00aa8ef2.zip
dd: support iflag=count_bytes
It allows passing amount of bytes in the count= function old new delta packed_usage 33599 33617 +18 static.iflag_words 29 41 +12 dd_main 1601 1607 +6 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/0 up/down: 36/0) Total: 36 bytes Signed-off-by: Rafał Miłecki <rafal@milecki.pl> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/dd.c50
1 files changed, 32 insertions, 18 deletions
diff --git a/coreutils/dd.c b/coreutils/dd.c
index 24d7f0b84..a243f718b 100644
--- a/coreutils/dd.c
+++ b/coreutils/dd.c
@@ -59,7 +59,7 @@
59//usage: "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]\n" 59//usage: "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]\n"
60//usage: IF_FEATURE_DD_IBS_OBS( 60//usage: IF_FEATURE_DD_IBS_OBS(
61//usage: " [conv=notrunc|noerror|sync|fsync]\n" 61//usage: " [conv=notrunc|noerror|sync|fsync]\n"
62//usage: " [iflag=skip_bytes|fullblock|direct] [oflag=seek_bytes|append|direct]" 62//usage: " [iflag=skip_bytes|count_bytes|fullblock|direct] [oflag=seek_bytes|append|direct]"
63//usage: ) 63//usage: )
64//usage:#define dd_full_usage "\n\n" 64//usage:#define dd_full_usage "\n\n"
65//usage: "Copy a file with converting and formatting\n" 65//usage: "Copy a file with converting and formatting\n"
@@ -82,6 +82,7 @@
82//usage: "\n conv=fsync Physically write data out before finishing" 82//usage: "\n conv=fsync Physically write data out before finishing"
83//usage: "\n conv=swab Swap every pair of bytes" 83//usage: "\n conv=swab Swap every pair of bytes"
84//usage: "\n iflag=skip_bytes skip=N is in bytes" 84//usage: "\n iflag=skip_bytes skip=N is in bytes"
85//usage: "\n iflag=count_bytes count=N is in bytes"
85//usage: "\n oflag=seek_bytes seek=N is in bytes" 86//usage: "\n oflag=seek_bytes seek=N is in bytes"
86//usage: "\n iflag=direct O_DIRECT input" 87//usage: "\n iflag=direct O_DIRECT input"
87//usage: "\n oflag=direct O_DIRECT output" 88//usage: "\n oflag=direct O_DIRECT output"
@@ -136,21 +137,22 @@ enum {
136 FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS, 137 FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
137 /* end of conv flags */ 138 /* end of conv flags */
138 /* start of input flags */ 139 /* start of input flags */
139 FLAG_IFLAG_SHIFT = 5, 140 FLAG_IFLAG_SHIFT = 5,
140 FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS, 141 FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
141 FLAG_FULLBLOCK = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS, 142 FLAG_COUNT_BYTES = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
142 FLAG_IDIRECT = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS, 143 FLAG_FULLBLOCK = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
144 FLAG_IDIRECT = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
143 /* end of input flags */ 145 /* end of input flags */
144 /* start of output flags */ 146 /* start of output flags */
145 FLAG_OFLAG_SHIFT = 8, 147 FLAG_OFLAG_SHIFT = 9,
146 FLAG_SEEK_BYTES = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS, 148 FLAG_SEEK_BYTES = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
147 FLAG_APPEND = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS, 149 FLAG_APPEND = (1 << 10) * ENABLE_FEATURE_DD_IBS_OBS,
148 FLAG_ODIRECT = (1 << 10) * ENABLE_FEATURE_DD_IBS_OBS, 150 FLAG_ODIRECT = (1 << 11) * ENABLE_FEATURE_DD_IBS_OBS,
149 /* end of output flags */ 151 /* end of output flags */
150 FLAG_TWOBUFS = (1 << 11) * ENABLE_FEATURE_DD_IBS_OBS, 152 FLAG_TWOBUFS = (1 << 12) * ENABLE_FEATURE_DD_IBS_OBS,
151 FLAG_COUNT = 1 << 12, 153 FLAG_COUNT = 1 << 13,
152 FLAG_STATUS_NONE = 1 << 13, 154 FLAG_STATUS_NONE = 1 << 14,
153 FLAG_STATUS_NOXFER = 1 << 14, 155 FLAG_STATUS_NOXFER = 1 << 15,
154}; 156};
155 157
156static void dd_output_status(int UNUSED_PARAM cur_signal) 158static void dd_output_status(int UNUSED_PARAM cur_signal)
@@ -175,8 +177,9 @@ static void dd_output_status(int UNUSED_PARAM cur_signal)
175 //So far we react to it (we print the stats), 177 //So far we react to it (we print the stats),
176 //status=none only suppresses final, non-USR1 generated status message. 178 //status=none only suppresses final, non-USR1 generated status message.
177# endif 179# endif
178 fprintf(stderr, "%llu bytes (%sB) copied, ", 180 fprintf(stderr, /*G.total_bytes < 1024
179 G.total_bytes, 181 ? "%llu bytes copied, " : */ "%llu bytes (%sB) copied, "
182 , G.total_bytes,
180 /* show fractional digit, use suffixes */ 183 /* show fractional digit, use suffixes */
181 make_human_readable_str(G.total_bytes, 1, 0) 184 make_human_readable_str(G.total_bytes, 1, 0)
182 ); 185 );
@@ -317,7 +320,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
317 static const char conv_words[] ALIGN1 = 320 static const char conv_words[] ALIGN1 =
318 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0"; 321 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
319 static const char iflag_words[] ALIGN1 = 322 static const char iflag_words[] ALIGN1 =
320 "skip_bytes\0""fullblock\0""direct\0"; 323 "skip_bytes\0""count_bytes\0""fullblock\0""direct\0";
321 static const char oflag_words[] ALIGN1 = 324 static const char oflag_words[] ALIGN1 =
322 "seek_bytes\0append\0""direct\0"; 325 "seek_bytes\0append\0""direct\0";
323#endif 326#endif
@@ -359,6 +362,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
359 /* Partially implemented: */ 362 /* Partially implemented: */
360 //swab swap every pair of input bytes: will abort on non-even reads 363 //swab swap every pair of input bytes: will abort on non-even reads
361 OP_iflag_skip_bytes, 364 OP_iflag_skip_bytes,
365 OP_iflag_count_bytes,
362 OP_iflag_fullblock, 366 OP_iflag_fullblock,
363 OP_iflag_direct, 367 OP_iflag_direct,
364 OP_oflag_seek_bytes, 368 OP_oflag_seek_bytes,
@@ -551,8 +555,17 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
551 goto die_outfile; 555 goto die_outfile;
552 } 556 }
553 557
554 while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) { 558 while (1) {
555 ssize_t n = dd_read(ibuf, ibs); 559 ssize_t n = ibs;
560
561 if (G.flags & FLAG_COUNT) {
562 if (count == 0)
563 break;
564 if ((G.flags & FLAG_COUNT_BYTES) && count < ibs)
565 n = count;
566 }
567
568 n = dd_read(ibuf, n);
556 if (n == 0) 569 if (n == 0)
557 break; 570 break;
558 if (n < 0) { 571 if (n < 0) {
@@ -587,6 +600,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
587 p16++; 600 p16++;
588 } 601 }
589 } 602 }
603 count -= (G.flags & FLAG_COUNT_BYTES) ? n : 1;
590 if ((size_t)n == ibs) 604 if ((size_t)n == ibs)
591 G.in_full++; 605 G.in_full++;
592 else { 606 else {