blob: a5b8882a0fb11caf194f407fe589f9661b2349bd [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 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 *
Matt Kraai3eeab3b2001-12-07 16:27:37 +00006 * Copyright (C) 2000,2001 Matt Kraai
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersencc8ed391999-10-05 16:24:54 +00009 */
10
Ari Sundholmf22a8382015-02-07 01:41:22 +010011//config:config DD
12//config: bool "dd"
13//config: default y
14//config: help
15//config: dd copies a file (from standard input to standard output,
16//config: by default) using specific input and output blocksizes,
17//config: while optionally performing conversions on it.
18//config:
19//config:config FEATURE_DD_SIGNAL_HANDLING
20//config: bool "Enable signal handling for status reporting"
21//config: default y
22//config: depends on DD
23//config: help
24//config: Sending a SIGUSR1 signal to a running `dd' process makes it
25//config: print to standard error the number of records read and written
26//config: so far, then to resume copying.
27//config:
28//config: $ dd if=/dev/zero of=/dev/null &
29//config: $ pid=$!; kill -USR1 $pid; sleep 1; kill $pid
30//config: 10899206+0 records in
31//config: 10899206+0 records out
32//config:
33//config:config FEATURE_DD_THIRD_STATUS_LINE
34//config: bool "Enable the third status line upon signal"
35//config: default y
36//config: depends on DD && FEATURE_DD_SIGNAL_HANDLING
37//config: help
38//config: Displays a coreutils-like third status line with transferred bytes,
39//config: elapsed time and speed.
40//config:
41//config:config FEATURE_DD_IBS_OBS
42//config: bool "Enable ibs, obs and conv options"
43//config: default y
44//config: depends on DD
45//config: help
46//config: Enables support for writing a certain number of bytes in and out,
47//config: at a time, and performing conversions on the data stream.
48//config:
49//config:config FEATURE_DD_STATUS
50//config: bool "Enable status display options"
51//config: default y
52//config: depends on DD
53//config: help
54//config: Enables support for status=noxfer/none option.
55
Pere Orga34425382011-03-31 14:43:25 +020056//usage:#define dd_trivial_usage
57//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 +010058//usage: " [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync] [iflag=skip_bytes]")
Pere Orga34425382011-03-31 14:43:25 +020059//usage:#define dd_full_usage "\n\n"
60//usage: "Copy a file with converting and formatting\n"
Pere Orga34425382011-03-31 14:43:25 +020061//usage: "\n if=FILE Read from FILE instead of stdin"
62//usage: "\n of=FILE Write to FILE instead of stdout"
63//usage: "\n bs=N Read and write N bytes at a time"
64//usage: IF_FEATURE_DD_IBS_OBS(
65//usage: "\n ibs=N Read N bytes at a time"
66//usage: )
67//usage: IF_FEATURE_DD_IBS_OBS(
68//usage: "\n obs=N Write N bytes at a time"
69//usage: )
70//usage: "\n count=N Copy only N input blocks"
71//usage: "\n skip=N Skip N input blocks"
72//usage: "\n seek=N Skip N output blocks"
73//usage: IF_FEATURE_DD_IBS_OBS(
74//usage: "\n conv=notrunc Don't truncate output file"
75//usage: "\n conv=noerror Continue after read errors"
76//usage: "\n conv=sync Pad blocks with zeros"
77//usage: "\n conv=fsync Physically write data out before finishing"
Denys Vlasenkob9413162013-08-19 09:01:39 +020078//usage: "\n conv=swab Swap every pair of bytes"
Rafał Miłeckic30a5b12016-02-01 02:17:28 +010079//usage: "\n iflag=skip_bytes skip=N is in bytes"
Pere Orga34425382011-03-31 14:43:25 +020080//usage: )
Ari Sundholmf22a8382015-02-07 01:41:22 +010081//usage: IF_FEATURE_DD_STATUS(
82//usage: "\n status=noxfer Suppress rate output"
83//usage: "\n status=none Suppress all output"
84//usage: )
Pere Orga34425382011-03-31 14:43:25 +020085//usage: "\n"
Ari Sundholmf22a8382015-02-07 01:41:22 +010086//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 +020087//usage:
88//usage:#define dd_example_usage
89//usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
90//usage: "4+0 records in\n"
91//usage: "4+0 records out\n"
92
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000093#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020094#include "common_bufsiz.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +000095
96/* This is a NOEXEC applet. Be very careful! */
97
Eric Andersencbe31da2001-02-20 06:14:08 +000098
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000099enum {
100 ifd = STDIN_FILENO,
101 ofd = STDOUT_FILENO,
102};
103
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000104struct globals {
105 off_t out_full, out_part, in_full, in_part;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200106#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
107 unsigned long long total_bytes;
108 unsigned long long begin_time_us;
109#endif
Ari Sundholmf22a8382015-02-07 01:41:22 +0100110 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100111} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200112#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200113#define INIT_G() do { \
114 /* we have to zero it out because of NOEXEC */ \
115 memset(&G, 0, sizeof(G)); \
116} while (0)
Denis Vlasenko75878702007-07-27 15:02:00 +0000117
Ari Sundholmf22a8382015-02-07 01:41:22 +0100118enum {
119 /* Must be in the same order as OP_conv_XXX! */
120 /* (see "flags |= (1 << what)" below) */
121 FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
122 FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
123 FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
124 FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
125 FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
126 /* end of conv flags */
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100127 /* start of input flags */
128 FLAG_IFLAG_SHIFT = 5,
129 FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
130 /* end of input flags */
131 FLAG_TWOBUFS = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
132 FLAG_COUNT = 1 << 7,
133 FLAG_STATUS = 1 << 8,
134 FLAG_STATUS_NONE = 1 << 9,
135 FLAG_STATUS_NOXFER = 1 << 10,
Ari Sundholmf22a8382015-02-07 01:41:22 +0100136};
Rob Landleyc5598172006-05-02 22:44:04 +0000137
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000138static void dd_output_status(int UNUSED_PARAM cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +0000139{
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200140#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200141 double seconds;
142 unsigned long long bytes_sec;
143 unsigned long long now_us = monotonic_us(); /* before fprintf */
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200144#endif
145
Denis Vlasenko980864d2007-07-30 10:58:09 +0000146 /* Deliberately using %u, not %d */
147 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
148 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000149 G.in_full, G.in_part,
150 G.out_full, G.out_part);
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200151
152#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Ari Sundholmf22a8382015-02-07 01:41:22 +0100153# if ENABLE_FEATURE_DD_STATUS
154 if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
155 return;
156 //TODO: should status=none make dd stop reacting to USR1 entirely?
157 //So far we react to it (we print the stats),
158 //status=none only suppresses final, non-USR1 generated status message.
159# endif
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200160 fprintf(stderr, "%llu bytes (%sB) copied, ",
161 G.total_bytes,
162 /* show fractional digit, use suffixes */
163 make_human_readable_str(G.total_bytes, 1, 0)
164 );
165 /* Corner cases:
166 * ./busybox dd </dev/null >/dev/null
167 * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
168 * (echo DONE) | ./busybox dd >/dev/null
169 * (sleep 1; echo DONE) | ./busybox dd >/dev/null
170 */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200171 seconds = (now_us - G.begin_time_us) / 1000000.0;
172 bytes_sec = G.total_bytes / seconds;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200173 fprintf(stderr, "%f seconds, %sB/s\n",
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200174 seconds,
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200175 /* show fractional digit, use suffixes */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200176 make_human_readable_str(bytes_sec, 1, 0)
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200177 );
178#endif
Rob Landleyc5598172006-05-02 22:44:04 +0000179}
180
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000181static ssize_t full_write_or_warn(const void *buf, size_t len,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000182 const char *const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000183{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000184 ssize_t n = full_write(ofd, buf, len);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000185 if (n < 0)
186 bb_perror_msg("writing '%s'", filename);
187 return n;
188}
189
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000190static bool write_and_stats(const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000191 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000192{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000193 ssize_t n = full_write_or_warn(buf, len, filename);
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000194 if (n < 0)
195 return 1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000196 if ((size_t)n == obs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000197 G.out_full++;
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000198 else if (n) /* > 0 */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000199 G.out_part++;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200200#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
201 G.total_bytes += n;
202#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000203 return 0;
204}
205
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000206#if ENABLE_LFS
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200207# define XATOU_SFX xatoull_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000208#else
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200209# define XATOU_SFX xatoul_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000210#endif
211
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100212#if ENABLE_FEATURE_DD_IBS_OBS
213static int parse_comma_flags(char *val, const char *words, const char *error_in)
214{
215 int flags = 0;
216 while (1) {
217 int n;
218 char *arg;
219 /* find ',', replace them with NUL so we can use val for
220 * index_in_strings() without copying.
221 * We rely on val being non-null, else strchr would fault.
222 */
223 arg = strchr(val, ',');
224 if (arg)
225 *arg = '\0';
226 n = index_in_strings(words, val);
227 if (n < 0)
228 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in);
229 flags |= (1 << n);
230 if (!arg) /* no ',' left, so this was the last specifier */
231 break;
232 *arg = ','; /* to preserve ps listing */
233 val = arg + 1; /* skip this keyword and ',' */
234 }
235 return flags;
236}
237#endif
238
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000239int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000240int dd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000241{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000242 static const char keywords[] ALIGN1 =
Ari Sundholmf22a8382015-02-07 01:41:22 +0100243 "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 +0000244#if ENABLE_FEATURE_DD_IBS_OBS
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100245 "ibs\0""obs\0""conv\0""iflag\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000246#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000247 ;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000248#if ENABLE_FEATURE_DD_IBS_OBS
249 static const char conv_words[] ALIGN1 =
Denys Vlasenkob9413162013-08-19 09:01:39 +0200250 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100251 static const char iflag_words[] ALIGN1 =
252 "skip_bytes\0";
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000253#endif
Ari Sundholmf22a8382015-02-07 01:41:22 +0100254#if ENABLE_FEATURE_DD_STATUS
255 static const char status_words[] ALIGN1 =
256 "none\0""noxfer\0";
257#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000258 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000259 OP_bs = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000260 OP_count,
261 OP_seek,
262 OP_skip,
263 OP_if,
264 OP_of,
Ari Sundholmf22a8382015-02-07 01:41:22 +0100265 IF_FEATURE_DD_STATUS(OP_status,)
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000266#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000267 OP_ibs,
268 OP_obs,
269 OP_conv,
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100270 OP_iflag,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000271 /* Must be in the same order as FLAG_XXX! */
272 OP_conv_notrunc = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000273 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000274 OP_conv_noerror,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000275 OP_conv_fsync,
Denys Vlasenkob9413162013-08-19 09:01:39 +0200276 OP_conv_swab,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000277 /* Unimplemented conv=XXX: */
278 //nocreat do not create the output file
279 //excl fail if the output file already exists
280 //fdatasync physically write output file data before finishing
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000281 //lcase change upper case to lower case
282 //ucase change lower case to upper case
283 //block pad newline-terminated records with spaces to cbs-size
284 //unblock replace trailing spaces in cbs-size records with newline
285 //ascii from EBCDIC to ASCII
286 //ebcdic from ASCII to EBCDIC
287 //ibm from ASCII to alternate EBCDIC
Denys Vlasenkob9413162013-08-19 09:01:39 +0200288 /* Partially implemented: */
289 //swab swap every pair of input bytes: will abort on non-even reads
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100290 OP_iflag_skip_bytes,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000291#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000292 };
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200293 smallint exitcode = EXIT_FAILURE;
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200294 int i;
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200295 size_t ibs = 512;
296 char *ibuf;
297#if ENABLE_FEATURE_DD_IBS_OBS
298 size_t obs = 512;
299 char *obuf;
300#else
301# define obs ibs
302# define obuf ibuf
303#endif
304 /* These are all zeroed at once! */
Denis Vlasenko75878702007-07-27 15:02:00 +0000305 struct {
Denis Vlasenko75878702007-07-27 15:02:00 +0000306 size_t oc;
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200307 ssize_t prev_read_size; /* for detecting swab failure */
Denis Vlasenko75878702007-07-27 15:02:00 +0000308 off_t count;
309 off_t seek, skip;
310 const char *infile, *outfile;
Denis Vlasenko75878702007-07-27 15:02:00 +0000311 } Z;
Denis Vlasenko75878702007-07-27 15:02:00 +0000312#define oc (Z.oc )
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200313#define prev_read_size (Z.prev_read_size)
Denis Vlasenko75878702007-07-27 15:02:00 +0000314#define count (Z.count )
315#define seek (Z.seek )
316#define skip (Z.skip )
317#define infile (Z.infile )
318#define outfile (Z.outfile)
Eric Andersenddea3682000-11-29 22:33:02 +0000319
Denis Vlasenko75878702007-07-27 15:02:00 +0000320 memset(&Z, 0, sizeof(Z));
321 INIT_G();
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100322 //fflush_all(); - is this needed because of NOEXEC?
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000323
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200324 for (i = 1; argv[i]; i++) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000325 int what;
326 char *val;
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200327 char *arg = argv[i];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000328
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000329#if ENABLE_DESKTOP
330 /* "dd --". NB: coreutils 6.9 will complain if they see
331 * more than one of them. We wouldn't. */
332 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
333 continue;
334#endif
335 val = strchr(arg, '=');
336 if (val == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000337 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000338 *val = '\0';
339 what = index_in_strings(keywords, arg);
340 if (what < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000341 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000342 /* *val = '='; - to preserve ps listing? */
343 val++;
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000344#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000345 if (what == OP_ibs) {
346 /* Must fit into positive ssize_t */
Ari Sundholm88930232015-03-04 18:46:48 +0200347 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000348 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000349 }
350 if (what == OP_obs) {
Ari Sundholm88930232015-03-04 18:46:48 +0200351 obs = 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_conv) {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100355 G.flags |= parse_comma_flags(val, conv_words, "conv");
356 /*continue;*/
357 }
358 if (what == OP_iflag) {
359 G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200360 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000361 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000362#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000363 if (what == OP_bs) {
Ari Sundholm88930232015-03-04 18:46:48 +0200364 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200365 obs = ibs;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000366 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000367 }
368 /* These can be large: */
369 if (what == OP_count) {
Ari Sundholmf22a8382015-02-07 01:41:22 +0100370 G.flags |= FLAG_COUNT;
Ari Sundholm88930232015-03-04 18:46:48 +0200371 count = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000372 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000373 }
374 if (what == OP_seek) {
Ari Sundholm88930232015-03-04 18:46:48 +0200375 seek = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000376 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000377 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000378 if (what == OP_skip) {
Ari Sundholm88930232015-03-04 18:46:48 +0200379 skip = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000380 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000381 }
382 if (what == OP_if) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000383 infile = val;
384 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000385 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000386 if (what == OP_of) {
387 outfile = val;
388 /*continue;*/
389 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100390#if ENABLE_FEATURE_DD_STATUS
391 if (what == OP_status) {
392 int n;
393 n = index_in_strings(status_words, val);
394 if (n < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200395 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status");
Ari Sundholmf22a8382015-02-07 01:41:22 +0100396 G.flags |= FLAG_STATUS << n;
397 /*continue;*/
398 }
399#endif
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200400 } /* end of "for (argv[i])" */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000401
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000402//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200403 ibuf = xmalloc(ibs);
404 obuf = ibuf;
405#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000406 if (ibs != obs) {
Ari Sundholmf22a8382015-02-07 01:41:22 +0100407 G.flags |= FLAG_TWOBUFS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000408 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000409 }
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200410#endif
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200411
412#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
413 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
414#endif
415#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
416 G.begin_time_us = monotonic_us();
417#endif
418
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200419 if (infile) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000420 xmove_fd(xopen(infile, O_RDONLY), ifd);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200421 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000422 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000423 }
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200424 if (outfile) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000425 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000426
Ari Sundholmf22a8382015-02-07 01:41:22 +0100427 if (!seek && !(G.flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000428 oflag |= O_TRUNC;
429
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000430 xmove_fd(xopen(outfile, oflag), ofd);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000431
Ari Sundholmf22a8382015-02-07 01:41:22 +0100432 if (seek && !(G.flags & FLAG_NOTRUNC)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000433 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000434 struct stat st;
435
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100436 if (fstat(ofd, &st) < 0
437 || S_ISREG(st.st_mode)
438 || S_ISDIR(st.st_mode)
439 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000440 goto die_outfile;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100441 }
Matt Kraaieb834782002-02-05 15:28:54 +0000442 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000443 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000444 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000445 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000447 if (skip) {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100448 size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
449 if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
Denys Vlasenko1b57fe12013-08-20 12:45:05 +0200450 do {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100451 ssize_t n = safe_read(ifd, ibuf, blocksz);
Rob Landley2686d3b2006-05-16 16:52:12 +0000452 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000453 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000454 if (n == 0)
455 break;
Denys Vlasenko1b57fe12013-08-20 12:45:05 +0200456 } while (--skip != 0);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000457 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000458 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000459 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000460 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000461 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000462 }
Eric Andersenddea3682000-11-29 22:33:02 +0000463
Ari Sundholmf22a8382015-02-07 01:41:22 +0100464 while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200465 ssize_t n;
466
Rob Landley2686d3b2006-05-16 16:52:12 +0000467 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000468 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000469 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000470 if (n < 0) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200471 /* "Bad block" */
Ari Sundholmf22a8382015-02-07 01:41:22 +0100472 if (!(G.flags & FLAG_NOERROR))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000473 goto die_infile;
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000474 bb_simple_perror_msg(infile);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200475 /* GNU dd with conv=noerror skips over bad blocks */
Denys Vlasenko334fcc82009-08-05 23:16:27 +0200476 xlseek(ifd, ibs, SEEK_CUR);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200477 /* conv=noerror,sync writes NULs,
478 * conv=noerror just ignores input bad blocks */
479 n = 0;
Eric Andersenef38b392002-04-27 01:31:43 +0000480 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100481 if (G.flags & FLAG_SWAB) {
Denys Vlasenko0ff0b322013-08-20 12:49:28 +0200482 uint16_t *p16;
483 ssize_t n2;
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200484
485 /* Our code allows only last read to be odd-sized */
486 if (prev_read_size & 1)
487 bb_error_msg_and_die("can't swab %lu byte buffer",
488 (unsigned long)prev_read_size);
489 prev_read_size = n;
490
Denys Vlasenko90e9cfd2013-08-20 12:27:19 +0200491 /* If n is odd, last byte is not swapped:
Denys Vlasenko8395bd32013-08-19 10:30:55 +0200492 * echo -n "qwe" | dd conv=swab
Denys Vlasenkob9413162013-08-19 09:01:39 +0200493 * prints "wqe".
Denys Vlasenkob9413162013-08-19 09:01:39 +0200494 */
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200495 p16 = (void*) ibuf;
Denys Vlasenko0ff0b322013-08-20 12:49:28 +0200496 n2 = (n >> 1);
497 while (--n2 >= 0) {
Denys Vlasenkob9413162013-08-19 09:01:39 +0200498 *p16 = bswap_16(*p16);
499 p16++;
500 }
501 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000502 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000503 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000504 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000505 G.in_part++;
Ari Sundholmf22a8382015-02-07 01:41:22 +0100506 if (G.flags & FLAG_SYNC) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200507 memset(ibuf + n, 0, ibs - n);
Rob Landley2686d3b2006-05-16 16:52:12 +0000508 n = ibs;
509 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000510 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100511 if (G.flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000512 char *tmp = ibuf;
513 while (n) {
514 size_t d = obs - oc;
515
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000516 if (d > (size_t)n)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000517 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000518 memcpy(obuf + oc, tmp, d);
519 n -= d;
520 tmp += d;
521 oc += d;
522 if (oc == obs) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000523 if (write_and_stats(obuf, obs, obs, outfile))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000524 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000525 oc = 0;
526 }
527 }
Denys Vlasenkof3114a32013-08-20 12:40:29 +0200528 } else {
529 if (write_and_stats(ibuf, n, obs, outfile))
530 goto out_status;
531 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000532
Ari Sundholmf22a8382015-02-07 01:41:22 +0100533 if (G.flags & FLAG_FSYNC) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000534 if (fsync(ofd) < 0)
535 goto die_outfile;
536 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000537 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000538
Rob Landley2686d3b2006-05-16 16:52:12 +0000539 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denys Vlasenkof3114a32013-08-20 12:40:29 +0200540 if (write_and_stats(obuf, oc, obs, outfile))
541 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000542 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000543 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000544 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000545 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000546 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000547
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000548 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000549 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000550 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000551 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000552
553 exitcode = EXIT_SUCCESS;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000554 out_status:
Ari Sundholmf22a8382015-02-07 01:41:22 +0100555 if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
556 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000557
Alexey Solovievf9221542011-03-16 10:39:46 +0300558 if (ENABLE_FEATURE_CLEAN_UP) {
559 free(obuf);
Ari Sundholmf22a8382015-02-07 01:41:22 +0100560 if (G.flags & FLAG_TWOBUFS)
Alexey Solovievf9221542011-03-16 10:39:46 +0300561 free(ibuf);
562 }
563
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000564 return exitcode;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000565}