blob: 8a2eaed72370f54c9db8a387dfa9b9d5af7b1715 [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
Pere Orga34425382011-03-31 14:43:25 +020011//usage:#define dd_trivial_usage
12//usage: "[if=FILE] [of=FILE] " IF_FEATURE_DD_IBS_OBS("[ibs=N] [obs=N] ") "[bs=N] [count=N] [skip=N]\n"
13//usage: " [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync]")
14//usage:#define dd_full_usage "\n\n"
15//usage: "Copy a file with converting and formatting\n"
16//usage: "\nOptions:"
17//usage: "\n if=FILE Read from FILE instead of stdin"
18//usage: "\n of=FILE Write to FILE instead of stdout"
19//usage: "\n bs=N Read and write N bytes at a time"
20//usage: IF_FEATURE_DD_IBS_OBS(
21//usage: "\n ibs=N Read N bytes at a time"
22//usage: )
23//usage: IF_FEATURE_DD_IBS_OBS(
24//usage: "\n obs=N Write N bytes at a time"
25//usage: )
26//usage: "\n count=N Copy only N input blocks"
27//usage: "\n skip=N Skip N input blocks"
28//usage: "\n seek=N Skip N output blocks"
29//usage: IF_FEATURE_DD_IBS_OBS(
30//usage: "\n conv=notrunc Don't truncate output file"
31//usage: "\n conv=noerror Continue after read errors"
32//usage: "\n conv=sync Pad blocks with zeros"
33//usage: "\n conv=fsync Physically write data out before finishing"
34//usage: )
35//usage: "\n"
36//usage: "\nNumbers may be suffixed by c (x1), w (x2), b (x512), kD (x1000), k (x1024),"
37//usage: "\nMD (x1000000), M (x1048576), GD (x1000000000) or G (x1073741824)"
38//usage:
39//usage:#define dd_example_usage
40//usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
41//usage: "4+0 records in\n"
42//usage: "4+0 records out\n"
43
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000044#include "libbb.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +000045
46/* This is a NOEXEC applet. Be very careful! */
47
Eric Andersencbe31da2001-02-20 06:14:08 +000048
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000049enum {
50 ifd = STDIN_FILENO,
51 ofd = STDOUT_FILENO,
52};
53
Matt Kraaia164c642001-02-05 17:50:03 +000054static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000055 { "c", 1 },
56 { "w", 2 },
57 { "b", 512 },
58 { "kD", 1000 },
59 { "k", 1024 },
Denys Vlasenkofb132e42010-10-29 11:46:52 +020060 { "K", 1024 }, /* compat with coreutils dd */
Matt Kraai24ac0172000-12-18 21:38:57 +000061 { "MD", 1000000 },
62 { "M", 1048576 },
63 { "GD", 1000000000 },
64 { "G", 1073741824 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020065 { "", 0 }
Matt Kraai24ac0172000-12-18 21:38:57 +000066};
67
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000068struct globals {
69 off_t out_full, out_part, in_full, in_part;
Gabor Heja4e5b07b2009-10-14 00:29:28 +020070#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
71 unsigned long long total_bytes;
72 unsigned long long begin_time_us;
73#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010074} FIX_ALIASING;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000075#define G (*(struct globals*)&bb_common_bufsiz1)
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +020076#define INIT_G() do { \
77 /* we have to zero it out because of NOEXEC */ \
78 memset(&G, 0, sizeof(G)); \
79} while (0)
Denis Vlasenko75878702007-07-27 15:02:00 +000080
Rob Landleyc5598172006-05-02 22:44:04 +000081
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000082static void dd_output_status(int UNUSED_PARAM cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +000083{
Gabor Heja4e5b07b2009-10-14 00:29:28 +020084#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
Denys Vlasenko7eabffa2009-10-14 17:43:41 +020085 double seconds;
86 unsigned long long bytes_sec;
87 unsigned long long now_us = monotonic_us(); /* before fprintf */
Gabor Heja4e5b07b2009-10-14 00:29:28 +020088#endif
89
Denis Vlasenko980864d2007-07-30 10:58:09 +000090 /* Deliberately using %u, not %d */
91 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
92 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000093 G.in_full, G.in_part,
94 G.out_full, G.out_part);
Gabor Heja4e5b07b2009-10-14 00:29:28 +020095
96#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
97 fprintf(stderr, "%llu bytes (%sB) copied, ",
98 G.total_bytes,
99 /* show fractional digit, use suffixes */
100 make_human_readable_str(G.total_bytes, 1, 0)
101 );
102 /* Corner cases:
103 * ./busybox dd </dev/null >/dev/null
104 * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
105 * (echo DONE) | ./busybox dd >/dev/null
106 * (sleep 1; echo DONE) | ./busybox dd >/dev/null
107 */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200108 seconds = (now_us - G.begin_time_us) / 1000000.0;
109 bytes_sec = G.total_bytes / seconds;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200110 fprintf(stderr, "%f seconds, %sB/s\n",
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200111 seconds,
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200112 /* show fractional digit, use suffixes */
Denys Vlasenko7eabffa2009-10-14 17:43:41 +0200113 make_human_readable_str(bytes_sec, 1, 0)
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200114 );
115#endif
Rob Landleyc5598172006-05-02 22:44:04 +0000116}
117
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000118static ssize_t full_write_or_warn(const void *buf, size_t len,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000119 const char *const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000120{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000121 ssize_t n = full_write(ofd, buf, len);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000122 if (n < 0)
123 bb_perror_msg("writing '%s'", filename);
124 return n;
125}
126
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000127static bool write_and_stats(const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000128 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000129{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000130 ssize_t n = full_write_or_warn(buf, len, filename);
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000131 if (n < 0)
132 return 1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000133 if ((size_t)n == obs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000134 G.out_full++;
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000135 else if (n) /* > 0 */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000136 G.out_part++;
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200137#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
138 G.total_bytes += n;
139#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000140 return 0;
141}
142
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000143#if ENABLE_LFS
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200144# define XATOU_SFX xatoull_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000145#else
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200146# define XATOU_SFX xatoul_sfx
Denis Vlasenko5dd27b12006-11-25 14:46:21 +0000147#endif
148
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000149int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000150int dd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +0000151{
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000152 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000153 /* Must be in the same order as OP_conv_XXX! */
154 /* (see "flags |= (1 << what)" below) */
155 FLAG_NOTRUNC = 1 << 0,
156 FLAG_SYNC = 1 << 1,
157 FLAG_NOERROR = 1 << 2,
158 FLAG_FSYNC = 1 << 3,
159 /* end of conv flags */
160 FLAG_TWOBUFS = 1 << 4,
161 FLAG_COUNT = 1 << 5,
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000162 };
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000163 static const char keywords[] ALIGN1 =
Denis Vlasenko9d4a17e2008-04-23 05:44:59 +0000164 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000165#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko9d4a17e2008-04-23 05:44:59 +0000166 "ibs\0""obs\0""conv\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000167#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000168 ;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000169#if ENABLE_FEATURE_DD_IBS_OBS
170 static const char conv_words[] ALIGN1 =
171 "notrunc\0""sync\0""noerror\0""fsync\0";
172#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000173 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000174 OP_bs = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000175 OP_count,
176 OP_seek,
177 OP_skip,
178 OP_if,
179 OP_of,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000180#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000181 OP_ibs,
182 OP_obs,
183 OP_conv,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000184 /* Must be in the same order as FLAG_XXX! */
185 OP_conv_notrunc = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000186 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000187 OP_conv_noerror,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000188 OP_conv_fsync,
189 /* Unimplemented conv=XXX: */
190 //nocreat do not create the output file
191 //excl fail if the output file already exists
192 //fdatasync physically write output file data before finishing
193 //swab swap every pair of input bytes
194 //lcase change upper case to lower case
195 //ucase change lower case to upper case
196 //block pad newline-terminated records with spaces to cbs-size
197 //unblock replace trailing spaces in cbs-size records with newline
198 //ascii from EBCDIC to ASCII
199 //ebcdic from ASCII to EBCDIC
200 //ibm from ASCII to alternate EBCDIC
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000201#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000202 };
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000203 int exitcode = EXIT_FAILURE;
Denis Vlasenko75878702007-07-27 15:02:00 +0000204 size_t ibs = 512, obs = 512;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000205 ssize_t n, w;
Rob Landley2686d3b2006-05-16 16:52:12 +0000206 char *ibuf, *obuf;
Denis Vlasenko75878702007-07-27 15:02:00 +0000207 /* And these are all zeroed at once! */
208 struct {
209 int flags;
Denis Vlasenko75878702007-07-27 15:02:00 +0000210 size_t oc;
211 off_t count;
212 off_t seek, skip;
213 const char *infile, *outfile;
Denis Vlasenko75878702007-07-27 15:02:00 +0000214 } Z;
215#define flags (Z.flags )
Denis Vlasenko75878702007-07-27 15:02:00 +0000216#define oc (Z.oc )
217#define count (Z.count )
218#define seek (Z.seek )
219#define skip (Z.skip )
220#define infile (Z.infile )
221#define outfile (Z.outfile)
Eric Andersenddea3682000-11-29 22:33:02 +0000222
Denis Vlasenko75878702007-07-27 15:02:00 +0000223 memset(&Z, 0, sizeof(Z));
224 INIT_G();
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100225 //fflush_all(); - is this needed because of NOEXEC?
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000226
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000227 for (n = 1; argv[n]; n++) {
228 int what;
229 char *val;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000230 char *arg = argv[n];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000231
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000232#if ENABLE_DESKTOP
233 /* "dd --". NB: coreutils 6.9 will complain if they see
234 * more than one of them. We wouldn't. */
235 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
236 continue;
237#endif
238 val = strchr(arg, '=');
239 if (val == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000241 *val = '\0';
242 what = index_in_strings(keywords, arg);
243 if (what < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000244 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000245 /* *val = '='; - to preserve ps listing? */
246 val++;
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000247#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000248 if (what == OP_ibs) {
249 /* Must fit into positive ssize_t */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000250 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
251 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000252 }
253 if (what == OP_obs) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000254 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
255 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000256 }
257 if (what == OP_conv) {
258 while (1) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000259 /* find ',', replace them with NUL so we can use val for
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000260 * index_in_strings() without copying.
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000261 * We rely on val being non-null, else strchr would fault.
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000262 */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000263 arg = strchr(val, ',');
264 if (arg)
265 *arg = '\0';
266 what = index_in_strings(conv_words, val);
267 if (what < 0)
268 bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv");
269 flags |= (1 << what);
270 if (!arg) /* no ',' left, so this was the last specifier */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000271 break;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000272 /* *arg = ','; - to preserve ps listing? */
273 val = arg + 1; /* skip this keyword and ',' */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000274 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000275 continue; /* we trashed 'what', can't fall through */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000276 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000277#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000278 if (what == OP_bs) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000279 ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
280 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000281 }
282 /* These can be large: */
283 if (what == OP_count) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000284 flags |= FLAG_COUNT;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000285 count = XATOU_SFX(val, dd_suffixes);
286 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000287 }
288 if (what == OP_seek) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000289 seek = XATOU_SFX(val, dd_suffixes);
290 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000291 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000292 if (what == OP_skip) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000293 skip = XATOU_SFX(val, dd_suffixes);
294 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000295 }
296 if (what == OP_if) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000297 infile = val;
298 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000299 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000300 if (what == OP_of) {
301 outfile = val;
302 /*continue;*/
303 }
304 } /* end of "for (argv[n])" */
305
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000306//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000307 ibuf = obuf = xmalloc(ibs);
308 if (ibs != obs) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000309 flags |= FLAG_TWOBUFS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000310 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000311 }
Gabor Heja4e5b07b2009-10-14 00:29:28 +0200312
313#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
314 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
315#endif
316#if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
317 G.begin_time_us = monotonic_us();
318#endif
319
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000320 if (infile != NULL)
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000321 xmove_fd(xopen(infile, O_RDONLY), ifd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000322 else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000323 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000324 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000325 if (outfile != NULL) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000326 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000327
Denis Vlasenko75878702007-07-27 15:02:00 +0000328 if (!seek && !(flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000329 oflag |= O_TRUNC;
330
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000331 xmove_fd(xopen(outfile, oflag), ofd);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000332
Denis Vlasenko75878702007-07-27 15:02:00 +0000333 if (seek && !(flags & FLAG_NOTRUNC)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000334 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000335 struct stat st;
336
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100337 if (fstat(ofd, &st) < 0
338 || S_ISREG(st.st_mode)
339 || S_ISDIR(st.st_mode)
340 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000341 goto die_outfile;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100342 }
Matt Kraaieb834782002-02-05 15:28:54 +0000343 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000344 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000345 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000346 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000347 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000348 if (skip) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000349 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000350 while (skip-- > 0) {
351 n = safe_read(ifd, ibuf, ibs);
352 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000353 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000354 if (n == 0)
355 break;
356 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000357 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000358 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000359 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000360 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000361 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000362 }
Eric Andersenddea3682000-11-29 22:33:02 +0000363
Denis Vlasenko2b03e5e2007-07-29 14:59:06 +0000364 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000365 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000366 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000367 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000368 if (n < 0) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200369 /* "Bad block" */
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000370 if (!(flags & FLAG_NOERROR))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000371 goto die_infile;
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000372 bb_simple_perror_msg(infile);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200373 /* GNU dd with conv=noerror skips over bad blocks */
Denys Vlasenko334fcc82009-08-05 23:16:27 +0200374 xlseek(ifd, ibs, SEEK_CUR);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200375 /* conv=noerror,sync writes NULs,
376 * conv=noerror just ignores input bad blocks */
377 n = 0;
Eric Andersenef38b392002-04-27 01:31:43 +0000378 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000379 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000380 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000381 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000382 G.in_part++;
Denis Vlasenko75878702007-07-27 15:02:00 +0000383 if (flags & FLAG_SYNC) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200384 memset(ibuf + n, 0, ibs - n);
Rob Landley2686d3b2006-05-16 16:52:12 +0000385 n = ibs;
386 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000387 }
Denis Vlasenko75878702007-07-27 15:02:00 +0000388 if (flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000389 char *tmp = ibuf;
390 while (n) {
391 size_t d = obs - oc;
392
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000393 if (d > (size_t)n)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000394 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000395 memcpy(obuf + oc, tmp, d);
396 n -= d;
397 tmp += d;
398 oc += d;
399 if (oc == obs) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000400 if (write_and_stats(obuf, obs, obs, outfile))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000401 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000402 oc = 0;
403 }
404 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000405 } else if (write_and_stats(ibuf, n, obs, outfile))
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000406 goto out_status;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000407
408 if (flags & FLAG_FSYNC) {
409 if (fsync(ofd) < 0)
410 goto die_outfile;
411 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000412 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000413
Rob Landley2686d3b2006-05-16 16:52:12 +0000414 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000415 w = full_write_or_warn(obuf, oc, outfile);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000416 if (w < 0) goto out_status;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000417 if (w > 0) G.out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000418 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000419 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000420 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000421 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000422 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000423
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000424 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000425 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000426 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000427 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000428
429 exitcode = EXIT_SUCCESS;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000430 out_status:
Rob Landleyc5598172006-05-02 22:44:04 +0000431 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000432
Alexey Solovievf9221542011-03-16 10:39:46 +0300433 if (ENABLE_FEATURE_CLEAN_UP) {
434 free(obuf);
435 if (flags & FLAG_TWOBUFS)
436 free(ibuf);
437 }
438
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000439 return exitcode;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000440}