2000-02-09 03:58:47 +08:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-06 00:24:54 +08:00
|
|
|
/*
|
1999-10-21 06:08:37 +08:00
|
|
|
* Mini dd implementation for busybox
|
|
|
|
*
|
2001-12-08 00:27:37 +08:00
|
|
|
* Copyright (C) 2000,2001 Matt Kraai
|
1999-10-21 06:08:37 +08:00
|
|
|
*
|
2010-08-17 02:14:46 +08:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
1999-10-06 00:24:54 +08:00
|
|
|
*/
|
2015-02-07 08:41:22 +08:00
|
|
|
//config:config DD
|
2018-12-28 10:20:17 +08:00
|
|
|
//config: bool "dd (7.5 kb)"
|
2015-02-07 08:41:22 +08:00
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 15:50:55 +08:00
|
|
|
//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.
|
2015-02-07 08:41:22 +08:00
|
|
|
//config:
|
|
|
|
//config:config FEATURE_DD_SIGNAL_HANDLING
|
|
|
|
//config: bool "Enable signal handling for status reporting"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on DD
|
|
|
|
//config: help
|
2017-08-02 20:12:48 +08:00
|
|
|
//config: Sending a SIGUSR1 signal to a running 'dd' process makes it
|
2017-07-21 15:50:55 +08:00
|
|
|
//config: print to standard error the number of records read and written
|
|
|
|
//config: so far, then to resume copying.
|
2015-02-07 08:41:22 +08:00
|
|
|
//config:
|
2017-07-21 15:50:55 +08:00
|
|
|
//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
|
2015-02-07 08:41:22 +08:00
|
|
|
//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
|
2017-07-21 15:50:55 +08:00
|
|
|
//config: Displays a coreutils-like third status line with transferred bytes,
|
|
|
|
//config: elapsed time and speed.
|
2015-02-07 08:41:22 +08:00
|
|
|
//config:
|
|
|
|
//config:config FEATURE_DD_IBS_OBS
|
2018-10-15 18:26:15 +08:00
|
|
|
//config: bool "Enable ibs, obs, iflag, oflag and conv options"
|
2015-02-07 08:41:22 +08:00
|
|
|
//config: default y
|
|
|
|
//config: depends on DD
|
|
|
|
//config: help
|
2017-07-21 15:50:55 +08:00
|
|
|
//config: Enable support for writing a certain number of bytes in and out,
|
|
|
|
//config: at a time, and performing conversions on the data stream.
|
2015-02-07 08:41:22 +08:00
|
|
|
//config:
|
|
|
|
//config:config FEATURE_DD_STATUS
|
|
|
|
//config: bool "Enable status display options"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on DD
|
|
|
|
//config: help
|
2017-07-21 15:50:55 +08:00
|
|
|
//config: Enable support for status=noxfer/none option.
|
2015-02-07 08:41:22 +08:00
|
|
|
|
2016-11-23 21:46:56 +08:00
|
|
|
//applet:IF_DD(APPLET_NOEXEC(dd, dd, BB_DIR_BIN, BB_SUID_DROP, dd))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_DD) += dd.o
|
|
|
|
|
2011-03-31 20:43:25 +08:00
|
|
|
//usage:#define dd_trivial_usage
|
2021-08-16 05:05:30 +08:00
|
|
|
//usage: "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]"
|
|
|
|
//usage: IF_FEATURE_DD_IBS_OBS("\n"
|
2018-10-31 00:04:52 +08:00
|
|
|
//usage: " [conv=notrunc|noerror|sync|fsync]\n"
|
2021-03-24 23:01:42 +08:00
|
|
|
//usage: " [iflag=skip_bytes|count_bytes|fullblock|direct] [oflag=seek_bytes|append|direct]"
|
2018-10-31 00:04:52 +08:00
|
|
|
//usage: )
|
2011-03-31 20:43:25 +08:00
|
|
|
//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"
|
2013-08-19 15:01:39 +08:00
|
|
|
//usage: "\n conv=swab Swap every pair of bytes"
|
2016-02-01 09:17:28 +08:00
|
|
|
//usage: "\n iflag=skip_bytes skip=N is in bytes"
|
2021-03-24 23:01:42 +08:00
|
|
|
//usage: "\n iflag=count_bytes count=N is in bytes"
|
2018-10-15 18:26:15 +08:00
|
|
|
//usage: "\n oflag=seek_bytes seek=N is in bytes"
|
2020-12-15 03:27:57 +08:00
|
|
|
//usage: "\n iflag=direct O_DIRECT input"
|
|
|
|
//usage: "\n oflag=direct O_DIRECT output"
|
|
|
|
//usage: "\n iflag=fullblock Read full blocks"
|
|
|
|
//usage: "\n oflag=append Open output in append mode"
|
2011-03-31 20:43:25 +08:00
|
|
|
//usage: )
|
2015-02-07 08:41:22 +08:00
|
|
|
//usage: IF_FEATURE_DD_STATUS(
|
|
|
|
//usage: "\n status=noxfer Suppress rate output"
|
|
|
|
//usage: "\n status=none Suppress all output"
|
|
|
|
//usage: )
|
2011-03-31 20:43:25 +08:00
|
|
|
//usage: "\n"
|
2015-02-07 08:41:22 +08:00
|
|
|
//usage: "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G"
|
2011-03-31 20:43:25 +08:00
|
|
|
//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"
|
|
|
|
|
2007-05-27 03:00:18 +08:00
|
|
|
#include "libbb.h"
|
2016-04-21 22:26:30 +08:00
|
|
|
#include "common_bufsiz.h"
|
2007-04-10 23:43:37 +08:00
|
|
|
|
|
|
|
/* This is a NOEXEC applet. Be very careful! */
|
|
|
|
|
2001-02-20 14:14:08 +08:00
|
|
|
|
2007-12-02 09:44:42 +08:00
|
|
|
enum {
|
|
|
|
ifd = STDIN_FILENO,
|
|
|
|
ofd = STDOUT_FILENO,
|
|
|
|
};
|
|
|
|
|
2007-04-04 22:01:23 +08:00
|
|
|
struct globals {
|
|
|
|
off_t out_full, out_part, in_full, in_part;
|
2009-10-14 06:29:28 +08:00
|
|
|
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
|
|
|
unsigned long long total_bytes;
|
|
|
|
unsigned long long begin_time_us;
|
|
|
|
#endif
|
2015-02-07 08:41:22 +08:00
|
|
|
int flags;
|
2010-02-04 22:00:15 +08:00
|
|
|
} FIX_ALIASING;
|
2016-04-21 22:26:30 +08:00
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2009-09-30 08:39:57 +08:00
|
|
|
#define INIT_G() do { \
|
2016-04-22 00:18:48 +08:00
|
|
|
setup_common_bufsiz(); \
|
2009-09-30 08:39:57 +08:00
|
|
|
/* we have to zero it out because of NOEXEC */ \
|
|
|
|
memset(&G, 0, sizeof(G)); \
|
|
|
|
} while (0)
|
2007-07-27 23:02:00 +08:00
|
|
|
|
2015-02-07 08:41:22 +08:00
|
|
|
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 */
|
2016-02-01 09:17:28 +08:00
|
|
|
/* start of input flags */
|
2021-03-24 23:01:42 +08:00
|
|
|
FLAG_IFLAG_SHIFT = 5,
|
|
|
|
FLAG_SKIP_BYTES = (1 << 5) * 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,
|
2016-02-01 09:17:28 +08:00
|
|
|
/* end of input flags */
|
2018-10-15 18:26:15 +08:00
|
|
|
/* start of output flags */
|
2021-03-24 23:01:42 +08:00
|
|
|
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,
|
2018-10-15 18:26:15 +08:00
|
|
|
/* end of output flags */
|
2021-03-24 23:01:42 +08:00
|
|
|
FLAG_TWOBUFS = (1 << 12) * ENABLE_FEATURE_DD_IBS_OBS,
|
|
|
|
FLAG_COUNT = 1 << 13,
|
|
|
|
FLAG_STATUS_NONE = 1 << 14,
|
|
|
|
FLAG_STATUS_NOXFER = 1 << 15,
|
2015-02-07 08:41:22 +08:00
|
|
|
};
|
2006-05-03 06:44:04 +08:00
|
|
|
|
2008-07-05 17:18:54 +08:00
|
|
|
static void dd_output_status(int UNUSED_PARAM cur_signal)
|
2006-05-03 06:44:04 +08:00
|
|
|
{
|
2009-10-14 06:29:28 +08:00
|
|
|
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
2009-10-14 23:43:41 +08:00
|
|
|
double seconds;
|
|
|
|
unsigned long long bytes_sec;
|
|
|
|
unsigned long long now_us = monotonic_us(); /* before fprintf */
|
2009-10-14 06:29:28 +08:00
|
|
|
#endif
|
|
|
|
|
2007-07-30 18:58:09 +08:00
|
|
|
/* 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",
|
2007-04-04 22:01:23 +08:00
|
|
|
G.in_full, G.in_part,
|
|
|
|
G.out_full, G.out_part);
|
2009-10-14 06:29:28 +08:00
|
|
|
|
|
|
|
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
2015-02-07 08:41:22 +08:00
|
|
|
# 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
|
2021-03-24 23:01:42 +08:00
|
|
|
fprintf(stderr, /*G.total_bytes < 1024
|
|
|
|
? "%llu bytes copied, " : */ "%llu bytes (%sB) copied, "
|
|
|
|
, G.total_bytes,
|
2009-10-14 06:29:28 +08:00
|
|
|
/* 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
|
|
|
|
*/
|
2009-10-14 23:43:41 +08:00
|
|
|
seconds = (now_us - G.begin_time_us) / 1000000.0;
|
|
|
|
bytes_sec = G.total_bytes / seconds;
|
2009-10-14 06:29:28 +08:00
|
|
|
fprintf(stderr, "%f seconds, %sB/s\n",
|
2009-10-14 23:43:41 +08:00
|
|
|
seconds,
|
2009-10-14 06:29:28 +08:00
|
|
|
/* show fractional digit, use suffixes */
|
2009-10-14 23:43:41 +08:00
|
|
|
make_human_readable_str(bytes_sec, 1, 0)
|
2009-10-14 06:29:28 +08:00
|
|
|
);
|
|
|
|
#endif
|
2006-05-03 06:44:04 +08:00
|
|
|
}
|
|
|
|
|
2020-12-15 03:27:57 +08:00
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2022-10-16 08:04:59 +08:00
|
|
|
# ifdef O_DIRECT
|
2020-12-15 03:27:57 +08:00
|
|
|
static int clear_O_DIRECT(int fd)
|
|
|
|
{
|
|
|
|
if (errno == EINVAL) {
|
|
|
|
int fl = fcntl(fd, F_GETFL);
|
|
|
|
if (fl & O_DIRECT) {
|
|
|
|
fcntl(fd, F_SETFL, fl & ~O_DIRECT);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2022-10-16 08:04:59 +08:00
|
|
|
# endif
|
2020-12-15 03:27:57 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static ssize_t dd_read(void *ibuf, size_t ibs)
|
|
|
|
{
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
read_again:
|
|
|
|
if (G.flags & FLAG_FULLBLOCK)
|
|
|
|
n = full_read(ifd, ibuf, ibs);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
n = safe_read(ifd, ibuf, ibs);
|
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2022-10-16 08:04:59 +08:00
|
|
|
# ifdef O_DIRECT
|
2020-12-15 03:27:57 +08:00
|
|
|
if (n < 0 && (G.flags & FLAG_IDIRECT) && clear_O_DIRECT(ifd))
|
|
|
|
goto read_again;
|
2022-10-16 08:04:59 +08:00
|
|
|
# endif
|
2020-12-15 03:27:57 +08:00
|
|
|
#endif
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-12-02 09:44:42 +08:00
|
|
|
static bool write_and_stats(const void *buf, size_t len, size_t obs,
|
2007-07-24 23:54:42 +08:00
|
|
|
const char *filename)
|
2007-04-04 22:01:23 +08:00
|
|
|
{
|
2019-05-14 23:46:18 +08:00
|
|
|
ssize_t n;
|
|
|
|
|
2020-12-15 03:27:57 +08:00
|
|
|
IF_FEATURE_DD_IBS_OBS(write_again:)
|
2019-05-14 23:46:18 +08:00
|
|
|
n = full_write(ofd, buf, len);
|
2020-12-15 03:27:57 +08:00
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2022-10-16 08:04:59 +08:00
|
|
|
# ifdef O_DIRECT
|
2020-12-15 03:27:57 +08:00
|
|
|
if (n < 0 && (G.flags & FLAG_ODIRECT) && clear_O_DIRECT(ofd))
|
|
|
|
goto write_again;
|
2022-10-16 08:04:59 +08:00
|
|
|
# endif
|
2020-12-15 03:27:57 +08:00
|
|
|
#endif
|
|
|
|
|
2009-10-14 06:29:28 +08:00
|
|
|
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
2019-05-14 23:46:18 +08:00
|
|
|
if (n > 0)
|
|
|
|
G.total_bytes += n;
|
2009-10-14 06:29:28 +08:00
|
|
|
#endif
|
2018-01-10 17:58:55 +08:00
|
|
|
if ((size_t)n == obs) {
|
|
|
|
G.out_full++;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-10 18:04:09 +08:00
|
|
|
if ((size_t)n == len) {
|
2018-01-10 17:58:55 +08:00
|
|
|
G.out_part++;
|
2018-01-10 18:04:09 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2019-05-14 23:46:18 +08:00
|
|
|
/* n is < len (and possibly is -1).
|
|
|
|
* Even if n >= 0, errno is usually set correctly.
|
|
|
|
* For example, if writing to block device and getting ENOSPC,
|
|
|
|
* full_write() first sees a short write, then tries to write
|
|
|
|
* the remainder and gets errno set to ENOSPC.
|
|
|
|
* It returns n > 0 (the amount which it did write).
|
|
|
|
*/
|
|
|
|
bb_perror_msg("error writing '%s'", filename);
|
2018-01-10 17:58:55 +08:00
|
|
|
return 1;
|
2007-04-04 22:01:23 +08:00
|
|
|
}
|
|
|
|
|
2006-11-25 22:46:21 +08:00
|
|
|
#if ENABLE_LFS
|
2009-10-14 06:29:28 +08:00
|
|
|
# define XATOU_SFX xatoull_sfx
|
2006-11-25 22:46:21 +08:00
|
|
|
#else
|
2009-10-14 06:29:28 +08:00
|
|
|
# define XATOU_SFX xatoul_sfx
|
2006-11-25 22:46:21 +08:00
|
|
|
#endif
|
|
|
|
|
2016-02-01 09:17:28 +08:00
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
static int parse_comma_flags(char *val, const char *words, const char *error_in)
|
|
|
|
{
|
|
|
|
int flags = 0;
|
|
|
|
while (1) {
|
|
|
|
int n;
|
|
|
|
char *arg;
|
|
|
|
/* find ',', replace them with NUL so we can use val for
|
|
|
|
* index_in_strings() without copying.
|
|
|
|
* We rely on val being non-null, else strchr would fault.
|
|
|
|
*/
|
|
|
|
arg = strchr(val, ',');
|
|
|
|
if (arg)
|
|
|
|
*arg = '\0';
|
|
|
|
n = index_in_strings(words, val);
|
|
|
|
if (n < 0)
|
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in);
|
|
|
|
flags |= (1 << n);
|
|
|
|
if (!arg) /* no ',' left, so this was the last specifier */
|
|
|
|
break;
|
|
|
|
*arg = ','; /* to preserve ps listing */
|
|
|
|
val = arg + 1; /* skip this keyword and ',' */
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-12-15 03:27:57 +08:00
|
|
|
static void *alloc_buf(size_t size)
|
|
|
|
{
|
|
|
|
/* Important for "{i,o}flag=direct" - buffers must be page aligned */
|
|
|
|
if (size >= bb_getpagesize())
|
|
|
|
return xmmap_anon(size);
|
|
|
|
return xmalloc(size);
|
|
|
|
}
|
|
|
|
|
2007-10-11 18:05:36 +08:00
|
|
|
int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 17:18:54 +08:00
|
|
|
int dd_main(int argc UNUSED_PARAM, char **argv)
|
1999-10-13 06:26:06 +08:00
|
|
|
{
|
2007-08-13 04:58:27 +08:00
|
|
|
static const char keywords[] ALIGN1 =
|
2015-02-07 08:41:22 +08:00
|
|
|
"bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0")
|
2007-04-04 22:01:23 +08:00
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2018-10-15 18:26:15 +08:00
|
|
|
"ibs\0""obs\0""conv\0""iflag\0""oflag\0"
|
2007-04-04 22:01:23 +08:00
|
|
|
#endif
|
2007-07-24 23:54:42 +08:00
|
|
|
;
|
2008-04-03 05:20:35 +08:00
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
static const char conv_words[] ALIGN1 =
|
2013-08-19 15:01:39 +08:00
|
|
|
"notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
|
2016-02-01 09:17:28 +08:00
|
|
|
static const char iflag_words[] ALIGN1 =
|
2021-03-24 23:01:42 +08:00
|
|
|
"skip_bytes\0""count_bytes\0""fullblock\0""direct\0";
|
2018-10-15 18:26:15 +08:00
|
|
|
static const char oflag_words[] ALIGN1 =
|
2020-12-15 03:27:57 +08:00
|
|
|
"seek_bytes\0append\0""direct\0";
|
2015-02-07 08:41:22 +08:00
|
|
|
#endif
|
|
|
|
#if ENABLE_FEATURE_DD_STATUS
|
|
|
|
static const char status_words[] ALIGN1 =
|
|
|
|
"none\0""noxfer\0";
|
2008-04-03 05:20:35 +08:00
|
|
|
#endif
|
2007-04-11 03:30:50 +08:00
|
|
|
enum {
|
2008-04-03 05:20:35 +08:00
|
|
|
OP_bs = 0,
|
2007-04-11 03:30:50 +08:00
|
|
|
OP_count,
|
|
|
|
OP_seek,
|
|
|
|
OP_skip,
|
|
|
|
OP_if,
|
|
|
|
OP_of,
|
2015-02-07 08:41:22 +08:00
|
|
|
IF_FEATURE_DD_STATUS(OP_status,)
|
2007-04-16 20:21:05 +08:00
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2007-04-11 03:30:50 +08:00
|
|
|
OP_ibs,
|
|
|
|
OP_obs,
|
|
|
|
OP_conv,
|
2016-02-01 09:17:28 +08:00
|
|
|
OP_iflag,
|
2018-10-15 18:26:15 +08:00
|
|
|
OP_oflag,
|
2008-04-03 05:20:35 +08:00
|
|
|
/* Must be in the same order as FLAG_XXX! */
|
|
|
|
OP_conv_notrunc = 0,
|
2007-04-11 03:30:50 +08:00
|
|
|
OP_conv_sync,
|
2007-04-20 04:16:14 +08:00
|
|
|
OP_conv_noerror,
|
2008-04-03 05:20:35 +08:00
|
|
|
OP_conv_fsync,
|
2013-08-19 15:01:39 +08:00
|
|
|
OP_conv_swab,
|
2008-04-03 05:20:35 +08:00
|
|
|
/* Unimplemented conv=XXX: */
|
|
|
|
//nocreat do not create the output file
|
|
|
|
//excl fail if the output file already exists
|
|
|
|
//fdatasync physically write output file data before finishing
|
|
|
|
//lcase change upper case to lower case
|
|
|
|
//ucase change lower case to upper case
|
|
|
|
//block pad newline-terminated records with spaces to cbs-size
|
|
|
|
//unblock replace trailing spaces in cbs-size records with newline
|
|
|
|
//ascii from EBCDIC to ASCII
|
|
|
|
//ebcdic from ASCII to EBCDIC
|
|
|
|
//ibm from ASCII to alternate EBCDIC
|
2013-08-19 15:01:39 +08:00
|
|
|
/* Partially implemented: */
|
|
|
|
//swab swap every pair of input bytes: will abort on non-even reads
|
2016-02-01 09:17:28 +08:00
|
|
|
OP_iflag_skip_bytes,
|
2021-03-24 23:01:42 +08:00
|
|
|
OP_iflag_count_bytes,
|
2018-01-26 02:00:19 +08:00
|
|
|
OP_iflag_fullblock,
|
2020-12-15 03:27:57 +08:00
|
|
|
OP_iflag_direct,
|
2018-10-15 18:26:15 +08:00
|
|
|
OP_oflag_seek_bytes,
|
2020-12-15 03:27:57 +08:00
|
|
|
OP_oflag_direct,
|
2007-04-16 20:21:05 +08:00
|
|
|
#endif
|
2007-04-11 03:30:50 +08:00
|
|
|
};
|
2013-08-20 08:50:49 +08:00
|
|
|
smallint exitcode = EXIT_FAILURE;
|
2013-08-19 15:00:08 +08:00
|
|
|
int i;
|
2013-08-20 18:08:46 +08:00
|
|
|
size_t ibs = 512;
|
|
|
|
char *ibuf;
|
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
size_t obs = 512;
|
|
|
|
char *obuf;
|
|
|
|
#else
|
|
|
|
# define obs ibs
|
|
|
|
# define obuf ibuf
|
|
|
|
#endif
|
|
|
|
/* These are all zeroed at once! */
|
2007-07-27 23:02:00 +08:00
|
|
|
struct {
|
2018-10-31 00:04:52 +08:00
|
|
|
IF_FEATURE_DD_IBS_OBS(size_t ocount;)
|
2013-08-20 08:50:49 +08:00
|
|
|
ssize_t prev_read_size; /* for detecting swab failure */
|
2007-07-27 23:02:00 +08:00
|
|
|
off_t count;
|
|
|
|
off_t seek, skip;
|
|
|
|
const char *infile, *outfile;
|
|
|
|
} Z;
|
2018-10-31 00:04:52 +08:00
|
|
|
#define ocount (Z.ocount )
|
2013-08-20 08:50:49 +08:00
|
|
|
#define prev_read_size (Z.prev_read_size)
|
2007-07-27 23:02:00 +08:00
|
|
|
#define count (Z.count )
|
|
|
|
#define seek (Z.seek )
|
|
|
|
#define skip (Z.skip )
|
|
|
|
#define infile (Z.infile )
|
|
|
|
#define outfile (Z.outfile)
|
2000-12-19 05:38:57 +08:00
|
|
|
|
2007-07-27 23:02:00 +08:00
|
|
|
memset(&Z, 0, sizeof(Z));
|
|
|
|
INIT_G();
|
2009-11-02 21:19:51 +08:00
|
|
|
//fflush_all(); - is this needed because of NOEXEC?
|
2006-05-03 06:44:04 +08:00
|
|
|
|
2013-08-19 15:00:08 +08:00
|
|
|
for (i = 1; argv[i]; i++) {
|
2008-04-03 05:20:35 +08:00
|
|
|
int what;
|
|
|
|
char *val;
|
2013-08-19 15:00:08 +08:00
|
|
|
char *arg = argv[i];
|
2007-04-04 22:01:23 +08:00
|
|
|
|
2008-04-03 05:20:35 +08:00
|
|
|
#if ENABLE_DESKTOP
|
|
|
|
/* "dd --". NB: coreutils 6.9 will complain if they see
|
|
|
|
* more than one of them. We wouldn't. */
|
|
|
|
if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
val = strchr(arg, '=');
|
|
|
|
if (val == NULL)
|
2007-04-04 22:01:23 +08:00
|
|
|
bb_show_usage();
|
2008-04-03 05:20:35 +08:00
|
|
|
*val = '\0';
|
|
|
|
what = index_in_strings(keywords, arg);
|
|
|
|
if (what < 0)
|
2007-04-04 22:01:23 +08:00
|
|
|
bb_show_usage();
|
2008-04-03 05:20:35 +08:00
|
|
|
/* *val = '='; - to preserve ps listing? */
|
|
|
|
val++;
|
2007-04-16 20:21:05 +08:00
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2008-02-24 21:36:01 +08:00
|
|
|
if (what == OP_ibs) {
|
|
|
|
/* Must fit into positive ssize_t */
|
2015-03-05 00:46:48 +08:00
|
|
|
ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
|
2008-04-03 05:20:35 +08:00
|
|
|
/*continue;*/
|
2008-02-24 21:36:01 +08:00
|
|
|
}
|
|
|
|
if (what == OP_obs) {
|
2015-03-05 00:46:48 +08:00
|
|
|
obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
|
2008-04-03 05:20:35 +08:00
|
|
|
/*continue;*/
|
2008-02-24 21:36:01 +08:00
|
|
|
}
|
|
|
|
if (what == OP_conv) {
|
2016-02-01 09:17:28 +08:00
|
|
|
G.flags |= parse_comma_flags(val, conv_words, "conv");
|
|
|
|
/*continue;*/
|
|
|
|
}
|
|
|
|
if (what == OP_iflag) {
|
|
|
|
G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
|
2013-08-20 18:08:46 +08:00
|
|
|
/*continue;*/
|
2008-02-24 21:36:01 +08:00
|
|
|
}
|
2018-10-15 18:26:15 +08:00
|
|
|
if (what == OP_oflag) {
|
|
|
|
G.flags |= parse_comma_flags(val, oflag_words, "oflag") << FLAG_OFLAG_SHIFT;
|
|
|
|
/*continue;*/
|
|
|
|
}
|
2007-04-16 20:21:05 +08:00
|
|
|
#endif
|
2007-04-04 22:01:23 +08:00
|
|
|
if (what == OP_bs) {
|
2015-03-05 00:46:48 +08:00
|
|
|
ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
|
2013-08-20 18:08:46 +08:00
|
|
|
obs = ibs;
|
2008-04-03 05:20:35 +08:00
|
|
|
/*continue;*/
|
2007-04-04 22:01:23 +08:00
|
|
|
}
|
|
|
|
/* These can be large: */
|
|
|
|
if (what == OP_count) {
|
2015-02-07 08:41:22 +08:00
|
|
|
G.flags |= FLAG_COUNT;
|
2015-03-05 00:46:48 +08:00
|
|
|
count = XATOU_SFX(val, cwbkMG_suffixes);
|
2008-04-03 05:20:35 +08:00
|
|
|
/*continue;*/
|
2007-04-04 22:01:23 +08:00
|
|
|
}
|
|
|
|
if (what == OP_seek) {
|
2015-03-05 00:46:48 +08:00
|
|
|
seek = XATOU_SFX(val, cwbkMG_suffixes);
|
2008-04-03 05:20:35 +08:00
|
|
|
/*continue;*/
|
2007-04-04 22:01:23 +08:00
|
|
|
}
|
2007-04-20 04:08:19 +08:00
|
|
|
if (what == OP_skip) {
|
2015-03-05 00:46:48 +08:00
|
|
|
skip = XATOU_SFX(val, cwbkMG_suffixes);
|
2008-04-03 05:20:35 +08:00
|
|
|
/*continue;*/
|
2007-04-04 22:01:23 +08:00
|
|
|
}
|
|
|
|
if (what == OP_if) {
|
2008-04-03 05:20:35 +08:00
|
|
|
infile = val;
|
|
|
|
/*continue;*/
|
2007-04-04 22:01:23 +08:00
|
|
|
}
|
2008-04-03 05:20:35 +08:00
|
|
|
if (what == OP_of) {
|
|
|
|
outfile = val;
|
|
|
|
/*continue;*/
|
|
|
|
}
|
2015-02-07 08:41:22 +08:00
|
|
|
#if ENABLE_FEATURE_DD_STATUS
|
|
|
|
if (what == OP_status) {
|
|
|
|
int n;
|
|
|
|
n = index_in_strings(status_words, val);
|
|
|
|
if (n < 0)
|
2015-10-14 19:21:01 +08:00
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status");
|
2017-07-14 19:44:30 +08:00
|
|
|
G.flags |= FLAG_STATUS_NONE << n;
|
2015-02-07 08:41:22 +08:00
|
|
|
/*continue;*/
|
|
|
|
}
|
|
|
|
#endif
|
2013-08-19 15:00:08 +08:00
|
|
|
} /* end of "for (argv[i])" */
|
2008-04-03 05:20:35 +08:00
|
|
|
|
2020-12-15 03:27:57 +08:00
|
|
|
ibuf = alloc_buf(ibs);
|
2013-08-20 18:08:46 +08:00
|
|
|
obuf = ibuf;
|
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2006-10-31 23:55:56 +08:00
|
|
|
if (ibs != obs) {
|
2015-02-07 08:41:22 +08:00
|
|
|
G.flags |= FLAG_TWOBUFS;
|
2020-12-15 03:27:57 +08:00
|
|
|
obuf = alloc_buf(obs);
|
2006-10-31 23:55:56 +08:00
|
|
|
}
|
2013-08-20 18:08:46 +08:00
|
|
|
#endif
|
2009-10-14 06:29:28 +08:00
|
|
|
|
|
|
|
#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
|
|
|
|
signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
|
|
|
|
#endif
|
|
|
|
#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
|
|
|
|
G.begin_time_us = monotonic_us();
|
|
|
|
#endif
|
|
|
|
|
2013-08-20 18:08:46 +08:00
|
|
|
if (infile) {
|
2020-12-15 03:27:57 +08:00
|
|
|
int iflag = O_RDONLY;
|
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2022-10-16 08:04:59 +08:00
|
|
|
if (G.flags & FLAG_IDIRECT) {
|
|
|
|
# ifdef O_DIRECT
|
2020-12-15 03:27:57 +08:00
|
|
|
iflag |= O_DIRECT;
|
2022-10-16 08:04:59 +08:00
|
|
|
# else
|
|
|
|
bb_error_msg_and_die("O_DIRECT not supported on this platform");
|
|
|
|
# endif
|
|
|
|
}
|
2020-12-15 03:27:57 +08:00
|
|
|
#endif
|
|
|
|
xmove_fd(xopen(infile, iflag), ifd);
|
2013-08-20 18:08:46 +08:00
|
|
|
} else {
|
2003-03-19 17:13:01 +08:00
|
|
|
infile = bb_msg_standard_input;
|
1999-10-13 08:53:55 +08:00
|
|
|
}
|
2013-08-20 18:08:46 +08:00
|
|
|
if (outfile) {
|
2007-04-04 22:01:23 +08:00
|
|
|
int oflag = O_WRONLY | O_CREAT;
|
2001-01-17 08:21:05 +08:00
|
|
|
|
2015-02-07 08:41:22 +08:00
|
|
|
if (!seek && !(G.flags & FLAG_NOTRUNC))
|
2001-01-17 08:21:05 +08:00
|
|
|
oflag |= O_TRUNC;
|
2019-02-06 19:57:15 +08:00
|
|
|
if (G.flags & FLAG_APPEND)
|
|
|
|
oflag |= O_APPEND;
|
2020-12-15 03:27:57 +08:00
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2022-10-16 08:04:59 +08:00
|
|
|
if (G.flags & FLAG_ODIRECT) {
|
|
|
|
# ifdef O_DIRECT
|
2020-12-15 03:27:57 +08:00
|
|
|
oflag |= O_DIRECT;
|
2022-10-16 08:04:59 +08:00
|
|
|
# else
|
|
|
|
bb_error_msg_and_die("O_DIRECT not supported on this platform");
|
|
|
|
# endif
|
|
|
|
}
|
2020-12-15 03:27:57 +08:00
|
|
|
#endif
|
2007-12-02 09:44:42 +08:00
|
|
|
xmove_fd(xopen(outfile, oflag), ofd);
|
2001-01-17 08:21:05 +08:00
|
|
|
|
2015-02-07 08:41:22 +08:00
|
|
|
if (seek && !(G.flags & FLAG_NOTRUNC)) {
|
2018-10-15 18:26:15 +08:00
|
|
|
size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
|
|
|
|
if (ftruncate(ofd, seek * blocksz) < 0) {
|
2002-02-05 23:28:54 +08:00
|
|
|
struct stat st;
|
|
|
|
|
2010-01-28 09:24:24 +08:00
|
|
|
if (fstat(ofd, &st) < 0
|
|
|
|
|| S_ISREG(st.st_mode)
|
|
|
|
|| S_ISDIR(st.st_mode)
|
|
|
|
) {
|
2006-08-29 07:31:54 +08:00
|
|
|
goto die_outfile;
|
2010-01-28 09:24:24 +08:00
|
|
|
}
|
2002-02-05 23:28:54 +08:00
|
|
|
}
|
2001-01-17 08:21:05 +08:00
|
|
|
}
|
2000-12-19 05:38:57 +08:00
|
|
|
} else {
|
2003-03-19 17:13:01 +08:00
|
|
|
outfile = bb_msg_standard_output;
|
2000-12-19 05:38:57 +08:00
|
|
|
}
|
|
|
|
if (skip) {
|
2016-02-01 09:17:28 +08:00
|
|
|
size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
|
|
|
|
if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
|
2013-08-20 18:45:05 +08:00
|
|
|
do {
|
2020-12-15 03:27:57 +08:00
|
|
|
ssize_t n = dd_read(ibuf, blocksz);
|
2006-05-17 00:52:12 +08:00
|
|
|
if (n < 0)
|
2007-04-04 22:01:23 +08:00
|
|
|
goto die_infile;
|
2006-05-17 00:52:12 +08:00
|
|
|
if (n == 0)
|
|
|
|
break;
|
2013-08-20 18:45:05 +08:00
|
|
|
} while (--skip != 0);
|
2002-11-28 19:05:28 +08:00
|
|
|
}
|
1999-10-13 06:26:06 +08:00
|
|
|
}
|
2000-12-19 05:38:57 +08:00
|
|
|
if (seek) {
|
2018-10-15 18:26:15 +08:00
|
|
|
size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
|
|
|
|
if (lseek(ofd, seek * blocksz, SEEK_CUR) < 0)
|
2006-08-29 07:31:54 +08:00
|
|
|
goto die_outfile;
|
2000-12-19 05:38:57 +08:00
|
|
|
}
|
2000-11-30 06:33:02 +08:00
|
|
|
|
2021-03-24 23:01:42 +08:00
|
|
|
while (1) {
|
|
|
|
ssize_t n = ibs;
|
|
|
|
|
|
|
|
if (G.flags & FLAG_COUNT) {
|
|
|
|
if (count == 0)
|
|
|
|
break;
|
|
|
|
if ((G.flags & FLAG_COUNT_BYTES) && count < ibs)
|
|
|
|
n = count;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = dd_read(ibuf, n);
|
2006-08-29 07:31:54 +08:00
|
|
|
if (n == 0)
|
2006-05-17 00:52:12 +08:00
|
|
|
break;
|
2002-04-27 09:31:43 +08:00
|
|
|
if (n < 0) {
|
2009-09-19 02:49:36 +08:00
|
|
|
/* "Bad block" */
|
2015-02-07 08:41:22 +08:00
|
|
|
if (!(G.flags & FLAG_NOERROR))
|
2007-04-04 22:01:23 +08:00
|
|
|
goto die_infile;
|
2007-12-02 09:44:42 +08:00
|
|
|
bb_simple_perror_msg(infile);
|
2009-09-19 02:49:36 +08:00
|
|
|
/* GNU dd with conv=noerror skips over bad blocks */
|
2009-08-06 05:16:27 +08:00
|
|
|
xlseek(ifd, ibs, SEEK_CUR);
|
2009-09-19 02:49:36 +08:00
|
|
|
/* conv=noerror,sync writes NULs,
|
|
|
|
* conv=noerror just ignores input bad blocks */
|
|
|
|
n = 0;
|
2002-04-27 09:31:43 +08:00
|
|
|
}
|
2015-02-07 08:41:22 +08:00
|
|
|
if (G.flags & FLAG_SWAB) {
|
2013-08-20 18:49:28 +08:00
|
|
|
uint16_t *p16;
|
|
|
|
ssize_t n2;
|
2013-08-20 08:50:49 +08:00
|
|
|
|
|
|
|
/* Our code allows only last read to be odd-sized */
|
|
|
|
if (prev_read_size & 1)
|
|
|
|
bb_error_msg_and_die("can't swab %lu byte buffer",
|
|
|
|
(unsigned long)prev_read_size);
|
|
|
|
prev_read_size = n;
|
|
|
|
|
2013-08-20 18:27:19 +08:00
|
|
|
/* If n is odd, last byte is not swapped:
|
2013-08-19 16:30:55 +08:00
|
|
|
* echo -n "qwe" | dd conv=swab
|
2013-08-19 15:01:39 +08:00
|
|
|
* prints "wqe".
|
|
|
|
*/
|
2013-08-20 08:50:49 +08:00
|
|
|
p16 = (void*) ibuf;
|
2013-08-20 18:49:28 +08:00
|
|
|
n2 = (n >> 1);
|
|
|
|
while (--n2 >= 0) {
|
2013-08-19 15:01:39 +08:00
|
|
|
*p16 = bswap_16(*p16);
|
|
|
|
p16++;
|
|
|
|
}
|
|
|
|
}
|
2021-03-24 23:01:42 +08:00
|
|
|
count -= (G.flags & FLAG_COUNT_BYTES) ? n : 1;
|
2006-08-29 07:31:54 +08:00
|
|
|
if ((size_t)n == ibs)
|
2007-04-04 22:01:23 +08:00
|
|
|
G.in_full++;
|
2006-08-29 07:31:54 +08:00
|
|
|
else {
|
2007-04-04 22:01:23 +08:00
|
|
|
G.in_part++;
|
2015-02-07 08:41:22 +08:00
|
|
|
if (G.flags & FLAG_SYNC) {
|
2009-09-19 02:49:36 +08:00
|
|
|
memset(ibuf + n, 0, ibs - n);
|
2006-05-17 00:52:12 +08:00
|
|
|
n = ibs;
|
|
|
|
}
|
2002-11-28 19:05:28 +08:00
|
|
|
}
|
2018-10-31 00:04:52 +08:00
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
2015-02-07 08:41:22 +08:00
|
|
|
if (G.flags & FLAG_TWOBUFS) {
|
2006-05-17 00:52:12 +08:00
|
|
|
char *tmp = ibuf;
|
|
|
|
while (n) {
|
2018-10-31 00:04:52 +08:00
|
|
|
size_t d = obs - ocount;
|
2008-05-13 10:27:31 +08:00
|
|
|
if (d > (size_t)n)
|
2006-08-29 07:31:54 +08:00
|
|
|
d = n;
|
2018-10-31 00:04:52 +08:00
|
|
|
memcpy(obuf + ocount, tmp, d);
|
2006-05-17 00:52:12 +08:00
|
|
|
n -= d;
|
|
|
|
tmp += d;
|
2018-10-31 00:04:52 +08:00
|
|
|
ocount += d;
|
|
|
|
if (ocount == obs) {
|
2007-12-02 09:44:42 +08:00
|
|
|
if (write_and_stats(obuf, obs, obs, outfile))
|
2007-04-04 22:01:23 +08:00
|
|
|
goto out_status;
|
2018-10-31 00:04:52 +08:00
|
|
|
ocount = 0;
|
2006-05-17 00:52:12 +08:00
|
|
|
}
|
|
|
|
}
|
2018-10-31 00:04:52 +08:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2013-08-20 18:40:29 +08:00
|
|
|
if (write_and_stats(ibuf, n, obs, outfile))
|
|
|
|
goto out_status;
|
|
|
|
}
|
2017-03-17 03:54:35 +08:00
|
|
|
}
|
2008-04-03 05:20:35 +08:00
|
|
|
|
2017-03-17 03:54:35 +08:00
|
|
|
if (G.flags & FLAG_FSYNC) {
|
|
|
|
if (fsync(ofd) < 0)
|
|
|
|
goto die_outfile;
|
2006-05-17 00:52:12 +08:00
|
|
|
}
|
2006-09-18 00:28:10 +08:00
|
|
|
|
2018-10-31 00:04:52 +08:00
|
|
|
#if ENABLE_FEATURE_DD_IBS_OBS
|
|
|
|
if (ocount != 0) {
|
|
|
|
if (write_and_stats(obuf, ocount, obs, outfile))
|
2013-08-20 18:40:29 +08:00
|
|
|
goto out_status;
|
2000-09-11 08:32:13 +08:00
|
|
|
}
|
2018-10-31 00:04:52 +08:00
|
|
|
#endif
|
2006-10-31 23:55:56 +08:00
|
|
|
if (close(ifd) < 0) {
|
2007-07-24 23:54:42 +08:00
|
|
|
die_infile:
|
2007-10-01 19:58:38 +08:00
|
|
|
bb_simple_perror_msg_and_die(infile);
|
2002-11-28 19:05:28 +08:00
|
|
|
}
|
2001-12-08 00:27:37 +08:00
|
|
|
|
2006-10-31 23:55:56 +08:00
|
|
|
if (close(ofd) < 0) {
|
2007-07-24 23:54:42 +08:00
|
|
|
die_outfile:
|
2007-10-01 19:58:38 +08:00
|
|
|
bb_simple_perror_msg_and_die(outfile);
|
2002-11-28 19:05:28 +08:00
|
|
|
}
|
2007-12-02 09:44:42 +08:00
|
|
|
|
|
|
|
exitcode = EXIT_SUCCESS;
|
2007-07-24 23:54:42 +08:00
|
|
|
out_status:
|
2015-02-07 08:41:22 +08:00
|
|
|
if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
|
|
|
|
dd_output_status(0);
|
1999-10-06 00:24:54 +08:00
|
|
|
|
2020-12-15 03:27:57 +08:00
|
|
|
#if 0 /* can't just free(), they can be mmap()ed */
|
2011-03-16 15:39:46 +08:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) {
|
|
|
|
free(obuf);
|
2015-02-07 08:41:22 +08:00
|
|
|
if (G.flags & FLAG_TWOBUFS)
|
2011-03-16 15:39:46 +08:00
|
|
|
free(ibuf);
|
|
|
|
}
|
2020-12-15 03:27:57 +08:00
|
|
|
#endif
|
2011-03-16 15:39:46 +08:00
|
|
|
|
2007-12-02 09:44:42 +08:00
|
|
|
return exitcode;
|
1999-10-06 00:24:54 +08:00
|
|
|
}
|