[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] dd: support iflag=count_bytes
From:       Rafał Miłecki <zajec5 () gmail ! com>
Date:       2021-03-24 15:01:42
Message-ID: 20210324150142.3204-1-zajec5 () gmail ! com
[Download RAW message or body]

From: Rafał Miłecki <rafal@milecki.pl>

It allows passing amount of bytes in the count=

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 coreutils/dd.c              | 54 +++++++++++++++++++++++++++----------
 testsuite/dd/dd-count-bytes |  1 +
 2 files changed, 41 insertions(+), 14 deletions(-)
 create mode 100644 testsuite/dd/dd-count-bytes

diff --git a/coreutils/dd.c b/coreutils/dd.c
index 24d7f0b84..22600f510 100644
--- a/coreutils/dd.c
+++ b/coreutils/dd.c
@@ -59,7 +59,7 @@
 //usage:       "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] \
[count=N] [skip=N] [seek=N]\n"  //usage:	IF_FEATURE_DD_IBS_OBS(
 //usage:       "	[conv=notrunc|noerror|sync|fsync]\n"
-//usage:       "	[iflag=skip_bytes|fullblock|direct] \
[oflag=seek_bytes|append|direct]" +//usage:       \
"	[iflag=skip_bytes|count_bytes|fullblock|direct] [oflag=seek_bytes|append|direct]"  \
//usage:	)  //usage:#define dd_full_usage "\n\n"
 //usage:       "Copy a file with converting and formatting\n"
@@ -82,6 +82,7 @@
 //usage:     "\n	conv=fsync	Physically write data out before finishing"
 //usage:     "\n	conv=swab	Swap every pair of bytes"
 //usage:     "\n	iflag=skip_bytes	skip=N is in bytes"
+//usage:     "\n	iflag=count_bytes	count=N is in bytes"
 //usage:     "\n	oflag=seek_bytes	seek=N is in bytes"
 //usage:     "\n	iflag=direct	O_DIRECT input"
 //usage:     "\n	oflag=direct	O_DIRECT output"
@@ -138,19 +139,20 @@ enum {
 	/* start of input flags */
 	FLAG_IFLAG_SHIFT = 5,
 	FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
-	FLAG_FULLBLOCK = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
-	FLAG_IDIRECT = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
+	FLAG_COUNT_BYTES = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
+	FLAG_FULLBLOCK = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
+	FLAG_IDIRECT = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
 	/* end of input flags */
 	/* start of output flags */
-	FLAG_OFLAG_SHIFT = 8,
-	FLAG_SEEK_BYTES = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
-	FLAG_APPEND = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
-	FLAG_ODIRECT = (1 << 10) * ENABLE_FEATURE_DD_IBS_OBS,
+	FLAG_OFLAG_SHIFT = 9,
+	FLAG_SEEK_BYTES = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
+	FLAG_APPEND = (1 << 10) * ENABLE_FEATURE_DD_IBS_OBS,
+	FLAG_ODIRECT = (1 << 11) * ENABLE_FEATURE_DD_IBS_OBS,
 	/* end of output flags */
-	FLAG_TWOBUFS = (1 << 11) * ENABLE_FEATURE_DD_IBS_OBS,
-	FLAG_COUNT   = 1 << 12,
-	FLAG_STATUS_NONE = 1 << 13,
-	FLAG_STATUS_NOXFER = 1 << 14,
+	FLAG_TWOBUFS = (1 << 12) * ENABLE_FEATURE_DD_IBS_OBS,
+	FLAG_COUNT   = 1 << 13,
+	FLAG_STATUS_NONE = 1 << 14,
+	FLAG_STATUS_NOXFER = 1 << 15,
 };
 
 static void dd_output_status(int UNUSED_PARAM cur_signal)
@@ -317,7 +319,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
 	static const char conv_words[] ALIGN1 =
 		"notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
 	static const char iflag_words[] ALIGN1 =
-		"skip_bytes\0""fullblock\0""direct\0";
+		"skip_bytes\0""count_bytes\0""fullblock\0""direct\0";
 	static const char oflag_words[] ALIGN1 =
 		"seek_bytes\0append\0""direct\0";
 #endif
@@ -359,6 +361,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
 	/* Partially implemented: */
 	//swab          swap every pair of input bytes: will abort on non-even reads
 		OP_iflag_skip_bytes,
+		OP_iflag_count_bytes,
 		OP_iflag_fullblock,
 		OP_iflag_direct,
 		OP_oflag_seek_bytes,
@@ -366,6 +369,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
 #endif
 	};
 	smallint exitcode = EXIT_FAILURE;
+	ssize_t max_in_full = -1;
 	int i;
 	size_t ibs = 512;
 	char *ibuf;
@@ -551,8 +555,20 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
 			goto die_outfile;
 	}
 
-	while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
-		ssize_t n = dd_read(ibuf, ibs);
+	if (G.flags & FLAG_COUNT)
+		max_in_full = (G.flags & FLAG_COUNT_BYTES) ? count / ibs : count;
+
+	while (1) {
+		size_t to_read;
+		ssize_t n;
+
+		to_read = ibs;
+		if (G.flags & FLAG_COUNT &&
+		    G.flags & FLAG_COUNT_BYTES &&
+		    G.in_full == max_in_full)
+			to_read = count % ibs;
+
+		n = dd_read(ibuf, to_read);
 		if (n == 0)
 			break;
 		if (n < 0) {
@@ -619,6 +635,16 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
 			if (write_and_stats(ibuf, n, obs, outfile))
 				goto out_status;
 		}
+
+		if (G.flags & FLAG_COUNT) {
+			if (G.flags & FLAG_COUNT_BYTES) {
+				if ((G.in_full + G.in_part) * ibs >= count)
+					break;
+			} else {
+				if (G.in_full + G.in_part == count)
+					break;
+			}
+		}
 	}
 
 	if (G.flags & FLAG_FSYNC) {
diff --git a/testsuite/dd/dd-count-bytes b/testsuite/dd/dd-count-bytes
new file mode 100644
index 000000000..0730cba5e
--- /dev/null
+++ b/testsuite/dd/dd-count-bytes
@@ -0,0 +1 @@
+test "$(echo I WANT | busybox dd count=3 iflag=count_bytes 2>/dev/null)" = "I W"
-- 
2.26.2

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic