Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 3 | * Mini dd implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 6 | * Copyright (C) 2000,2001 Matt Kraai |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 11 | //config:config DD |
| 12 | //config: bool "dd" |
| 13 | //config: default y |
| 14 | //config: help |
| 15 | //config: dd copies a file (from standard input to standard output, |
| 16 | //config: by default) using specific input and output blocksizes, |
| 17 | //config: while optionally performing conversions on it. |
| 18 | //config: |
| 19 | //config:config FEATURE_DD_SIGNAL_HANDLING |
| 20 | //config: bool "Enable signal handling for status reporting" |
| 21 | //config: default y |
| 22 | //config: depends on DD |
| 23 | //config: help |
| 24 | //config: Sending a SIGUSR1 signal to a running `dd' process makes it |
| 25 | //config: print to standard error the number of records read and written |
| 26 | //config: so far, then to resume copying. |
| 27 | //config: |
| 28 | //config: $ dd if=/dev/zero of=/dev/null & |
| 29 | //config: $ pid=$!; kill -USR1 $pid; sleep 1; kill $pid |
| 30 | //config: 10899206+0 records in |
| 31 | //config: 10899206+0 records out |
| 32 | //config: |
| 33 | //config:config FEATURE_DD_THIRD_STATUS_LINE |
| 34 | //config: bool "Enable the third status line upon signal" |
| 35 | //config: default y |
| 36 | //config: depends on DD && FEATURE_DD_SIGNAL_HANDLING |
| 37 | //config: help |
| 38 | //config: Displays a coreutils-like third status line with transferred bytes, |
| 39 | //config: elapsed time and speed. |
| 40 | //config: |
| 41 | //config:config FEATURE_DD_IBS_OBS |
| 42 | //config: bool "Enable ibs, obs and conv options" |
| 43 | //config: default y |
| 44 | //config: depends on DD |
| 45 | //config: help |
| 46 | //config: Enables support for writing a certain number of bytes in and out, |
| 47 | //config: at a time, and performing conversions on the data stream. |
| 48 | //config: |
| 49 | //config:config FEATURE_DD_STATUS |
| 50 | //config: bool "Enable status display options" |
| 51 | //config: default y |
| 52 | //config: depends on DD |
| 53 | //config: help |
| 54 | //config: Enables support for status=noxfer/none option. |
| 55 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 56 | //usage:#define dd_trivial_usage |
| 57 | //usage: "[if=FILE] [of=FILE] " IF_FEATURE_DD_IBS_OBS("[ibs=N] [obs=N] ") "[bs=N] [count=N] [skip=N]\n" |
| 58 | //usage: " [seek=N]" IF_FEATURE_DD_IBS_OBS(" [conv=notrunc|noerror|sync|fsync]") |
| 59 | //usage:#define dd_full_usage "\n\n" |
| 60 | //usage: "Copy a file with converting and formatting\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 61 | //usage: "\n if=FILE Read from FILE instead of stdin" |
| 62 | //usage: "\n of=FILE Write to FILE instead of stdout" |
| 63 | //usage: "\n bs=N Read and write N bytes at a time" |
| 64 | //usage: IF_FEATURE_DD_IBS_OBS( |
| 65 | //usage: "\n ibs=N Read N bytes at a time" |
| 66 | //usage: ) |
| 67 | //usage: IF_FEATURE_DD_IBS_OBS( |
| 68 | //usage: "\n obs=N Write N bytes at a time" |
| 69 | //usage: ) |
| 70 | //usage: "\n count=N Copy only N input blocks" |
| 71 | //usage: "\n skip=N Skip N input blocks" |
| 72 | //usage: "\n seek=N Skip N output blocks" |
| 73 | //usage: IF_FEATURE_DD_IBS_OBS( |
| 74 | //usage: "\n conv=notrunc Don't truncate output file" |
| 75 | //usage: "\n conv=noerror Continue after read errors" |
| 76 | //usage: "\n conv=sync Pad blocks with zeros" |
| 77 | //usage: "\n conv=fsync Physically write data out before finishing" |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 78 | //usage: "\n conv=swab Swap every pair of bytes" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 79 | //usage: ) |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 80 | //usage: IF_FEATURE_DD_STATUS( |
| 81 | //usage: "\n status=noxfer Suppress rate output" |
| 82 | //usage: "\n status=none Suppress all output" |
| 83 | //usage: ) |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 84 | //usage: "\n" |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 85 | //usage: "\nN may be suffixed by c (1), w (2), b (512), kB (1000), k (1024), MB, M, GB, G" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 86 | //usage: |
| 87 | //usage:#define dd_example_usage |
| 88 | //usage: "$ dd if=/dev/zero of=/dev/ram1 bs=1M count=4\n" |
| 89 | //usage: "4+0 records in\n" |
| 90 | //usage: "4+0 records out\n" |
| 91 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 92 | #include "libbb.h" |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 93 | |
| 94 | /* This is a NOEXEC applet. Be very careful! */ |
| 95 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 96 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 97 | enum { |
| 98 | ifd = STDIN_FILENO, |
| 99 | ofd = STDOUT_FILENO, |
| 100 | }; |
| 101 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 102 | struct globals { |
| 103 | off_t out_full, out_part, in_full, in_part; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 104 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 105 | unsigned long long total_bytes; |
| 106 | unsigned long long begin_time_us; |
| 107 | #endif |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 108 | int flags; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 109 | } FIX_ALIASING; |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 110 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 111 | #define INIT_G() do { \ |
| 112 | /* we have to zero it out because of NOEXEC */ \ |
| 113 | memset(&G, 0, sizeof(G)); \ |
| 114 | } while (0) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 115 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 116 | enum { |
| 117 | /* Must be in the same order as OP_conv_XXX! */ |
| 118 | /* (see "flags |= (1 << what)" below) */ |
| 119 | FLAG_NOTRUNC = (1 << 0) * ENABLE_FEATURE_DD_IBS_OBS, |
| 120 | FLAG_SYNC = (1 << 1) * ENABLE_FEATURE_DD_IBS_OBS, |
| 121 | FLAG_NOERROR = (1 << 2) * ENABLE_FEATURE_DD_IBS_OBS, |
| 122 | FLAG_FSYNC = (1 << 3) * ENABLE_FEATURE_DD_IBS_OBS, |
| 123 | FLAG_SWAB = (1 << 4) * ENABLE_FEATURE_DD_IBS_OBS, |
| 124 | /* end of conv flags */ |
| 125 | FLAG_TWOBUFS = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS, |
| 126 | FLAG_COUNT = 1 << 6, |
| 127 | FLAG_STATUS = 1 << 7, |
| 128 | FLAG_STATUS_NONE = 1 << 7, |
| 129 | FLAG_STATUS_NOXFER = 1 << 8, |
| 130 | }; |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 131 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 132 | static void dd_output_status(int UNUSED_PARAM cur_signal) |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 133 | { |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 134 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 135 | double seconds; |
| 136 | unsigned long long bytes_sec; |
| 137 | unsigned long long now_us = monotonic_us(); /* before fprintf */ |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 138 | #endif |
| 139 | |
Denis Vlasenko | 980864d | 2007-07-30 10:58:09 +0000 | [diff] [blame] | 140 | /* Deliberately using %u, not %d */ |
| 141 | fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n" |
| 142 | "%"OFF_FMT"u+%"OFF_FMT"u records out\n", |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 143 | G.in_full, G.in_part, |
| 144 | G.out_full, G.out_part); |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 145 | |
| 146 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 147 | # if ENABLE_FEATURE_DD_STATUS |
| 148 | if (G.flags & FLAG_STATUS_NOXFER) /* status=noxfer active? */ |
| 149 | return; |
| 150 | //TODO: should status=none make dd stop reacting to USR1 entirely? |
| 151 | //So far we react to it (we print the stats), |
| 152 | //status=none only suppresses final, non-USR1 generated status message. |
| 153 | # endif |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 154 | fprintf(stderr, "%llu bytes (%sB) copied, ", |
| 155 | G.total_bytes, |
| 156 | /* show fractional digit, use suffixes */ |
| 157 | make_human_readable_str(G.total_bytes, 1, 0) |
| 158 | ); |
| 159 | /* Corner cases: |
| 160 | * ./busybox dd </dev/null >/dev/null |
| 161 | * ./busybox dd bs=1M count=2000 </dev/zero >/dev/null |
| 162 | * (echo DONE) | ./busybox dd >/dev/null |
| 163 | * (sleep 1; echo DONE) | ./busybox dd >/dev/null |
| 164 | */ |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 165 | seconds = (now_us - G.begin_time_us) / 1000000.0; |
| 166 | bytes_sec = G.total_bytes / seconds; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 167 | fprintf(stderr, "%f seconds, %sB/s\n", |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 168 | seconds, |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 169 | /* show fractional digit, use suffixes */ |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 170 | make_human_readable_str(bytes_sec, 1, 0) |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 171 | ); |
| 172 | #endif |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 175 | static ssize_t full_write_or_warn(const void *buf, size_t len, |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 176 | const char *const filename) |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 177 | { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 178 | ssize_t n = full_write(ofd, buf, len); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 179 | if (n < 0) |
| 180 | bb_perror_msg("writing '%s'", filename); |
| 181 | return n; |
| 182 | } |
| 183 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 184 | static bool write_and_stats(const void *buf, size_t len, size_t obs, |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 185 | const char *filename) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 186 | { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 187 | ssize_t n = full_write_or_warn(buf, len, filename); |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 188 | if (n < 0) |
| 189 | return 1; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 190 | if ((size_t)n == obs) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 191 | G.out_full++; |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 192 | else if (n) /* > 0 */ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 193 | G.out_part++; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 194 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 195 | G.total_bytes += n; |
| 196 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 200 | #if ENABLE_LFS |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 201 | # define XATOU_SFX xatoull_sfx |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 202 | #else |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 203 | # define XATOU_SFX xatoul_sfx |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 204 | #endif |
| 205 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 206 | int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 207 | int dd_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 208 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 209 | static const char keywords[] ALIGN1 = |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 210 | "bs\0""count\0""seek\0""skip\0""if\0""of\0"IF_FEATURE_DD_STATUS("status\0") |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 211 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 9d4a17e | 2008-04-23 05:44:59 +0000 | [diff] [blame] | 212 | "ibs\0""obs\0""conv\0" |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 213 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 214 | ; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 215 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 216 | static const char conv_words[] ALIGN1 = |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 217 | "notrunc\0""sync\0""noerror\0""fsync\0""swab\0"; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 218 | #endif |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 219 | #if ENABLE_FEATURE_DD_STATUS |
| 220 | static const char status_words[] ALIGN1 = |
| 221 | "none\0""noxfer\0"; |
| 222 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 223 | enum { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 224 | OP_bs = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 225 | OP_count, |
| 226 | OP_seek, |
| 227 | OP_skip, |
| 228 | OP_if, |
| 229 | OP_of, |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 230 | IF_FEATURE_DD_STATUS(OP_status,) |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 231 | #if ENABLE_FEATURE_DD_IBS_OBS |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 232 | OP_ibs, |
| 233 | OP_obs, |
| 234 | OP_conv, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 235 | /* Must be in the same order as FLAG_XXX! */ |
| 236 | OP_conv_notrunc = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 237 | OP_conv_sync, |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 238 | OP_conv_noerror, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 239 | OP_conv_fsync, |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 240 | OP_conv_swab, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 241 | /* Unimplemented conv=XXX: */ |
| 242 | //nocreat do not create the output file |
| 243 | //excl fail if the output file already exists |
| 244 | //fdatasync physically write output file data before finishing |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 245 | //lcase change upper case to lower case |
| 246 | //ucase change lower case to upper case |
| 247 | //block pad newline-terminated records with spaces to cbs-size |
| 248 | //unblock replace trailing spaces in cbs-size records with newline |
| 249 | //ascii from EBCDIC to ASCII |
| 250 | //ebcdic from ASCII to EBCDIC |
| 251 | //ibm from ASCII to alternate EBCDIC |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 252 | /* Partially implemented: */ |
| 253 | //swab swap every pair of input bytes: will abort on non-even reads |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 254 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 255 | }; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 256 | smallint exitcode = EXIT_FAILURE; |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 257 | int i; |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 258 | size_t ibs = 512; |
| 259 | char *ibuf; |
| 260 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 261 | size_t obs = 512; |
| 262 | char *obuf; |
| 263 | #else |
| 264 | # define obs ibs |
| 265 | # define obuf ibuf |
| 266 | #endif |
| 267 | /* These are all zeroed at once! */ |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 268 | struct { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 269 | size_t oc; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 270 | ssize_t prev_read_size; /* for detecting swab failure */ |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 271 | off_t count; |
| 272 | off_t seek, skip; |
| 273 | const char *infile, *outfile; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 274 | } Z; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 275 | #define oc (Z.oc ) |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 276 | #define prev_read_size (Z.prev_read_size) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 277 | #define count (Z.count ) |
| 278 | #define seek (Z.seek ) |
| 279 | #define skip (Z.skip ) |
| 280 | #define infile (Z.infile ) |
| 281 | #define outfile (Z.outfile) |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 282 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 283 | memset(&Z, 0, sizeof(Z)); |
| 284 | INIT_G(); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 285 | //fflush_all(); - is this needed because of NOEXEC? |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 286 | |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 287 | for (i = 1; argv[i]; i++) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 288 | int what; |
| 289 | char *val; |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 290 | char *arg = argv[i]; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 291 | |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 292 | #if ENABLE_DESKTOP |
| 293 | /* "dd --". NB: coreutils 6.9 will complain if they see |
| 294 | * more than one of them. We wouldn't. */ |
| 295 | if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0') |
| 296 | continue; |
| 297 | #endif |
| 298 | val = strchr(arg, '='); |
| 299 | if (val == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 300 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 301 | *val = '\0'; |
| 302 | what = index_in_strings(keywords, arg); |
| 303 | if (what < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 304 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 305 | /* *val = '='; - to preserve ps listing? */ |
| 306 | val++; |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 307 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 308 | if (what == OP_ibs) { |
| 309 | /* Must fit into positive ssize_t */ |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 310 | ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 311 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 312 | } |
| 313 | if (what == OP_obs) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 314 | obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 315 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 316 | } |
| 317 | if (what == OP_conv) { |
| 318 | while (1) { |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 319 | int n; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 320 | /* find ',', replace them with NUL so we can use val for |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 321 | * index_in_strings() without copying. |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 322 | * We rely on val being non-null, else strchr would fault. |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 323 | */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 324 | arg = strchr(val, ','); |
| 325 | if (arg) |
| 326 | *arg = '\0'; |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 327 | n = index_in_strings(conv_words, val); |
| 328 | if (n < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 329 | bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "conv"); |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 330 | G.flags |= (1 << n); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 331 | if (!arg) /* no ',' left, so this was the last specifier */ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 332 | break; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 333 | /* *arg = ','; - to preserve ps listing? */ |
| 334 | val = arg + 1; /* skip this keyword and ',' */ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 335 | } |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 336 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 337 | } |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 338 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 339 | if (what == OP_bs) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 340 | ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, cwbkMG_suffixes); |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 341 | obs = ibs; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 342 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 343 | } |
| 344 | /* These can be large: */ |
| 345 | if (what == OP_count) { |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 346 | G.flags |= FLAG_COUNT; |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 347 | count = XATOU_SFX(val, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 348 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 349 | } |
| 350 | if (what == OP_seek) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 351 | seek = XATOU_SFX(val, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 352 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 353 | } |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 354 | if (what == OP_skip) { |
Ari Sundholm | 8893023 | 2015-03-04 18:46:48 +0200 | [diff] [blame] | 355 | skip = XATOU_SFX(val, cwbkMG_suffixes); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 356 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 357 | } |
| 358 | if (what == OP_if) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 359 | infile = val; |
| 360 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 361 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 362 | if (what == OP_of) { |
| 363 | outfile = val; |
| 364 | /*continue;*/ |
| 365 | } |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 366 | #if ENABLE_FEATURE_DD_STATUS |
| 367 | if (what == OP_status) { |
| 368 | int n; |
| 369 | n = index_in_strings(status_words, val); |
| 370 | if (n < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 371 | bb_error_msg_and_die(bb_msg_invalid_arg_to, val, "status"); |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 372 | G.flags |= FLAG_STATUS << n; |
| 373 | /*continue;*/ |
| 374 | } |
| 375 | #endif |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 376 | } /* end of "for (argv[i])" */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 377 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 378 | //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 379 | ibuf = xmalloc(ibs); |
| 380 | obuf = ibuf; |
| 381 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 382 | if (ibs != obs) { |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 383 | G.flags |= FLAG_TWOBUFS; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 384 | obuf = xmalloc(obs); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 385 | } |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 386 | #endif |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 387 | |
| 388 | #if ENABLE_FEATURE_DD_SIGNAL_HANDLING |
| 389 | signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status); |
| 390 | #endif |
| 391 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 392 | G.begin_time_us = monotonic_us(); |
| 393 | #endif |
| 394 | |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 395 | if (infile) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 396 | xmove_fd(xopen(infile, O_RDONLY), ifd); |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 397 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 398 | infile = bb_msg_standard_input; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 399 | } |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 400 | if (outfile) { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 401 | int oflag = O_WRONLY | O_CREAT; |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 402 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 403 | if (!seek && !(G.flags & FLAG_NOTRUNC)) |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 404 | oflag |= O_TRUNC; |
| 405 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 406 | xmove_fd(xopen(outfile, oflag), ofd); |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 407 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 408 | if (seek && !(G.flags & FLAG_NOTRUNC)) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 409 | if (ftruncate(ofd, seek * obs) < 0) { |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 410 | struct stat st; |
| 411 | |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 412 | if (fstat(ofd, &st) < 0 |
| 413 | || S_ISREG(st.st_mode) |
| 414 | || S_ISDIR(st.st_mode) |
| 415 | ) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 416 | goto die_outfile; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 417 | } |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 418 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 419 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 420 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 421 | outfile = bb_msg_standard_output; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 422 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 423 | if (skip) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 424 | if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) { |
Denys Vlasenko | 1b57fe1 | 2013-08-20 12:45:05 +0200 | [diff] [blame] | 425 | do { |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 426 | ssize_t n = safe_read(ifd, ibuf, ibs); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 427 | if (n < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 428 | goto die_infile; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 429 | if (n == 0) |
| 430 | break; |
Denys Vlasenko | 1b57fe1 | 2013-08-20 12:45:05 +0200 | [diff] [blame] | 431 | } while (--skip != 0); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 432 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 433 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 434 | if (seek) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 435 | if (lseek(ofd, seek * obs, SEEK_CUR) < 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 436 | goto die_outfile; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 437 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 438 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 439 | while (!(G.flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) { |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 440 | ssize_t n; |
| 441 | |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 442 | n = safe_read(ifd, ibuf, ibs); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 443 | if (n == 0) |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 444 | break; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 445 | if (n < 0) { |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 446 | /* "Bad block" */ |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 447 | if (!(G.flags & FLAG_NOERROR)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 448 | goto die_infile; |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 449 | bb_simple_perror_msg(infile); |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 450 | /* GNU dd with conv=noerror skips over bad blocks */ |
Denys Vlasenko | 334fcc8 | 2009-08-05 23:16:27 +0200 | [diff] [blame] | 451 | xlseek(ifd, ibs, SEEK_CUR); |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 452 | /* conv=noerror,sync writes NULs, |
| 453 | * conv=noerror just ignores input bad blocks */ |
| 454 | n = 0; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 455 | } |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 456 | if (G.flags & FLAG_SWAB) { |
Denys Vlasenko | 0ff0b32 | 2013-08-20 12:49:28 +0200 | [diff] [blame] | 457 | uint16_t *p16; |
| 458 | ssize_t n2; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 459 | |
| 460 | /* Our code allows only last read to be odd-sized */ |
| 461 | if (prev_read_size & 1) |
| 462 | bb_error_msg_and_die("can't swab %lu byte buffer", |
| 463 | (unsigned long)prev_read_size); |
| 464 | prev_read_size = n; |
| 465 | |
Denys Vlasenko | 90e9cfd | 2013-08-20 12:27:19 +0200 | [diff] [blame] | 466 | /* If n is odd, last byte is not swapped: |
Denys Vlasenko | 8395bd3 | 2013-08-19 10:30:55 +0200 | [diff] [blame] | 467 | * echo -n "qwe" | dd conv=swab |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 468 | * prints "wqe". |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 469 | */ |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 470 | p16 = (void*) ibuf; |
Denys Vlasenko | 0ff0b32 | 2013-08-20 12:49:28 +0200 | [diff] [blame] | 471 | n2 = (n >> 1); |
| 472 | while (--n2 >= 0) { |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 473 | *p16 = bswap_16(*p16); |
| 474 | p16++; |
| 475 | } |
| 476 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 477 | if ((size_t)n == ibs) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 478 | G.in_full++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 479 | else { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 480 | G.in_part++; |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 481 | if (G.flags & FLAG_SYNC) { |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 482 | memset(ibuf + n, 0, ibs - n); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 483 | n = ibs; |
| 484 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 485 | } |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 486 | if (G.flags & FLAG_TWOBUFS) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 487 | char *tmp = ibuf; |
| 488 | while (n) { |
| 489 | size_t d = obs - oc; |
| 490 | |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 491 | if (d > (size_t)n) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 492 | d = n; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 493 | memcpy(obuf + oc, tmp, d); |
| 494 | n -= d; |
| 495 | tmp += d; |
| 496 | oc += d; |
| 497 | if (oc == obs) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 498 | if (write_and_stats(obuf, obs, obs, outfile)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 499 | goto out_status; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 500 | oc = 0; |
| 501 | } |
| 502 | } |
Denys Vlasenko | f3114a3 | 2013-08-20 12:40:29 +0200 | [diff] [blame] | 503 | } else { |
| 504 | if (write_and_stats(ibuf, n, obs, outfile)) |
| 505 | goto out_status; |
| 506 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 507 | |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 508 | if (G.flags & FLAG_FSYNC) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 509 | if (fsync(ofd) < 0) |
| 510 | goto die_outfile; |
| 511 | } |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 512 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 513 | |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 514 | if (ENABLE_FEATURE_DD_IBS_OBS && oc) { |
Denys Vlasenko | f3114a3 | 2013-08-20 12:40:29 +0200 | [diff] [blame] | 515 | if (write_and_stats(obuf, oc, obs, outfile)) |
| 516 | goto out_status; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 517 | } |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 518 | if (close(ifd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 519 | die_infile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 520 | bb_simple_perror_msg_and_die(infile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 521 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 522 | |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 523 | if (close(ofd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 524 | die_outfile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 525 | bb_simple_perror_msg_and_die(outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 526 | } |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 527 | |
| 528 | exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 529 | out_status: |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 530 | if (!ENABLE_FEATURE_DD_STATUS || !(G.flags & FLAG_STATUS_NONE)) |
| 531 | dd_output_status(0); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 532 | |
Alexey Soloviev | f922154 | 2011-03-16 10:39:46 +0300 | [diff] [blame] | 533 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 534 | free(obuf); |
Ari Sundholm | f22a838 | 2015-02-07 01:41:22 +0100 | [diff] [blame] | 535 | if (G.flags & FLAG_TWOBUFS) |
Alexey Soloviev | f922154 | 2011-03-16 10:39:46 +0300 | [diff] [blame] | 536 | free(ibuf); |
| 537 | } |
| 538 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 539 | return exitcode; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 540 | } |