blob: 9d173cc13b03caf706066fcb85444cfb1890f81d [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Eric Andersenc4996011999-10-20 22:08:37 +00003 * Mini dd implementation for busybox
4 *
Matt Kraai3eeab3b2001-12-07 16:27:37 +00005 * Copyright (C) 2000,2001 Matt Kraai
Eric Andersenc4996011999-10-20 22:08:37 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersencc8ed391999-10-05 16:24:54 +00008 */
Ari Sundholmf22a8382015-02-07 01:41:22 +01009//config:config DD
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "dd (7.1 kb)"
Ari Sundholmf22a8382015-02-07 01:41:22 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: dd copies a file (from standard input to standard output,
14//config: by default) using specific input and output blocksizes,
15//config: while optionally performing conversions on it.
Ari Sundholmf22a8382015-02-07 01:41:22 +010016//config:
17//config:config FEATURE_DD_SIGNAL_HANDLING
18//config: bool "Enable signal handling for status reporting"
19//config: default y
20//config: depends on DD
21//config: help
Denys Vlasenko18e781d2017-08-02 14:12:48 +020022//config: Sending a SIGUSR1 signal to a running 'dd' process makes it
Denys Vlasenko72089cf2017-07-21 09:50:55 +020023//config: print to standard error the number of records read and written
24//config: so far, then to resume copying.
Ari Sundholmf22a8382015-02-07 01:41:22 +010025//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020026//config: $ dd if=/dev/zero of=/dev/null &
27//config: $ pid=$!; kill -USR1 $pid; sleep 1; kill $pid
28//config: 10899206+0 records in
29//config: 10899206+0 records out
Ari Sundholmf22a8382015-02-07 01:41:22 +010030//config:
31//config:config FEATURE_DD_THIRD_STATUS_LINE
32//config: bool "Enable the third status line upon signal"
33//config: default y
34//config: depends on DD && FEATURE_DD_SIGNAL_HANDLING
35//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020036//config: Displays a coreutils-like third status line with transferred bytes,
37//config: elapsed time and speed.
Ari Sundholmf22a8382015-02-07 01:41:22 +010038//config:
39//config:config FEATURE_DD_IBS_OBS
40//config: bool "Enable ibs, obs and conv options"
41//config: default y
42//config: depends on DD
43//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020044//config: Enable support for writing a certain number of bytes in and out,
45//config: at a time, and performing conversions on the data stream.
Ari Sundholmf22a8382015-02-07 01:41:22 +010046//config:
47//config:config FEATURE_DD_STATUS
48//config: bool "Enable status display options"
49//config: default y
50//config: depends on DD
51//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020052//config: Enable support for status=noxfer/none option.
Ari Sundholmf22a8382015-02-07 01:41:22 +010053
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010054//applet:IF_DD(APPLET_NOEXEC(dd, dd, BB_DIR_BIN, BB_SUID_DROP, dd))
55
56//kbuild:lib-$(CONFIG_DD) += dd.o
57
Pere Orga34425382011-03-31 14:43:25 +020058//usage:#define dd_trivial_usage
59//usage: "[if=FILE] [of=FILE] " IF_FEATURE_DD_IBS_OBS("[ibs=N] [obs=N] ") "[bs=N] [count=N] [skip=N]\n"
Rafał Miłeckic30a5b12016-02-01 02:17:28 +010060//usage: " [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync] [iflag=skip_bytes]")
Pere Orga34425382011-03-31 14:43:25 +020061//usage:#define dd_full_usage "\n\n"
62//usage: "Copy a file with converting and formatting\n"
Pere Orga34425382011-03-31 14:43:25 +020063//usage: "\n if=FILE Read from FILE instead of stdin"
64//usage: "\n of=FILE Write to FILE instead of stdout"
65//usage: "\n bs=N Read and write N bytes at a time"
66//usage: IF_FEATURE_DD_IBS_OBS(
67//usage: "\n ibs=N Read N bytes at a time"
68//usage: )
69//usage: IF_FEATURE_DD_IBS_OBS(
70//usage: "\n obs=N Write N bytes at a time"
71//usage: )
72//usage: "\n count=N Copy only N input blocks"
73//usage: "\n skip=N Skip N input blocks"
74//usage: "\n seek=N Skip N output blocks"
75//usage: IF_FEATURE_DD_IBS_OBS(
76//usage: "\n conv=notrunc Don't truncate output file"
77//usage: "\n conv=noerror Continue after read errors"
78//usage: "\n conv=sync Pad blocks with zeros"
79//usage: "\n conv=fsync Physically write data out before finishing"
Denys Vlasenkob9413162013-08-19 09:01:39 +020080//usage: "\n conv=swab Swap every pair of bytes"
Rafał Miłeckic30a5b12016-02-01 02:17:28 +010081//usage: "\n iflag=skip_bytes skip=N is in bytes"
Pere Orga34425382011-03-31 14:43:25 +020082//usage: )
Ari Sundholmf22a8382015-02-07 01:41:22 +010083//usage: IF_FEATURE_DD_STATUS(
84//usage: "\n status=noxfer Suppress rate output"
85//usage: "\n status=none Suppress all output"
86//usage: )
Pere Orga34425382011-03-31 14:43:25 +020087//usage: "\n"
Ari Sundholmf22a8382015-02-07 01:41:22 +010088//usage: "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G"
Pere Orga34425382011-03-31 14:43:25 +020089//usage:
90//usage:#define dd_example_usage
91//usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
92//usage: "4+0 records in\n"
93//usage: "4+0 records out\n"
94
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000095#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020096#include "common_bufsiz.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +000097
98/* This is a NOEXEC applet. Be very careful! */
99
Eric Andersencbe31da2001-02-20 06:14:08 +0000100
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000101enum {
102 ifd = STDIN_FILENO,
103 ofd = STDOUT_FILENO,
104};
105
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000106struct globals {
107 off_t out_full, out_part, in_full, in_part;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200108#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
109 unsigned long long total_bytes;
110 unsigned long long begin_time_us;
111#endif
Ari Sundholmf22a8382015-02-07 01:41:22 +0100112 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100113} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200114#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200115#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200116 setup_common_bufsiz(); \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200117 /* we have to zero it out because of NOEXEC */ \
118 memset(&G, 0, sizeof(G)); \
119} while (0)
Denis Vlasenko75878702007-07-27 15:02:00 +0000120
Ari Sundholmf22a8382015-02-07 01:41:22 +0100121enum {
122 /* Must be in the same order as OP_conv_XXX! */
123 /* (see "flags |= (1 << what)" below) */
124 FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
125 FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
126 FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
127 FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
128 FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
129 /* end of conv flags */
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100130 /* start of input flags */
131 FLAG_IFLAG_SHIFT = 5,
132 FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
133 /* end of input flags */
134 FLAG_TWOBUFS = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
135 FLAG_COUNT = 1 << 7,
Denys Vlasenkod5b98e22017-07-14 13:44:30 +0200136 FLAG_STATUS_NONE = 1 << 8,
137 FLAG_STATUS_NOXFER = 1 << 9,
Ari Sundholmf22a8382015-02-07 01:41:22 +0100138};
Rob Landleyc5598172006-05-02 22:44:04 +0000139
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000140static void dd_output_status(int UNUSED_PARAM cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +0000141{
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200142#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200143 double seconds;
144 unsigned long long bytes_sec;
145 unsigned long long now_us = monotonic_us(); /* before fprintf */
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200146#endif
147
Denis Vlasenko980864d2007-07-30 10:58:09 +0000148 /* Deliberately using %u, not %d */
149 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
150 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000151 G.in_full, G.in_part,
152 G.out_full, G.out_part);
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200153
154#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Ari Sundholmf22a8382015-02-07 01:41:22 +0100155# if ENABLE_FEATURE_DD_STATUS
156 if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
157 return;
158 //TODO: should status=none make dd stop reacting to USR1 entirely?
159 //So far we react to it (we print the stats),
160 //status=none only suppresses final, non-USR1 generated status message.
161# endif
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200162 fprintf(stderr, "%llu bytes (%sB) copied, ",
163 G.total_bytes,
164 /* show fractional digit, use suffixes */
165 make_human_readable_str(G.total_bytes, 1, 0)
166 );
167 /* Corner cases:
168 * ./busybox dd </dev/null >/dev/null
169 * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
170 * (echo DONE) | ./busybox dd >/dev/null
171 * (sleep 1; echo DONE) | ./busybox dd >/dev/null
172 */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200173 seconds = (now_us - G.begin_time_us) / 1000000.0;
174 bytes_sec = G.total_bytes / seconds;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200175 fprintf(stderr, "%f seconds, %sB/s\n",
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200176 seconds,
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200177 /* show fractional digit, use suffixes */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200178 make_human_readable_str(bytes_sec, 1, 0)
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200179 );
180#endif
Rob Landleyc5598172006-05-02 22:44:04 +0000181}
182
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000183static ssize_t full_write_or_warn(const void *buf, size_t len,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000184 const char *const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000185{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000186 ssize_t n = full_write(ofd, buf, len);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000187 if (n < 0)
188 bb_perror_msg("writing '%s'", filename);
189 return n;
190}
191
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000192static bool write_and_stats(const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000193 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000194{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000195 ssize_t n = full_write_or_warn(buf, len, filename);
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000196 if (n < 0)
197 return 1;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200198#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
199 G.total_bytes += n;
200#endif
Denys Vlasenko5afd63a2018-01-10 10:58:55 +0100201 if ((size_t)n == obs) {
202 G.out_full++;
203 return 0;
204 }
205 if (n) /* > 0 */
206 G.out_part++;
207 return 1;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000208}
209
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000210#if ENABLE_LFS
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200211# define XATOU_SFX xatoull_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000212#else
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200213# define XATOU_SFX xatoul_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000214#endif
215
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100216#if ENABLE_FEATURE_DD_IBS_OBS
217static int parse_comma_flags(char *val, const char *words, const char *error_in)
218{
219 int flags = 0;
220 while (1) {
221 int n;
222 char *arg;
223 /* find ',', replace them with NUL so we can use val for
224 * index_in_strings() without copying.
225 * We rely on val being non-null, else strchr would fault.
226 */
227 arg = strchr(val, ',');
228 if (arg)
229 *arg = '\0';
230 n = index_in_strings(words, val);
231 if (n < 0)
232 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in);
233 flags |= (1 << n);
234 if (!arg) /* no ',' left, so this was the last specifier */
235 break;
236 *arg = ','; /* to preserve ps listing */
237 val = arg + 1; /* skip this keyword and ',' */
238 }
239 return flags;
240}
241#endif
242
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000243int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000244int dd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000245{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000246 static const char keywords[] ALIGN1 =
Ari Sundholmf22a8382015-02-07 01:41:22 +0100247 "bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0")
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000248#if ENABLE_FEATURE_DD_IBS_OBS
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100249 "ibs\0""obs\0""conv\0""iflag\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000250#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000251 ;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000252#if ENABLE_FEATURE_DD_IBS_OBS
253 static const char conv_words[] ALIGN1 =
Denys Vlasenkob9413162013-08-19 09:01:39 +0200254 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100255 static const char iflag_words[] ALIGN1 =
256 "skip_bytes\0";
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000257#endif
Ari Sundholmf22a8382015-02-07 01:41:22 +0100258#if ENABLE_FEATURE_DD_STATUS
259 static const char status_words[] ALIGN1 =
260 "none\0""noxfer\0";
261#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000262 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000263 OP_bs = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000264 OP_count,
265 OP_seek,
266 OP_skip,
267 OP_if,
268 OP_of,
Ari Sundholmf22a8382015-02-07 01:41:22 +0100269 IF_FEATURE_DD_STATUS(OP_status,)
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000270#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000271 OP_ibs,
272 OP_obs,
273 OP_conv,
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100274 OP_iflag,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000275 /* Must be in the same order as FLAG_XXX! */
276 OP_conv_notrunc = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000277 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000278 OP_conv_noerror,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000279 OP_conv_fsync,
Denys Vlasenkob9413162013-08-19 09:01:39 +0200280 OP_conv_swab,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000281 /* Unimplemented conv=XXX: */
282 //nocreat do not create the output file
283 //excl fail if the output file already exists
284 //fdatasync physically write output file data before finishing
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000285 //lcase change upper case to lower case
286 //ucase change lower case to upper case
287 //block pad newline-terminated records with spaces to cbs-size
288 //unblock replace trailing spaces in cbs-size records with newline
289 //ascii from EBCDIC to ASCII
290 //ebcdic from ASCII to EBCDIC
291 //ibm from ASCII to alternate EBCDIC
Denys Vlasenkob9413162013-08-19 09:01:39 +0200292 /* Partially implemented: */
293 //swab swap every pair of input bytes: will abort on non-even reads
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100294 OP_iflag_skip_bytes,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000295#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000296 };
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200297 smallint exitcode = EXIT_FAILURE;
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200298 int i;
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200299 size_t ibs = 512;
300 char *ibuf;
301#if ENABLE_FEATURE_DD_IBS_OBS
302 size_t obs = 512;
303 char *obuf;
304#else
305# define obs ibs
306# define obuf ibuf
307#endif
308 /* These are all zeroed at once! */
Denis Vlasenko75878702007-07-27 15:02:00 +0000309 struct {
Denis Vlasenko75878702007-07-27 15:02:00 +0000310 size_t oc;
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200311 ssize_t prev_read_size; /* for detecting swab failure */
Denis Vlasenko75878702007-07-27 15:02:00 +0000312 off_t count;
313 off_t seek, skip;
314 const char *infile, *outfile;
Denis Vlasenko75878702007-07-27 15:02:00 +0000315 } Z;
Denis Vlasenko75878702007-07-27 15:02:00 +0000316#define oc (Z.oc )
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200317#define prev_read_size (Z.prev_read_size)
Denis Vlasenko75878702007-07-27 15:02:00 +0000318#define count (Z.count )
319#define seek (Z.seek )
320#define skip (Z.skip )
321#define infile (Z.infile )
322#define outfile (Z.outfile)
Eric Andersenddea3682000-11-29 22:33:02 +0000323
Denis Vlasenko75878702007-07-27 15:02:00 +0000324 memset(&Z, 0, sizeof(Z));
325 INIT_G();
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100326 //fflush_all(); - is this needed because of NOEXEC?
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000327
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200328 for (i = 1; argv[i]; i++) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000329 int what;
330 char *val;
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200331 char *arg = argv[i];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000332
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000333#if ENABLE_DESKTOP
334 /* "dd --". NB: coreutils 6.9 will complain if they see
335 * more than one of them. We wouldn't. */
336 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
337 continue;
338#endif
339 val = strchr(arg, '=');
340 if (val == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000341 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000342 *val = '\0';
343 what = index_in_strings(keywords, arg);
344 if (what < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000345 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000346 /* *val = '='; - to preserve ps listing? */
347 val++;
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000348#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000349 if (what == OP_ibs) {
350 /* Must fit into positive ssize_t */
Ari Sundholm88930232015-03-04 18:46:48 +0200351 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000352 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000353 }
354 if (what == OP_obs) {
Ari Sundholm88930232015-03-04 18:46:48 +0200355 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000356 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000357 }
358 if (what == OP_conv) {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100359 G.flags |= parse_comma_flags(val, conv_words, "conv");
360 /*continue;*/
361 }
362 if (what == OP_iflag) {
363 G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200364 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000365 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000366#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000367 if (what == OP_bs) {
Ari Sundholm88930232015-03-04 18:46:48 +0200368 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200369 obs = ibs;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000370 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000371 }
372 /* These can be large: */
373 if (what == OP_count) {
Ari Sundholmf22a8382015-02-07 01:41:22 +0100374 G.flags |= FLAG_COUNT;
Ari Sundholm88930232015-03-04 18:46:48 +0200375 count = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000376 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000377 }
378 if (what == OP_seek) {
Ari Sundholm88930232015-03-04 18:46:48 +0200379 seek = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000380 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000381 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000382 if (what == OP_skip) {
Ari Sundholm88930232015-03-04 18:46:48 +0200383 skip = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000384 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000385 }
386 if (what == OP_if) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000387 infile = val;
388 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000389 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000390 if (what == OP_of) {
391 outfile = val;
392 /*continue;*/
393 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100394#if ENABLE_FEATURE_DD_STATUS
395 if (what == OP_status) {
396 int n;
397 n = index_in_strings(status_words, val);
398 if (n < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200399 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status");
Denys Vlasenkod5b98e22017-07-14 13:44:30 +0200400 G.flags |= FLAG_STATUS_NONE << n;
Ari Sundholmf22a8382015-02-07 01:41:22 +0100401 /*continue;*/
402 }
403#endif
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200404 } /* end of "for (argv[i])" */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000405
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000406//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200407 ibuf = xmalloc(ibs);
408 obuf = ibuf;
409#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000410 if (ibs != obs) {
Ari Sundholmf22a8382015-02-07 01:41:22 +0100411 G.flags |= FLAG_TWOBUFS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000412 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000413 }
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200414#endif
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200415
416#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
417 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
418#endif
419#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
420 G.begin_time_us = monotonic_us();
421#endif
422
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200423 if (infile) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000424 xmove_fd(xopen(infile, O_RDONLY), ifd);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200425 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000426 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000427 }
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200428 if (outfile) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000429 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000430
Ari Sundholmf22a8382015-02-07 01:41:22 +0100431 if (!seek && !(G.flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000432 oflag |= O_TRUNC;
433
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000434 xmove_fd(xopen(outfile, oflag), ofd);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000435
Ari Sundholmf22a8382015-02-07 01:41:22 +0100436 if (seek && !(G.flags & FLAG_NOTRUNC)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000437 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000438 struct stat st;
439
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100440 if (fstat(ofd, &st) < 0
441 || S_ISREG(st.st_mode)
442 || S_ISDIR(st.st_mode)
443 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000444 goto die_outfile;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100445 }
Matt Kraaieb834782002-02-05 15:28:54 +0000446 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000447 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000448 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000449 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000451 if (skip) {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100452 size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
453 if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
Denys Vlasenko1b57fe12013-08-20 12:45:05 +0200454 do {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100455 ssize_t n = safe_read(ifd, ibuf, blocksz);
Rob Landley2686d3b2006-05-16 16:52:12 +0000456 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000457 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000458 if (n == 0)
459 break;
Denys Vlasenko1b57fe12013-08-20 12:45:05 +0200460 } while (--skip != 0);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000461 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000462 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000463 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000464 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000465 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000466 }
Eric Andersenddea3682000-11-29 22:33:02 +0000467
Ari Sundholmf22a8382015-02-07 01:41:22 +0100468 while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200469 ssize_t n;
470
Rob Landley2686d3b2006-05-16 16:52:12 +0000471 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000472 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000473 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000474 if (n < 0) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200475 /* "Bad block" */
Ari Sundholmf22a8382015-02-07 01:41:22 +0100476 if (!(G.flags & FLAG_NOERROR))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000477 goto die_infile;
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000478 bb_simple_perror_msg(infile);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200479 /* GNU dd with conv=noerror skips over bad blocks */
Denys Vlasenko334fcc82009-08-05 23:16:27 +0200480 xlseek(ifd, ibs, SEEK_CUR);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200481 /* conv=noerror,sync writes NULs,
482 * conv=noerror just ignores input bad blocks */
483 n = 0;
Eric Andersenef38b392002-04-27 01:31:43 +0000484 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100485 if (G.flags & FLAG_SWAB) {
Denys Vlasenko0ff0b322013-08-20 12:49:28 +0200486 uint16_t *p16;
487 ssize_t n2;
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200488
489 /* Our code allows only last read to be odd-sized */
490 if (prev_read_size & 1)
491 bb_error_msg_and_die("can't swab %lu byte buffer",
492 (unsigned long)prev_read_size);
493 prev_read_size = n;
494
Denys Vlasenko90e9cfd2013-08-20 12:27:19 +0200495 /* If n is odd, last byte is not swapped:
Denys Vlasenko8395bd32013-08-19 10:30:55 +0200496 * echo -n "qwe" | dd conv=swab
Denys Vlasenkob9413162013-08-19 09:01:39 +0200497 * prints "wqe".
Denys Vlasenkob9413162013-08-19 09:01:39 +0200498 */
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200499 p16 = (void*) ibuf;
Denys Vlasenko0ff0b322013-08-20 12:49:28 +0200500 n2 = (n >> 1);
501 while (--n2 >= 0) {
Denys Vlasenkob9413162013-08-19 09:01:39 +0200502 *p16 = bswap_16(*p16);
503 p16++;
504 }
505 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000506 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000507 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000508 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000509 G.in_part++;
Ari Sundholmf22a8382015-02-07 01:41:22 +0100510 if (G.flags & FLAG_SYNC) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200511 memset(ibuf + n, 0, ibs - n);
Rob Landley2686d3b2006-05-16 16:52:12 +0000512 n = ibs;
513 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000514 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100515 if (G.flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000516 char *tmp = ibuf;
517 while (n) {
518 size_t d = obs - oc;
519
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000520 if (d > (size_t)n)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000521 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000522 memcpy(obuf + oc, tmp, d);
523 n -= d;
524 tmp += d;
525 oc += d;
526 if (oc == obs) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000527 if (write_and_stats(obuf, obs, obs, outfile))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000528 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000529 oc = 0;
530 }
531 }
Denys Vlasenkof3114a32013-08-20 12:40:29 +0200532 } else {
533 if (write_and_stats(ibuf, n, obs, outfile))
534 goto out_status;
535 }
Rostislav Skudnovdba0dc12017-03-16 20:54:35 +0100536 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000537
Rostislav Skudnovdba0dc12017-03-16 20:54:35 +0100538 if (G.flags & FLAG_FSYNC) {
539 if (fsync(ofd) < 0)
540 goto die_outfile;
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000541 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000542
Rob Landley2686d3b2006-05-16 16:52:12 +0000543 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denys Vlasenkof3114a32013-08-20 12:40:29 +0200544 if (write_and_stats(obuf, oc, obs, outfile))
545 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000546 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000547 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000548 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000549 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000550 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000551
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000552 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000553 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000554 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000555 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000556
557 exitcode = EXIT_SUCCESS;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000558 out_status:
Ari Sundholmf22a8382015-02-07 01:41:22 +0100559 if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
560 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000561
Alexey Solovievf9221542011-03-16 10:39:46 +0300562 if (ENABLE_FEATURE_CLEAN_UP) {
563 free(obuf);
Ari Sundholmf22a8382015-02-07 01:41:22 +0100564 if (G.flags & FLAG_TWOBUFS)
Alexey Solovievf9221542011-03-16 10:39:46 +0300565 free(ibuf);
566 }
567
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000568 return exitcode;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000569}