blob: b5f3cbec5d4aa4297a97f18d9dc5fd99dabf073c [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 Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "dd (7.5 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"
Rostislav Skudnovd712edc2019-02-06 11:57:15 +000062//usage: " [iflag=skip_bytes|fullblock] [oflag=seek_bytes|append]"
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +010063//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"
Rostislav Skudnovd712edc2019-02-06 11:57:15 +000087//usage: "\n oflag=append Open output file in append mode"
Pere Orga34425382011-03-31 14:43:25 +020088//usage: )
Ari Sundholmf22a8382015-02-07 01:41:22 +010089//usage: IF_FEATURE_DD_STATUS(
90//usage: "\n status=noxfer Suppress rate output"
91//usage: "\n status=none Suppress all output"
92//usage: )
Pere Orga34425382011-03-31 14:43:25 +020093//usage: "\n"
Ari Sundholmf22a8382015-02-07 01:41:22 +010094//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 +020095//usage:
96//usage:#define dd_example_usage
97//usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
98//usage: "4+0 records in\n"
99//usage: "4+0 records out\n"
100
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000101#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200102#include "common_bufsiz.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000103
104/* This is a NOEXEC applet. Be very careful! */
105
Eric Andersencbe31da2001-02-20 06:14:08 +0000106
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000107enum {
108 ifd = STDIN_FILENO,
109 ofd = STDOUT_FILENO,
110};
111
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000112struct globals {
113 off_t out_full, out_part, in_full, in_part;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200114#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
115 unsigned long long total_bytes;
116 unsigned long long begin_time_us;
117#endif
Ari Sundholmf22a8382015-02-07 01:41:22 +0100118 int flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100119} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200120#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200121#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200122 setup_common_bufsiz(); \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200123 /* we have to zero it out because of NOEXEC */ \
124 memset(&G, 0, sizeof(G)); \
125} while (0)
Denis Vlasenko75878702007-07-27 15:02:00 +0000126
Ari Sundholmf22a8382015-02-07 01:41:22 +0100127enum {
128 /* Must be in the same order as OP_conv_XXX! */
129 /* (see "flags |= (1 << what)" below) */
130 FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
131 FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
132 FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
133 FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
134 FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
135 /* end of conv flags */
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100136 /* start of input flags */
137 FLAG_IFLAG_SHIFT = 5,
138 FLAG_SKIP_BYTES = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
Nicholas Clark77a66782018-01-25 19:00:19 +0100139 FLAG_FULLBLOCK = (1 << 6) * ENABLE_FEATURE_DD_IBS_OBS,
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100140 /* end of input flags */
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000141 /* start of output flags */
142 FLAG_OFLAG_SHIFT = 7,
143 FLAG_SEEK_BYTES = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
Rostislav Skudnovd712edc2019-02-06 11:57:15 +0000144 FLAG_APPEND = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000145 /* end of output flags */
Rostislav Skudnovd712edc2019-02-06 11:57:15 +0000146 FLAG_TWOBUFS = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
147 FLAG_COUNT = 1 << 10,
148 FLAG_STATUS_NONE = 1 << 11,
149 FLAG_STATUS_NOXFER = 1 << 12,
Ari Sundholmf22a8382015-02-07 01:41:22 +0100150};
Rob Landleyc5598172006-05-02 22:44:04 +0000151
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000152static void dd_output_status(int UNUSED_PARAM cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +0000153{
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200154#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200155 double seconds;
156 unsigned long long bytes_sec;
157 unsigned long long now_us = monotonic_us(); /* before fprintf */
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200158#endif
159
Denis Vlasenko980864d2007-07-30 10:58:09 +0000160 /* Deliberately using %u, not %d */
161 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
162 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000163 G.in_full, G.in_part,
164 G.out_full, G.out_part);
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200165
166#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Ari Sundholmf22a8382015-02-07 01:41:22 +0100167# if ENABLE_FEATURE_DD_STATUS
168 if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */
169 return;
170 //TODO: should status=none make dd stop reacting to USR1 entirely?
171 //So far we react to it (we print the stats),
172 //status=none only suppresses final, non-USR1 generated status message.
173# endif
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200174 fprintf(stderr, "%llu bytes (%sB) copied, ",
175 G.total_bytes,
176 /* show fractional digit, use suffixes */
177 make_human_readable_str(G.total_bytes, 1, 0)
178 );
179 /* Corner cases:
180 * ./busybox dd </dev/null >/dev/null
181 * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
182 * (echo DONE) | ./busybox dd >/dev/null
183 * (sleep 1; echo DONE) | ./busybox dd >/dev/null
184 */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200185 seconds = (now_us - G.begin_time_us) / 1000000.0;
186 bytes_sec = G.total_bytes / seconds;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200187 fprintf(stderr, "%f seconds, %sB/s\n",
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200188 seconds,
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200189 /* show fractional digit, use suffixes */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200190 make_human_readable_str(bytes_sec, 1, 0)
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200191 );
192#endif
Rob Landleyc5598172006-05-02 22:44:04 +0000193}
194
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000195static bool write_and_stats(const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000196 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000197{
Denys Vlasenko875ce092019-05-14 17:46:18 +0200198 ssize_t n;
199
200 n = full_write(ofd, buf, len);
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200201#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Denys Vlasenko875ce092019-05-14 17:46:18 +0200202 if (n > 0)
203 G.total_bytes += n;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200204#endif
Denys Vlasenko5afd63a2018-01-10 10:58:55 +0100205 if ((size_t)n == obs) {
206 G.out_full++;
207 return 0;
208 }
Denys Vlasenko2c876772018-01-10 11:04:09 +0100209 if ((size_t)n == len) {
Denys Vlasenko5afd63a2018-01-10 10:58:55 +0100210 G.out_part++;
Denys Vlasenko2c876772018-01-10 11:04:09 +0100211 return 0;
212 }
Denys Vlasenko875ce092019-05-14 17:46:18 +0200213 /* n is < len (and possibly is -1).
214 * Even if n >= 0, errno is usually set correctly.
215 * For example, if writing to block device and getting ENOSPC,
216 * full_write() first sees a short write, then tries to write
217 * the remainder and gets errno set to ENOSPC.
218 * It returns n > 0 (the amount which it did write).
219 */
220 bb_perror_msg("error writing '%s'", filename);
Denys Vlasenko5afd63a2018-01-10 10:58:55 +0100221 return 1;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000222}
223
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000224#if ENABLE_LFS
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200225# define XATOU_SFX xatoull_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000226#else
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200227# define XATOU_SFX xatoul_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000228#endif
229
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100230#if ENABLE_FEATURE_DD_IBS_OBS
231static int parse_comma_flags(char *val, const char *words, const char *error_in)
232{
233 int flags = 0;
234 while (1) {
235 int n;
236 char *arg;
237 /* find ',', replace them with NUL so we can use val for
238 * index_in_strings() without copying.
239 * We rely on val being non-null, else strchr would fault.
240 */
241 arg = strchr(val, ',');
242 if (arg)
243 *arg = '\0';
244 n = index_in_strings(words, val);
245 if (n < 0)
246 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, error_in);
247 flags |= (1 << n);
248 if (!arg) /* no ',' left, so this was the last specifier */
249 break;
250 *arg = ','; /* to preserve ps listing */
251 val = arg + 1; /* skip this keyword and ',' */
252 }
253 return flags;
254}
255#endif
256
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000257int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000258int dd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000259{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000260 static const char keywords[] ALIGN1 =
Ari Sundholmf22a8382015-02-07 01:41:22 +0100261 "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 +0000262#if ENABLE_FEATURE_DD_IBS_OBS
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000263 "ibs\0""obs\0""conv\0""iflag\0""oflag\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000264#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000265 ;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000266#if ENABLE_FEATURE_DD_IBS_OBS
267 static const char conv_words[] ALIGN1 =
Denys Vlasenkob9413162013-08-19 09:01:39 +0200268 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100269 static const char iflag_words[] ALIGN1 =
Nicholas Clark77a66782018-01-25 19:00:19 +0100270 "skip_bytes\0""fullblock\0";
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000271 static const char oflag_words[] ALIGN1 =
Rostislav Skudnovd712edc2019-02-06 11:57:15 +0000272 "seek_bytes\0append\0";
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000273#endif
Ari Sundholmf22a8382015-02-07 01:41:22 +0100274#if ENABLE_FEATURE_DD_STATUS
275 static const char status_words[] ALIGN1 =
276 "none\0""noxfer\0";
277#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000278 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000279 OP_bs = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000280 OP_count,
281 OP_seek,
282 OP_skip,
283 OP_if,
284 OP_of,
Ari Sundholmf22a8382015-02-07 01:41:22 +0100285 IF_FEATURE_DD_STATUS(OP_status,)
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000286#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000287 OP_ibs,
288 OP_obs,
289 OP_conv,
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100290 OP_iflag,
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000291 OP_oflag,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000292 /* Must be in the same order as FLAG_XXX! */
293 OP_conv_notrunc = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000294 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000295 OP_conv_noerror,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000296 OP_conv_fsync,
Denys Vlasenkob9413162013-08-19 09:01:39 +0200297 OP_conv_swab,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000298 /* Unimplemented conv=XXX: */
299 //nocreat do not create the output file
300 //excl fail if the output file already exists
301 //fdatasync physically write output file data before finishing
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000302 //lcase change upper case to lower case
303 //ucase change lower case to upper case
304 //block pad newline-terminated records with spaces to cbs-size
305 //unblock replace trailing spaces in cbs-size records with newline
306 //ascii from EBCDIC to ASCII
307 //ebcdic from ASCII to EBCDIC
308 //ibm from ASCII to alternate EBCDIC
Denys Vlasenkob9413162013-08-19 09:01:39 +0200309 /* Partially implemented: */
310 //swab swap every pair of input bytes: will abort on non-even reads
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100311 OP_iflag_skip_bytes,
Nicholas Clark77a66782018-01-25 19:00:19 +0100312 OP_iflag_fullblock,
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000313 OP_oflag_seek_bytes,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000314#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000315 };
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200316 smallint exitcode = EXIT_FAILURE;
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200317 int i;
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200318 size_t ibs = 512;
319 char *ibuf;
320#if ENABLE_FEATURE_DD_IBS_OBS
321 size_t obs = 512;
322 char *obuf;
323#else
324# define obs ibs
325# define obuf ibuf
326#endif
327 /* These are all zeroed at once! */
Denis Vlasenko75878702007-07-27 15:02:00 +0000328 struct {
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100329 IF_FEATURE_DD_IBS_OBS(size_t ocount;)
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200330 ssize_t prev_read_size; /* for detecting swab failure */
Denis Vlasenko75878702007-07-27 15:02:00 +0000331 off_t count;
332 off_t seek, skip;
333 const char *infile, *outfile;
Denis Vlasenko75878702007-07-27 15:02:00 +0000334 } Z;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100335#define ocount (Z.ocount )
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200336#define prev_read_size (Z.prev_read_size)
Denis Vlasenko75878702007-07-27 15:02:00 +0000337#define count (Z.count )
338#define seek (Z.seek )
339#define skip (Z.skip )
340#define infile (Z.infile )
341#define outfile (Z.outfile)
Eric Andersenddea3682000-11-29 22:33:02 +0000342
Denis Vlasenko75878702007-07-27 15:02:00 +0000343 memset(&Z, 0, sizeof(Z));
344 INIT_G();
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100345 //fflush_all(); - is this needed because of NOEXEC?
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000346
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200347 for (i = 1; argv[i]; i++) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000348 int what;
349 char *val;
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200350 char *arg = argv[i];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000351
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000352#if ENABLE_DESKTOP
353 /* "dd --". NB: coreutils 6.9 will complain if they see
354 * more than one of them. We wouldn't. */
355 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
356 continue;
357#endif
358 val = strchr(arg, '=');
359 if (val == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000360 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000361 *val = '\0';
362 what = index_in_strings(keywords, arg);
363 if (what < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000364 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000365 /* *val = '='; - to preserve ps listing? */
366 val++;
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000367#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000368 if (what == OP_ibs) {
369 /* Must fit into positive ssize_t */
Ari Sundholm88930232015-03-04 18:46:48 +0200370 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000371 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000372 }
373 if (what == OP_obs) {
Ari Sundholm88930232015-03-04 18:46:48 +0200374 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000375 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000376 }
377 if (what == OP_conv) {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100378 G.flags |= parse_comma_flags(val, conv_words, "conv");
379 /*continue;*/
380 }
381 if (what == OP_iflag) {
382 G.flags |= parse_comma_flags(val, iflag_words, "iflag") << FLAG_IFLAG_SHIFT;
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200383 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000384 }
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000385 if (what == OP_oflag) {
386 G.flags |= parse_comma_flags(val, oflag_words, "oflag") << FLAG_OFLAG_SHIFT;
387 /*continue;*/
388 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000389#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000390 if (what == OP_bs) {
Ari Sundholm88930232015-03-04 18:46:48 +0200391 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200392 obs = ibs;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000393 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000394 }
395 /* These can be large: */
396 if (what == OP_count) {
Ari Sundholmf22a8382015-02-07 01:41:22 +0100397 G.flags |= FLAG_COUNT;
Ari Sundholm88930232015-03-04 18:46:48 +0200398 count = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000399 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000400 }
401 if (what == OP_seek) {
Ari Sundholm88930232015-03-04 18:46:48 +0200402 seek = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000403 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000404 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000405 if (what == OP_skip) {
Ari Sundholm88930232015-03-04 18:46:48 +0200406 skip = XATOU_SFX(val, cwbkMG_suffixes);
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000407 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000408 }
409 if (what == OP_if) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000410 infile = val;
411 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000412 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000413 if (what == OP_of) {
414 outfile = val;
415 /*continue;*/
416 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100417#if ENABLE_FEATURE_DD_STATUS
418 if (what == OP_status) {
419 int n;
420 n = index_in_strings(status_words, val);
421 if (n < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200422 bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status");
Denys Vlasenkod5b98e22017-07-14 13:44:30 +0200423 G.flags |= FLAG_STATUS_NONE << n;
Ari Sundholmf22a8382015-02-07 01:41:22 +0100424 /*continue;*/
425 }
426#endif
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200427 } /* end of "for (argv[i])" */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000428
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000429//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200430 ibuf = xmalloc(ibs);
431 obuf = ibuf;
432#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000433 if (ibs != obs) {
Ari Sundholmf22a8382015-02-07 01:41:22 +0100434 G.flags |= FLAG_TWOBUFS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000435 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000436 }
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200437#endif
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200438
439#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
440 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
441#endif
442#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
443 G.begin_time_us = monotonic_us();
444#endif
445
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200446 if (infile) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000447 xmove_fd(xopen(infile, O_RDONLY), ifd);
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200448 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000449 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 }
Denys Vlasenko4502bb12013-08-20 12:08:46 +0200451 if (outfile) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000452 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000453
Ari Sundholmf22a8382015-02-07 01:41:22 +0100454 if (!seek && !(G.flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000455 oflag |= O_TRUNC;
Rostislav Skudnovd712edc2019-02-06 11:57:15 +0000456 if (G.flags & FLAG_APPEND)
457 oflag |= O_APPEND;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000458
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000459 xmove_fd(xopen(outfile, oflag), ofd);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000460
Ari Sundholmf22a8382015-02-07 01:41:22 +0100461 if (seek && !(G.flags & FLAG_NOTRUNC)) {
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000462 size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
463 if (ftruncate(ofd, seek * blocksz) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000464 struct stat st;
465
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100466 if (fstat(ofd, &st) < 0
467 || S_ISREG(st.st_mode)
468 || S_ISDIR(st.st_mode)
469 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000470 goto die_outfile;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100471 }
Matt Kraaieb834782002-02-05 15:28:54 +0000472 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000473 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000474 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000475 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000476 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000477 if (skip) {
Rafał Miłeckic30a5b12016-02-01 02:17:28 +0100478 size_t blocksz = (G.flags & FLAG_SKIP_BYTES) ? 1 : ibs;
479 if (lseek(ifd, skip * blocksz, SEEK_CUR) < 0) {
Denys Vlasenko1b57fe12013-08-20 12:45:05 +0200480 do {
Nicholas Clark77a66782018-01-25 19:00:19 +0100481 ssize_t n;
482#if ENABLE_FEATURE_DD_IBS_OBS
483 if (G.flags & FLAG_FULLBLOCK)
484 n = full_read(ifd, ibuf, blocksz);
485 else
486#endif
487 n = safe_read(ifd, ibuf, blocksz);
Rob Landley2686d3b2006-05-16 16:52:12 +0000488 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000489 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000490 if (n == 0)
491 break;
Denys Vlasenko1b57fe12013-08-20 12:45:05 +0200492 } while (--skip != 0);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000493 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000494 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000495 if (seek) {
Rostislav Skudnov3f6a9782018-10-15 10:26:15 +0000496 size_t blocksz = (G.flags & FLAG_SEEK_BYTES) ? 1 : obs;
497 if (lseek(ofd, seek * blocksz, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000498 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000499 }
Eric Andersenddea3682000-11-29 22:33:02 +0000500
Ari Sundholmf22a8382015-02-07 01:41:22 +0100501 while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Denys Vlasenko4b1896c2013-08-19 09:00:08 +0200502 ssize_t n;
Nicholas Clark77a66782018-01-25 19:00:19 +0100503#if ENABLE_FEATURE_DD_IBS_OBS
504 if (G.flags & FLAG_FULLBLOCK)
505 n = full_read(ifd, ibuf, ibs);
506 else
507#endif
508 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000509 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000510 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000511 if (n < 0) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200512 /* "Bad block" */
Ari Sundholmf22a8382015-02-07 01:41:22 +0100513 if (!(G.flags & FLAG_NOERROR))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000514 goto die_infile;
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000515 bb_simple_perror_msg(infile);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200516 /* GNU dd with conv=noerror skips over bad blocks */
Denys Vlasenko334fcc82009-08-05 23:16:27 +0200517 xlseek(ifd, ibs, SEEK_CUR);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200518 /* conv=noerror,sync writes NULs,
519 * conv=noerror just ignores input bad blocks */
520 n = 0;
Eric Andersenef38b392002-04-27 01:31:43 +0000521 }
Ari Sundholmf22a8382015-02-07 01:41:22 +0100522 if (G.flags & FLAG_SWAB) {
Denys Vlasenko0ff0b322013-08-20 12:49:28 +0200523 uint16_t *p16;
524 ssize_t n2;
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200525
526 /* Our code allows only last read to be odd-sized */
527 if (prev_read_size & 1)
528 bb_error_msg_and_die("can't swab %lu byte buffer",
529 (unsigned long)prev_read_size);
530 prev_read_size = n;
531
Denys Vlasenko90e9cfd2013-08-20 12:27:19 +0200532 /* If n is odd, last byte is not swapped:
Denys Vlasenko8395bd32013-08-19 10:30:55 +0200533 * echo -n "qwe" | dd conv=swab
Denys Vlasenkob9413162013-08-19 09:01:39 +0200534 * prints "wqe".
Denys Vlasenkob9413162013-08-19 09:01:39 +0200535 */
Denys Vlasenko5b9910f2013-08-20 02:50:49 +0200536 p16 = (void*) ibuf;
Denys Vlasenko0ff0b322013-08-20 12:49:28 +0200537 n2 = (n >> 1);
538 while (--n2 >= 0) {
Denys Vlasenkob9413162013-08-19 09:01:39 +0200539 *p16 = bswap_16(*p16);
540 p16++;
541 }
542 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000543 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000544 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000545 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000546 G.in_part++;
Ari Sundholmf22a8382015-02-07 01:41:22 +0100547 if (G.flags & FLAG_SYNC) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200548 memset(ibuf + n, 0, ibs - n);
Rob Landley2686d3b2006-05-16 16:52:12 +0000549 n = ibs;
550 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000551 }
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100552#if ENABLE_FEATURE_DD_IBS_OBS
Ari Sundholmf22a8382015-02-07 01:41:22 +0100553 if (G.flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000554 char *tmp = ibuf;
555 while (n) {
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100556 size_t d = obs - ocount;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000557 if (d > (size_t)n)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000558 d = n;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100559 memcpy(obuf + ocount, tmp, d);
Rob Landley2686d3b2006-05-16 16:52:12 +0000560 n -= d;
561 tmp += d;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100562 ocount += d;
563 if (ocount == obs) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000564 if (write_and_stats(obuf, obs, obs, outfile))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000565 goto out_status;
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100566 ocount = 0;
Rob Landley2686d3b2006-05-16 16:52:12 +0000567 }
568 }
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100569 } else
570#endif
571 {
Denys Vlasenkof3114a32013-08-20 12:40:29 +0200572 if (write_and_stats(ibuf, n, obs, outfile))
573 goto out_status;
574 }
Rostislav Skudnovdba0dc12017-03-16 20:54:35 +0100575 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000576
Rostislav Skudnovdba0dc12017-03-16 20:54:35 +0100577 if (G.flags & FLAG_FSYNC) {
578 if (fsync(ofd) < 0)
579 goto die_outfile;
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000580 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000581
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100582#if ENABLE_FEATURE_DD_IBS_OBS
583 if (ocount != 0) {
584 if (write_and_stats(obuf, ocount, obs, outfile))
Denys Vlasenkof3114a32013-08-20 12:40:29 +0200585 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000586 }
Denys Vlasenko9ab5a8d2018-10-30 17:04:52 +0100587#endif
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000588 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000589 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000590 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000591 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000592
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000593 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000594 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000595 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000596 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000597
598 exitcode = EXIT_SUCCESS;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000599 out_status:
Ari Sundholmf22a8382015-02-07 01:41:22 +0100600 if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE))
601 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000602
Alexey Solovievf9221542011-03-16 10:39:46 +0300603 if (ENABLE_FEATURE_CLEAN_UP) {
604 free(obuf);
Ari Sundholmf22a8382015-02-07 01:41:22 +0100605 if (G.flags & FLAG_TWOBUFS)
Alexey Solovievf9221542011-03-16 10:39:46 +0300606 free(ibuf);
607 }
608
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000609 return exitcode;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000610}