1 /* vi: set sw=4 ts=4: */
3 * Mini dd implementation for busybox
6 * Copyright (C) 2000,2001 Matt Kraai
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 //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: "\n if=FILE Read from FILE instead of stdin"
17 //usage: "\n of=FILE Write to FILE instead of stdout"
18 //usage: "\n bs=N Read and write N bytes at a time"
19 //usage: IF_FEATURE_DD_IBS_OBS(
20 //usage: "\n ibs=N Read N bytes at a time"
22 //usage: IF_FEATURE_DD_IBS_OBS(
23 //usage: "\n obs=N Write N bytes at a time"
25 //usage: "\n count=N Copy only N input blocks"
26 //usage: "\n skip=N Skip N input blocks"
27 //usage: "\n seek=N Skip N output blocks"
28 //usage: IF_FEATURE_DD_IBS_OBS(
29 //usage: "\n conv=notrunc Don't truncate output file"
30 //usage: "\n conv=noerror Continue after read errors"
31 //usage: "\n conv=sync Pad blocks with zeros"
32 //usage: "\n conv=fsync Physically write data out before finishing"
33 //usage: "\n conv=swab Swap every pair of bytes"
36 //usage: "\nN may be suffixed by c (1), w (2), b (512), kD (1000), k (1024), MD, M, GD, G"
38 //usage:#define dd_example_usage
39 //usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n"
40 //usage: "4+0 records in\n"
41 //usage: "4+0 records out\n"
45 /* This is a NOEXEC applet. Be very careful! */
53 static const struct suffix_mult dd_suffixes[] = {
59 { "K", 1024 }, /* compat with coreutils dd */
68 off_t out_full, out_part, in_full, in_part;
69 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
70 unsigned long long total_bytes;
71 unsigned long long begin_time_us;
74 #define G (*(struct globals*)&bb_common_bufsiz1)
75 #define INIT_G() do { \
76 /* we have to zero it out because of NOEXEC */ \
77 memset(&G, 0, sizeof(G)); \
81 static void dd_output_status(int UNUSED_PARAM cur_signal)
83 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
85 unsigned long long bytes_sec;
86 unsigned long long now_us = monotonic_us(); /* before fprintf */
89 /* Deliberately using %u, not %d */
90 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
91 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
93 G.out_full, G.out_part);
95 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
96 fprintf(stderr, "%llu bytes (%sB) copied, ",
98 /* show fractional digit, use suffixes */
99 make_human_readable_str(G.total_bytes, 1, 0)
102 * ./busybox dd </dev/null >/dev/null
103 * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null
104 * (echo DONE) | ./busybox dd >/dev/null
105 * (sleep 1; echo DONE) | ./busybox dd >/dev/null
107 seconds = (now_us - G.begin_time_us) / 1000000.0;
108 bytes_sec = G.total_bytes / seconds;
109 fprintf(stderr, "%f seconds, %sB/s\n",
111 /* show fractional digit, use suffixes */
112 make_human_readable_str(bytes_sec, 1, 0)
117 static ssize_t full_write_or_warn(const void *buf, size_t len,
118 const char *const filename)
120 ssize_t n = full_write(ofd, buf, len);
122 bb_perror_msg("writing '%s'", filename);
126 static bool write_and_stats(const void *buf, size_t len, size_t obs,
127 const char *filename)
129 ssize_t n = full_write_or_warn(buf, len, filename);
132 if ((size_t)n == obs)
134 else if (n) /* > 0 */
136 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
143 # define XATOU_SFX xatoull_sfx
145 # define XATOU_SFX xatoul_sfx
148 int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
149 int dd_main(int argc UNUSED_PARAM, char **argv)
152 /* Must be in the same order as OP_conv_XXX! */
153 /* (see "flags |= (1 << what)" below) */
154 FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS,
155 FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS,
156 FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS,
157 FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS,
158 FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS,
159 /* end of conv flags */
160 FLAG_TWOBUFS = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS,
163 static const char keywords[] ALIGN1 =
164 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
165 #if ENABLE_FEATURE_DD_IBS_OBS
166 "ibs\0""obs\0""conv\0"
169 #if ENABLE_FEATURE_DD_IBS_OBS
170 static const char conv_words[] ALIGN1 =
171 "notrunc\0""sync\0""noerror\0""fsync\0""swab\0";
180 #if ENABLE_FEATURE_DD_IBS_OBS
184 /* Must be in the same order as FLAG_XXX! */
190 /* Unimplemented conv=XXX: */
191 //nocreat do not create the output file
192 //excl fail if the output file already exists
193 //fdatasync physically write output file data before finishing
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
201 /* Partially implemented: */
202 //swab swap every pair of input bytes: will abort on non-even reads
205 smallint exitcode = EXIT_FAILURE;
209 #if ENABLE_FEATURE_DD_IBS_OBS
216 /* These are all zeroed at once! */
220 ssize_t prev_read_size; /* for detecting swab failure */
223 const char *infile, *outfile;
225 #define flags (Z.flags )
227 #define prev_read_size (Z.prev_read_size)
228 #define count (Z.count )
229 #define seek (Z.seek )
230 #define skip (Z.skip )
231 #define infile (Z.infile )
232 #define outfile (Z.outfile)
234 memset(&Z, 0, sizeof(Z));
236 //fflush_all(); - is this needed because of NOEXEC?
238 for (i = 1; argv[i]; i++) {
244 /* "dd --". NB: coreutils 6.9 will complain if they see
245 * more than one of them. We wouldn't. */
246 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
249 val = strchr(arg, '=');
253 what = index_in_strings(keywords, arg);
256 /* *val = '='; - to preserve ps listing? */
258 #if ENABLE_FEATURE_DD_IBS_OBS
259 if (what == OP_ibs) {
260 /* Must fit into positive ssize_t */
261 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
264 if (what == OP_obs) {
265 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
268 if (what == OP_conv) {
271 /* find ',', replace them with NUL so we can use val for
272 * index_in_strings() without copying.
273 * We rely on val being non-null, else strchr would fault.
275 arg = strchr(val, ',');
278 n = index_in_strings(conv_words, val);
280 bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv");
282 if (!arg) /* no ',' left, so this was the last specifier */
284 /* *arg = ','; - to preserve ps listing? */
285 val = arg + 1; /* skip this keyword and ',' */
291 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
295 /* These can be large: */
296 if (what == OP_count) {
298 count = XATOU_SFX(val, dd_suffixes);
301 if (what == OP_seek) {
302 seek = XATOU_SFX(val, dd_suffixes);
305 if (what == OP_skip) {
306 skip = XATOU_SFX(val, dd_suffixes);
317 } /* end of "for (argv[i])" */
319 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
322 #if ENABLE_FEATURE_DD_IBS_OBS
324 flags |= FLAG_TWOBUFS;
329 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
330 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
332 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
333 G.begin_time_us = monotonic_us();
337 xmove_fd(xopen(infile, O_RDONLY), ifd);
339 infile = bb_msg_standard_input;
342 int oflag = O_WRONLY | O_CREAT;
344 if (!seek && !(flags & FLAG_NOTRUNC))
347 xmove_fd(xopen(outfile, oflag), ofd);
349 if (seek && !(flags & FLAG_NOTRUNC)) {
350 if (ftruncate(ofd, seek * obs) < 0) {
353 if (fstat(ofd, &st) < 0
354 || S_ISREG(st.st_mode)
355 || S_ISDIR(st.st_mode)
362 outfile = bb_msg_standard_output;
365 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
367 ssize_t n = safe_read(ifd, ibuf, ibs);
372 } while (--skip != 0);
376 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
380 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
383 n = safe_read(ifd, ibuf, ibs);
388 if (!(flags & FLAG_NOERROR))
390 bb_simple_perror_msg(infile);
391 /* GNU dd with conv=noerror skips over bad blocks */
392 xlseek(ifd, ibs, SEEK_CUR);
393 /* conv=noerror,sync writes NULs,
394 * conv=noerror just ignores input bad blocks */
397 if (flags & FLAG_SWAB) {
401 /* Our code allows only last read to be odd-sized */
402 if (prev_read_size & 1)
403 bb_error_msg_and_die("can't swab %lu byte buffer",
404 (unsigned long)prev_read_size);
407 /* If n is odd, last byte is not swapped:
408 * echo -n "qwe" | dd conv=swab
414 *p16 = bswap_16(*p16);
418 if ((size_t)n == ibs)
422 if (flags & FLAG_SYNC) {
423 memset(ibuf + n, 0, ibs - n);
427 if (flags & FLAG_TWOBUFS) {
434 memcpy(obuf + oc, tmp, d);
439 if (write_and_stats(obuf, obs, obs, outfile))
445 if (write_and_stats(ibuf, n, obs, outfile))
449 if (flags & FLAG_FSYNC) {
455 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
456 if (write_and_stats(obuf, oc, obs, outfile))
459 if (close(ifd) < 0) {
461 bb_simple_perror_msg_and_die(infile);
464 if (close(ofd) < 0) {
466 bb_simple_perror_msg_and_die(outfile);
469 exitcode = EXIT_SUCCESS;
473 if (ENABLE_FEATURE_CLEAN_UP) {
475 if (flags & FLAG_TWOBUFS)