blob: 39ce5019f82e41fcb9d6111aa7e6aad7b5a9206b [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
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +000040//config: bool "Enable ibs, obs, iflag, oflag and conv options"
Ari Sundholmf22a8382015-02-07 01:41:22 +010041//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
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +010059//usage: "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]\n"
60//usage: IF_FEATURE_DD_IBS_OBS(
61//usage: " [conv=notrunc|noerror|sync|fsync]\n"
62//usage: " [iflag=skip_bytes|fullblock] [oflag=seek_bytes]"
63//usage: )
Pere Orga34425382011-03-31 14:43:25 +020064//usage:#define dd_full_usage "\n\n"
65//usage: "Copy a file with converting and formatting\n"
Pere Orga34425382011-03-31 14:43:25 +020066//usage: "\n if=FILE Read from FILE instead of stdin"
67//usage: "\n of=FILE Write to FILE instead of stdout"
68//usage: "\n bs=N Read and write N bytes at a time"
69//usage: IF_FEATURE_DD_IBS_OBS(
70//usage: "\n ibs=N Read N bytes at a time"
71//usage: )
72//usage: IF_FEATURE_DD_IBS_OBS(
73//usage: "\n obs=N Write N bytes at a time"
74//usage: )
75//usage: "\n count=N Copy only N input blocks"
76//usage: "\n skip=N Skip N input blocks"
77//usage: "\n seek=N Skip N output blocks"
78//usage: IF_FEATURE_DD_IBS_OBS(
79//usage: "\n conv=notrunc Don't truncate output file"
80//usage: "\n conv=noerror Continue after read errors"
81//usage: "\n conv=sync Pad blocks with zeros"
82//usage: "\n conv=fsync Physically write data out before finishing"
Denys Vlasenkob9413162013-08-19 09:01:39 +020083//usage: "\n conv=swab Swap every pair of bytes"
Rafał Miłeckic30a5b12016-02-01 02:17:28 +010084//usage: "\n iflag=skip_bytes skip=N is in bytes"
Nicholas Clark77a66782018-01-25 19:00:19 +010085//usage: "\n iflag=fullblock Read full blocks"
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +000086//usage: "\n oflag=seek_bytes seek=N is in bytes"
Pere Orga34425382011-03-31 14:43:25 +020087//usage: )
Ari Sundholmf22a8382015-02-07 01:41:22 +010088//usage: IF_FEATURE_DD_STATUS(
89//usage: "\n status=noxfer Suppress rate output"
90//usage: "\n status=none Suppress all output"
91//usage: )
Pere Orga34425382011-03-31 14:43:25 +020092//usage: "\n"
Ari Sundholmf22a8382015-02-07 01:41:22 +010093//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 +020094//usage:
95//usage:#define dd_example_usage
96//usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
97//usage: "4+0 records in\n"
98//usage: "4+0 records out\n"
99
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000100#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200101#include "common_bufsiz.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000102
103/* This is a NOEXEC applet. Be very careful! */
104
Eric Andersencbe31da2001-02-20 06:14:08 +0000105
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000106enum {
107 ifd = STDIN_FILENO,
108 ofd = STDOUT_FILENO,
109};
110
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000111struct globals {
112 off_t out_full, out_part, in_full, in_part;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200113#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
114 unsigned long long total_bytes;
115 unsigned long long begin_time_us;
116#endif
Ari Sundholmf22a8382015-02-07 01:41:22 +0100117 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100118} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200119#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200120#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200121 setup_common_bufsiz(); \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200122 /* we have to zero it out because of NOEXEC */ \
123 memset(&G, 0, sizeof(G)); \
124} while (0)
Denis Vlasenko75878702007-07-27 15:02:00 +0000125
Ari Sundholmf22a8382015-02-07 01:41:22 +0100126enum {
127 /* Must be in the same order as OP_conv_XXX! */
128 /* (see "flags |= (1 << what)" below) */
129 FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
130 FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
131 FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
132 FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
133 FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
134 /* end of conv flags */
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100135 /* start of input flags */
136 FLAG_IFLAG_SHIFT = 5,
137 FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
Nicholas Clark77a66782018-01-25 19:00:19 +0100138 FLAG_FULLBLOCK = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100139 /* end of input flags */
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000140 /* start of output flags */
141 FLAG_OFLAG_SHIFT = 7,
142 FLAG_SEEK_BYTES = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
143 /* end of output flags */
144 FLAG_TWOBUFS = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
145 FLAG_COUNT = 1 << 9,
146 FLAG_STATUS_NONE = 1 << 10,
147 FLAG_STATUS_NOXFER = 1 << 11,
Ari Sundholmf22a8382015-02-07 01:41:22 +0100148};
Rob Landleyc5598172006-05-02 22:44:04 +0000149
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000150static void dd_output_status(int UNUSED_PARAM cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +0000151{
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200152#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200153 double seconds;
154 unsigned long long bytes_sec;
155 unsigned long long now_us = monotonic_us(); /* before fprintf */
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200156#endif
157
Denis Vlasenko980864d2007-07-30 10:58:09 +0000158 /* Deliberately using %u, not %d */
159 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
160 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000161 G.in_full, G.in_part,
162 G.out_full, G.out_part);
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200163
164#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Ari Sundholmf22a8382015-02-07 01:41:22 +0100165# if ENABLE_FEATURE_DD_STATUS
166 if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
167 return;
168 //TODO: should status=none make dd stop reacting to USR1 entirely?
169 //So far we react to it (we print the stats),
170 //status=none only suppresses final, non-USR1 generated status message.
171# endif
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200172 fprintf(stderr, "%llu bytes (%sB) copied, ",
173 G.total_bytes,
174 /* show fractional digit, use suffixes */
175 make_human_readable_str(G.total_bytes, 1, 0)
176 );
177 /* Corner cases:
178 * ./busybox dd </dev/null >/dev/null
179 * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
180 * (echo DONE) | ./busybox dd >/dev/null
181 * (sleep 1; echo DONE) | ./busybox dd >/dev/null
182 */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200183 seconds = (now_us - G.begin_time_us) / 1000000.0;
184 bytes_sec = G.total_bytes / seconds;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200185 fprintf(stderr, "%f seconds, %sB/s\n",
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200186 seconds,
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200187 /* show fractional digit, use suffixes */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200188 make_human_readable_str(bytes_sec, 1, 0)
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200189 );
190#endif
Rob Landleyc5598172006-05-02 22:44:04 +0000191}
192
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000193static ssize_t full_write_or_warn(const void *buf, size_t len,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000194 const char *const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000195{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000196 ssize_t n = full_write(ofd, buf, len);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000197 if (n < 0)
198 bb_perror_msg("writing '%s'", filename);
199 return n;
200}
201
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000202static bool write_and_stats(const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000203 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000204{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000205 ssize_t n = full_write_or_warn(buf, len, filename);
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000206 if (n < 0)
207 return 1;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200208#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
209 G.total_bytes += n;
210#endif
Denys Vlasenko5afd63a2018-01-10 10:58:55 +0100211 if ((size_t)n == obs) {
212 G.out_full++;
213 return 0;
214 }
Denys Vlasenko2c876772018-01-10 11:04:09 +0100215 if ((size_t)n == len) {
Denys Vlasenko5afd63a2018-01-10 10:58:55 +0100216 G.out_part++;
Denys Vlasenko2c876772018-01-10 11:04:09 +0100217 return 0;
218 }
Denys Vlasenko5afd63a2018-01-10 10:58:55 +0100219 return 1;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000220}
221
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000222#if ENABLE_LFS
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200223# define XATOU_SFX xatoull_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000224#else
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200225# define XATOU_SFX xatoul_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000226#endif
227
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100228#if ENABLE_FEATURE_DD_IBS_OBS
229static int parse_comma_flags(char *val, const char *words, const char *error_in)
230{
231 int flags = 0;
232 while (1) {
233 int n;
234 char *arg;
235 /* find ',', replace them with NUL so we can use val for
236 * index_in_strings() without copying.
237 * We rely on val being non-null, else strchr would fault.
238 */
239 arg = strchr(val, ',');
240 if (arg)
241 *arg = '\0';
242 n = index_in_strings(words, val);
243 if (n < 0)
244 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in);
245 flags |= (1 << n);
246 if (!arg) /* no ',' left, so this was the last specifier */
247 break;
248 *arg = ','; /* to preserve ps listing */
249 val = arg + 1; /* skip this keyword and ',' */
250 }
251 return flags;
252}
253#endif
254
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000255int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000256int dd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000257{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000258 static const char keywords[] ALIGN1 =
Ari Sundholmf22a8382015-02-07 01:41:22 +0100259 "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 +0000260#if ENABLE_FEATURE_DD_IBS_OBS
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000261 "ibs\0""obs\0""conv\0""iflag\0""oflag\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000262#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000263 ;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000264#if ENABLE_FEATURE_DD_IBS_OBS
265 static const char conv_words[] ALIGN1 =
Denys Vlasenkob9413162013-08-19 09:01:39 +0200266 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100267 static const char iflag_words[] ALIGN1 =
Nicholas Clark77a66782018-01-25 19:00:19 +0100268 "skip_bytes\0""fullblock\0";
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000269 static const char oflag_words[] ALIGN1 =
270 "seek_bytes\0";
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000271#endif
Ari Sundholmf22a8382015-02-07 01:41:22 +0100272#if ENABLE_FEATURE_DD_STATUS
273 static const char status_words[] ALIGN1 =
274 "none\0""noxfer\0";
275#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000276 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000277 OP_bs = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000278 OP_count,
279 OP_seek,
280 OP_skip,
281 OP_if,
282 OP_of,
Ari Sundholmf22a8382015-02-07 01:41:22 +0100283 IF_FEATURE_DD_STATUS(OP_status,)
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000284#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000285 OP_ibs,
286 OP_obs,
287 OP_conv,
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100288 OP_iflag,
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000289 OP_oflag,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000290 /* Must be in the same order as FLAG_XXX! */
291 OP_conv_notrunc = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000292 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000293 OP_conv_noerror,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000294 OP_conv_fsync,
Denys Vlasenkob9413162013-08-19 09:01:39 +0200295 OP_conv_swab,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000296 /* Unimplemented conv=XXX: */
297 //nocreat do not create the output file
298 //excl fail if the output file already exists
299 //fdatasync physically write output file data before finishing
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000300 //lcase change upper case to lower case
301 //ucase change lower case to upper case
302 //block pad newline-terminated records with spaces to cbs-size
303 //unblock replace trailing spaces in cbs-size records with newline
304 //ascii from EBCDIC to ASCII
305 //ebcdic from ASCII to EBCDIC
306 //ibm from ASCII to alternate EBCDIC
Denys Vlasenkob9413162013-08-19 09:01:39 +0200307 /* Partially implemented: */
308 //swab swap every pair of input bytes: will abort on non-even reads
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100309 OP_iflag_skip_bytes,
Nicholas Clark77a66782018-01-25 19:00:19 +0100310 OP_iflag_fullblock,
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000311 OP_oflag_seek_bytes,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000312#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000313 };
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200314 smallint exitcode = EXIT_FAILURE;
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200315 int i;
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200316 size_t ibs = 512;
317 char *ibuf;
318#if ENABLE_FEATURE_DD_IBS_OBS
319 size_t obs = 512;
320 char *obuf;
321#else
322# define obs ibs
323# define obuf ibuf
324#endif
325 /* These are all zeroed at once! */
Denis Vlasenko75878702007-07-27 15:02:00 +0000326 struct {
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100327 IF_FEATURE_DD_IBS_OBS(size_t ocount;)
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200328 ssize_t prev_read_size; /* for detecting swab failure */
Denis Vlasenko75878702007-07-27 15:02:00 +0000329 off_t count;
330 off_t seek, skip;
331 const char *infile, *outfile;
Denis Vlasenko75878702007-07-27 15:02:00 +0000332 } Z;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100333#define ocount (Z.ocount )
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200334#define prev_read_size (Z.prev_read_size)
Denis Vlasenko75878702007-07-27 15:02:00 +0000335#define count (Z.count )
336#define seek (Z.seek )
337#define skip (Z.skip )
338#define infile (Z.infile )
339#define outfile (Z.outfile)
Eric Andersenddea3682000-11-29 22:33:02 +0000340
Denis Vlasenko75878702007-07-27 15:02:00 +0000341 memset(&Z, 0, sizeof(Z));
342 INIT_G();
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100343 //fflush_all(); - is this needed because of NOEXEC?
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000344
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200345 for (i = 1; argv[i]; i++) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000346 int what;
347 char *val;
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200348 char *arg = argv[i];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000349
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000350#if ENABLE_DESKTOP
351 /* "dd --". NB: coreutils 6.9 will complain if they see
352 * more than one of them. We wouldn't. */
353 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
354 continue;
355#endif
356 val = strchr(arg, '=');
357 if (val == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000358 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000359 *val = '\0';
360 what = index_in_strings(keywords, arg);
361 if (what < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000362 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000363 /* *val = '='; - to preserve ps listing? */
364 val++;
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000365#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000366 if (what == OP_ibs) {
367 /* Must fit into positive ssize_t */
Ari Sundholm88930232015-03-04 18:46:48 +0200368 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000369 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000370 }
371 if (what == OP_obs) {
Ari Sundholm88930232015-03-04 18:46:48 +0200372 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000373 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000374 }
375 if (what == OP_conv) {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100376 G.flags |= parse_comma_flags(val, conv_words, "conv");
377 /*continue;*/
378 }
379 if (what == OP_iflag) {
380 G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200381 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000382 }
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000383 if (what == OP_oflag) {
384 G.flags |= parse_comma_flags(val, oflag_words, "oflag") << FLAG_OFLAG_SHIFT;
385 /*continue;*/
386 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000387#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000388 if (what == OP_bs) {
Ari Sundholm88930232015-03-04 18:46:48 +0200389 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200390 obs = ibs;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000391 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000392 }
393 /* These can be large: */
394 if (what == OP_count) {
Ari Sundholmf22a8382015-02-07 01:41:22 +0100395 G.flags |= FLAG_COUNT;
Ari Sundholm88930232015-03-04 18:46:48 +0200396 count = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000397 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000398 }
399 if (what == OP_seek) {
Ari Sundholm88930232015-03-04 18:46:48 +0200400 seek = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000401 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000402 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000403 if (what == OP_skip) {
Ari Sundholm88930232015-03-04 18:46:48 +0200404 skip = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000405 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000406 }
407 if (what == OP_if) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000408 infile = val;
409 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000410 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000411 if (what == OP_of) {
412 outfile = val;
413 /*continue;*/
414 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100415#if ENABLE_FEATURE_DD_STATUS
416 if (what == OP_status) {
417 int n;
418 n = index_in_strings(status_words, val);
419 if (n < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200420 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status");
Denys Vlasenkod5b98e22017-07-14 13:44:30 +0200421 G.flags |= FLAG_STATUS_NONE << n;
Ari Sundholmf22a8382015-02-07 01:41:22 +0100422 /*continue;*/
423 }
424#endif
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200425 } /* end of "for (argv[i])" */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000426
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000427//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200428 ibuf = xmalloc(ibs);
429 obuf = ibuf;
430#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000431 if (ibs != obs) {
Ari Sundholmf22a8382015-02-07 01:41:22 +0100432 G.flags |= FLAG_TWOBUFS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000433 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000434 }
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200435#endif
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200436
437#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
438 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
439#endif
440#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
441 G.begin_time_us = monotonic_us();
442#endif
443
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200444 if (infile) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000445 xmove_fd(xopen(infile, O_RDONLY), ifd);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200446 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000447 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 }
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200449 if (outfile) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000450 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000451
Ari Sundholmf22a8382015-02-07 01:41:22 +0100452 if (!seek && !(G.flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000453 oflag |= O_TRUNC;
454
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000455 xmove_fd(xopen(outfile, oflag), ofd);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000456
Ari Sundholmf22a8382015-02-07 01:41:22 +0100457 if (seek && !(G.flags & FLAG_NOTRUNC)) {
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000458 size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
459 if (ftruncate(ofd, seek * blocksz) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000460 struct stat st;
461
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100462 if (fstat(ofd, &st) < 0
463 || S_ISREG(st.st_mode)
464 || S_ISDIR(st.st_mode)
465 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000466 goto die_outfile;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100467 }
Matt Kraaieb834782002-02-05 15:28:54 +0000468 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000469 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000470 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000471 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000473 if (skip) {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100474 size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
475 if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
Denys Vlasenko1b57fe12013-08-20 12:45:05 +0200476 do {
Nicholas Clark77a66782018-01-25 19:00:19 +0100477 ssize_t n;
478#if ENABLE_FEATURE_DD_IBS_OBS
479 if (G.flags & FLAG_FULLBLOCK)
480 n = full_read(ifd, ibuf, blocksz);
481 else
482#endif
483 n = safe_read(ifd, ibuf, blocksz);
Rob Landley2686d3b2006-05-16 16:52:12 +0000484 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000485 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000486 if (n == 0)
487 break;
Denys Vlasenko1b57fe12013-08-20 12:45:05 +0200488 } while (--skip != 0);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000489 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000490 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000491 if (seek) {
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000492 size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
493 if (lseek(ofd, seek * blocksz, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000494 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000495 }
Eric Andersenddea3682000-11-29 22:33:02 +0000496
Ari Sundholmf22a8382015-02-07 01:41:22 +0100497 while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200498 ssize_t n;
Nicholas Clark77a66782018-01-25 19:00:19 +0100499#if ENABLE_FEATURE_DD_IBS_OBS
500 if (G.flags & FLAG_FULLBLOCK)
501 n = full_read(ifd, ibuf, ibs);
502 else
503#endif
504 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000505 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000506 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000507 if (n < 0) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200508 /* "Bad block" */
Ari Sundholmf22a8382015-02-07 01:41:22 +0100509 if (!(G.flags & FLAG_NOERROR))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000510 goto die_infile;
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000511 bb_simple_perror_msg(infile);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200512 /* GNU dd with conv=noerror skips over bad blocks */
Denys Vlasenko334fcc82009-08-05 23:16:27 +0200513 xlseek(ifd, ibs, SEEK_CUR);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200514 /* conv=noerror,sync writes NULs,
515 * conv=noerror just ignores input bad blocks */
516 n = 0;
Eric Andersenef38b392002-04-27 01:31:43 +0000517 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100518 if (G.flags & FLAG_SWAB) {
Denys Vlasenko0ff0b322013-08-20 12:49:28 +0200519 uint16_t *p16;
520 ssize_t n2;
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200521
522 /* Our code allows only last read to be odd-sized */
523 if (prev_read_size & 1)
524 bb_error_msg_and_die("can't swab %lu byte buffer",
525 (unsigned long)prev_read_size);
526 prev_read_size = n;
527
Denys Vlasenko90e9cfd2013-08-20 12:27:19 +0200528 /* If n is odd, last byte is not swapped:
Denys Vlasenko8395bd32013-08-19 10:30:55 +0200529 * echo -n "qwe" | dd conv=swab
Denys Vlasenkob9413162013-08-19 09:01:39 +0200530 * prints "wqe".
Denys Vlasenkob9413162013-08-19 09:01:39 +0200531 */
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200532 p16 = (void*) ibuf;
Denys Vlasenko0ff0b322013-08-20 12:49:28 +0200533 n2 = (n >> 1);
534 while (--n2 >= 0) {
Denys Vlasenkob9413162013-08-19 09:01:39 +0200535 *p16 = bswap_16(*p16);
536 p16++;
537 }
538 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000539 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000540 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000541 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000542 G.in_part++;
Ari Sundholmf22a8382015-02-07 01:41:22 +0100543 if (G.flags & FLAG_SYNC) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200544 memset(ibuf + n, 0, ibs - n);
Rob Landley2686d3b2006-05-16 16:52:12 +0000545 n = ibs;
546 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000547 }
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100548#if ENABLE_FEATURE_DD_IBS_OBS
Ari Sundholmf22a8382015-02-07 01:41:22 +0100549 if (G.flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000550 char *tmp = ibuf;
551 while (n) {
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100552 size_t d = obs - ocount;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000553 if (d > (size_t)n)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000554 d = n;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100555 memcpy(obuf + ocount, tmp, d);
Rob Landley2686d3b2006-05-16 16:52:12 +0000556 n -= d;
557 tmp += d;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100558 ocount += d;
559 if (ocount == obs) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000560 if (write_and_stats(obuf, obs, obs, outfile))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000561 goto out_status;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100562 ocount = 0;
Rob Landley2686d3b2006-05-16 16:52:12 +0000563 }
564 }
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100565 } else
566#endif
567 {
Denys Vlasenkof3114a32013-08-20 12:40:29 +0200568 if (write_and_stats(ibuf, n, obs, outfile))
569 goto out_status;
570 }
Rostislav Skudnovdba0dc12017-03-16 20:54:35 +0100571 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000572
Rostislav Skudnovdba0dc12017-03-16 20:54:35 +0100573 if (G.flags & FLAG_FSYNC) {
574 if (fsync(ofd) < 0)
575 goto die_outfile;
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000576 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000577
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100578#if ENABLE_FEATURE_DD_IBS_OBS
579 if (ocount != 0) {
580 if (write_and_stats(obuf, ocount, obs, outfile))
Denys Vlasenkof3114a32013-08-20 12:40:29 +0200581 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000582 }
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100583#endif
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000584 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000585 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000586 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000587 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000588
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000589 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000590 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000591 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000592 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000593
594 exitcode = EXIT_SUCCESS;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000595 out_status:
Ari Sundholmf22a8382015-02-07 01:41:22 +0100596 if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
597 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000598
Alexey Solovievf9221542011-03-16 10:39:46 +0300599 if (ENABLE_FEATURE_CLEAN_UP) {
600 free(obuf);
Ari Sundholmf22a8382015-02-07 01:41:22 +0100601 if (G.flags & FLAG_TWOBUFS)
Alexey Solovievf9221542011-03-16 10:39:46 +0300602 free(ibuf);
603 }
604
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000605 return exitcode;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000606}