+//config:config DD
+//config: bool "dd"
+//config: default y
+//config: help
+//config: dd copies a file (from standard input to standard output,
+//config: by default) using specific input and output blocksizes,
+//config: while optionally performing conversions on it.
+//config:
+//config:config FEATURE_DD_SIGNAL_HANDLING
+//config: bool "Enable signal handling for status reporting"
+//config: default y
+//config: depends on DD
+//config: help
+//config: Sending a SIGUSR1 signal to a running `dd' process makes it
+//config: print to standard error the number of records read and written
+//config: so far, then to resume copying.
+//config:
+//config: $ dd if=/dev/zero of=/dev/null &
+//config: $ pid=$!; kill -USR1 $pid; sleep 1; kill $pid
+//config: 10899206+0 records in
+//config: 10899206+0 records out
+//config:
+//config:config FEATURE_DD_THIRD_STATUS_LINE
+//config: bool "Enable the third status line upon signal"
+//config: default y
+//config: depends on DD && FEATURE_DD_SIGNAL_HANDLING
+//config: help
+//config: Displays a coreutils-like third status line with transferred bytes,
+//config: elapsed time and speed.
+//config:
+//config:config FEATURE_DD_IBS_OBS
+//config: bool "Enable ibs, obs and conv options"
+//config: default y
+//config: depends on DD
+//config: help
+//config: Enables support for writing a certain number of bytes in and out,
+//config: at a time, and performing conversions on the data stream.
+//config:
+//config:config FEATURE_DD_STATUS
+//config: bool "Enable status display options"
+//config: default y
+//config: depends on DD
+//config: help
+//config: Enables support for status=noxfer/none option.
+
+//usage:#define dd_trivial_usage
+//usage: "[if=FILE] [of=FILE] " IF_FEATURE_DD_IBS_OBS("[ibs=N] [obs=N] ") "[bs=N] [count=N] [skip=N]\n"
+//usage: " [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync] [iflag=skip_bytes]")
+//usage:#define dd_full_usage "\n\n"
+//usage: "Copy a file with converting and formatting\n"
+//usage: "\n if=FILE Read from FILE instead of stdin"
+//usage: "\n of=FILE Write to FILE instead of stdout"
+//usage: "\n bs=N Read and write N bytes at a time"
+//usage: IF_FEATURE_DD_IBS_OBS(
+//usage: "\n ibs=N Read N bytes at a time"
+//usage: )
+//usage: IF_FEATURE_DD_IBS_OBS(
+//usage: "\n obs=N Write N bytes at a time"
+//usage: )
+//usage: "\n count=N Copy only N input blocks"
+//usage: "\n skip=N Skip N input blocks"
+//usage: "\n seek=N Skip N output blocks"
+//usage: IF_FEATURE_DD_IBS_OBS(
+//usage: "\n conv=notrunc Don't truncate output file"
+//usage: "\n conv=noerror Continue after read errors"
+//usage: "\n conv=sync Pad blocks with zeros"
+//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: )
+//usage: IF_FEATURE_DD_STATUS(
+//usage: "\n status=noxfer Suppress rate output"
+//usage: "\n status=none Suppress all output"
+//usage: )
+//usage: "\n"
+//usage: "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G"
+//usage:
+//usage:#define dd_example_usage
+//usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
+//usage: "4+0 records in\n"
+//usage: "4+0 records out\n"
+
+#include "libbb.h"
+
+/* This is a NOEXEC applet. Be very careful! */
+
+
+enum {
+ ifd = STDIN_FILENO,
+ ofd = STDOUT_FILENO,
+};
+
+struct globals {
+ off_t out_full, out_part, in_full, in_part;
+#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
+ unsigned long long total_bytes;
+ unsigned long long begin_time_us;
+#endif
+ int flags;
+} FIX_ALIASING;
+#define G (*(struct globals*)&bb_common_bufsiz1)
+#define INIT_G() do { \
+ /* we have to zero it out because of NOEXEC */ \
+ memset(&G, 0, sizeof(G)); \
+} while (0)
+
+enum {
+ /* Must be in the same order as OP_conv_XXX! */
+ /* (see "flags |= (1 << what)" below) */
+ FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
+ FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
+ FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
+ FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
+ FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
+ /* end of conv flags */
+ /* start of input flags */
+ FLAG_IFLAG_SHIFT = 5,
+ FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
+ /* end of input flags */
+ FLAG_TWOBUFS = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
+ FLAG_COUNT = 1 << 7,
+ FLAG_STATUS = 1 << 8,
+ FLAG_STATUS_NONE = 1 << 9,
+ FLAG_STATUS_NOXFER = 1 << 10,
+};
+
+static void dd_output_status(int UNUSED_PARAM cur_signal)
+{
+#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
+ double seconds;
+ unsigned long long bytes_sec;
+ unsigned long long now_us = monotonic_us(); /* before fprintf */
+#endif
+
+ /* Deliberately using %u, not %d */
+ fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
+ "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
+ G.in_full, G.in_part,
+ G.out_full, G.out_part);
+
+#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
+# if ENABLE_FEATURE_DD_STATUS
+ if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
+ return;
+ //TODO: should status=none make dd stop reacting to USR1 entirely?
+ //So far we react to it (we print the stats),
+ //status=none only suppresses final, non-USR1 generated status message.
+# endif
+ fprintf(stderr, "%llu bytes (%sB) copied, ",
+ G.total_bytes,
+ /* show fractional digit, use suffixes */
+ make_human_readable_str(G.total_bytes, 1, 0)
+ );
+ /* Corner cases:
+ * ./busybox dd </dev/null >/dev/null
+ * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
+ * (echo DONE) | ./busybox dd >/dev/null
+ * (sleep 1; echo DONE) | ./busybox dd >/dev/null
+ */
+ seconds = (now_us - G.begin_time_us) / 1000000.0;
+ bytes_sec = G.total_bytes / seconds;
+ fprintf(stderr, "%f seconds, %sB/s\n",
+ seconds,
+ /* show fractional digit, use suffixes */
+ make_human_readable_str(bytes_sec, 1, 0)
+ );
+#endif
+}
+
+static ssize_t full_write_or_warn(const void *buf, size_t len,
+ const char *const filename)
+{
+ ssize_t n = full_write(ofd, buf, len);
+ if (n < 0)
+ bb_perror_msg("writing '%s'", filename);
+ return n;
+}
+
+static bool write_and_stats(const void *buf, size_t len, size_t obs,
+ const char *filename)
+{
+ ssize_t n = full_write_or_warn(buf, len, filename);
+ if (n < 0)
+ return 1;
+ if ((size_t)n == obs)
+ G.out_full++;
+ else if (n) /* > 0 */
+ G.out_part++;
+#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
+ G.total_bytes += n;
+#endif
+ return 0;
+}