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 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 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" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 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" |
| 21 | //usage: ) |
| 22 | //usage: IF_FEATURE_DD_IBS_OBS( |
| 23 | //usage: "\n obs=N Write N bytes at a time" |
| 24 | //usage: ) |
| 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" |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 33 | //usage: "\n conv=swab Swap every pair of bytes" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 34 | //usage: ) |
| 35 | //usage: "\n" |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 36 | //usage: "\nN may be suffixed by c (1), w (2), b (512), kD (1000), k (1024), MD, M, GD, G" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 37 | //usage: |
| 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" |
| 42 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 43 | #include "libbb.h" |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 44 | |
| 45 | /* This is a NOEXEC applet. Be very careful! */ |
| 46 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 48 | enum { |
| 49 | ifd = STDIN_FILENO, |
| 50 | ofd = STDOUT_FILENO, |
| 51 | }; |
| 52 | |
Matt Kraai | a164c64 | 2001-02-05 17:50:03 +0000 | [diff] [blame] | 53 | static const struct suffix_mult dd_suffixes[] = { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 54 | { "c", 1 }, |
| 55 | { "w", 2 }, |
| 56 | { "b", 512 }, |
| 57 | { "kD", 1000 }, |
| 58 | { "k", 1024 }, |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 59 | { "K", 1024 }, /* compat with coreutils dd */ |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 60 | { "MD", 1000000 }, |
| 61 | { "M", 1048576 }, |
| 62 | { "GD", 1000000000 }, |
| 63 | { "G", 1073741824 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 64 | { "", 0 } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 67 | struct globals { |
| 68 | off_t out_full, out_part, in_full, in_part; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 69 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 70 | unsigned long long total_bytes; |
| 71 | unsigned long long begin_time_us; |
| 72 | #endif |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 73 | } FIX_ALIASING; |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 74 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 75 | #define INIT_G() do { \ |
| 76 | /* we have to zero it out because of NOEXEC */ \ |
| 77 | memset(&G, 0, sizeof(G)); \ |
| 78 | } while (0) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 79 | |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 81 | static void dd_output_status(int UNUSED_PARAM cur_signal) |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 82 | { |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 83 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 84 | double seconds; |
| 85 | unsigned long long bytes_sec; |
| 86 | unsigned long long now_us = monotonic_us(); /* before fprintf */ |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 87 | #endif |
| 88 | |
Denis Vlasenko | 980864d | 2007-07-30 10:58:09 +0000 | [diff] [blame] | 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", |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 92 | G.in_full, G.in_part, |
| 93 | G.out_full, G.out_part); |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 94 | |
| 95 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 96 | fprintf(stderr, "%llu bytes (%sB) copied, ", |
| 97 | G.total_bytes, |
| 98 | /* show fractional digit, use suffixes */ |
| 99 | make_human_readable_str(G.total_bytes, 1, 0) |
| 100 | ); |
| 101 | /* Corner cases: |
| 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 |
| 106 | */ |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 107 | seconds = (now_us - G.begin_time_us) / 1000000.0; |
| 108 | bytes_sec = G.total_bytes / seconds; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 109 | fprintf(stderr, "%f seconds, %sB/s\n", |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 110 | seconds, |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 111 | /* show fractional digit, use suffixes */ |
Denys Vlasenko | 7eabffa | 2009-10-14 17:43:41 +0200 | [diff] [blame] | 112 | make_human_readable_str(bytes_sec, 1, 0) |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 113 | ); |
| 114 | #endif |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 117 | 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] | 118 | const char *const filename) |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 119 | { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 120 | ssize_t n = full_write(ofd, buf, len); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 121 | if (n < 0) |
| 122 | bb_perror_msg("writing '%s'", filename); |
| 123 | return n; |
| 124 | } |
| 125 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 126 | 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] | 127 | const char *filename) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 128 | { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 129 | ssize_t n = full_write_or_warn(buf, len, filename); |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 130 | if (n < 0) |
| 131 | return 1; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 132 | if ((size_t)n == obs) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 133 | G.out_full++; |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 134 | else if (n) /* > 0 */ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 135 | G.out_part++; |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 136 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 137 | G.total_bytes += n; |
| 138 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 142 | #if ENABLE_LFS |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 143 | # define XATOU_SFX xatoull_sfx |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 144 | #else |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 145 | # define XATOU_SFX xatoul_sfx |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 146 | #endif |
| 147 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 148 | int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 149 | int dd_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 150 | { |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 151 | enum { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 152 | /* Must be in the same order as OP_conv_XXX! */ |
| 153 | /* (see "flags |= (1 << what)" below) */ |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 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, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 159 | /* end of conv flags */ |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 160 | FLAG_TWOBUFS = (1 << 5) * ENABLE_FEATURE_DD_IBS_OBS, |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 161 | FLAG_COUNT = 1 << 6, |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 162 | }; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 163 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 9d4a17e | 2008-04-23 05:44:59 +0000 | [diff] [blame] | 164 | "bs\0""count\0""seek\0""skip\0""if\0""of\0" |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 165 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 9d4a17e | 2008-04-23 05:44:59 +0000 | [diff] [blame] | 166 | "ibs\0""obs\0""conv\0" |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 167 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 168 | ; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 169 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 170 | static const char conv_words[] ALIGN1 = |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 171 | "notrunc\0""sync\0""noerror\0""fsync\0""swab\0"; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 172 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 173 | enum { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 174 | OP_bs = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 175 | OP_count, |
| 176 | OP_seek, |
| 177 | OP_skip, |
| 178 | OP_if, |
| 179 | OP_of, |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 180 | #if ENABLE_FEATURE_DD_IBS_OBS |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 181 | OP_ibs, |
| 182 | OP_obs, |
| 183 | OP_conv, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 184 | /* Must be in the same order as FLAG_XXX! */ |
| 185 | OP_conv_notrunc = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 186 | OP_conv_sync, |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 187 | OP_conv_noerror, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 188 | OP_conv_fsync, |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 189 | OP_conv_swab, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 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 |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 201 | /* Partially implemented: */ |
| 202 | //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] | 203 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 204 | }; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 205 | smallint exitcode = EXIT_FAILURE; |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 206 | int i; |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 207 | size_t ibs = 512; |
| 208 | char *ibuf; |
| 209 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 210 | size_t obs = 512; |
| 211 | char *obuf; |
| 212 | #else |
| 213 | # define obs ibs |
| 214 | # define obuf ibuf |
| 215 | #endif |
| 216 | /* These are all zeroed at once! */ |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 217 | struct { |
| 218 | int flags; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 219 | size_t oc; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 220 | ssize_t prev_read_size; /* for detecting swab failure */ |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 221 | off_t count; |
| 222 | off_t seek, skip; |
| 223 | const char *infile, *outfile; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 224 | } Z; |
| 225 | #define flags (Z.flags ) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 226 | #define oc (Z.oc ) |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 227 | #define prev_read_size (Z.prev_read_size) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 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) |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 233 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 234 | memset(&Z, 0, sizeof(Z)); |
| 235 | INIT_G(); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 236 | //fflush_all(); - is this needed because of NOEXEC? |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 237 | |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 238 | for (i = 1; argv[i]; i++) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 239 | int what; |
| 240 | char *val; |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 241 | char *arg = argv[i]; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 243 | #if ENABLE_DESKTOP |
| 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') |
| 247 | continue; |
| 248 | #endif |
| 249 | val = strchr(arg, '='); |
| 250 | if (val == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 251 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 252 | *val = '\0'; |
| 253 | what = index_in_strings(keywords, arg); |
| 254 | if (what < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 255 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 256 | /* *val = '='; - to preserve ps listing? */ |
| 257 | val++; |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 258 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 259 | if (what == OP_ibs) { |
| 260 | /* Must fit into positive ssize_t */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 261 | ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); |
| 262 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 263 | } |
| 264 | if (what == OP_obs) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 265 | obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); |
| 266 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 267 | } |
| 268 | if (what == OP_conv) { |
| 269 | while (1) { |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 270 | int n; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 271 | /* find ',', replace them with NUL so we can use val for |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 272 | * index_in_strings() without copying. |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 273 | * We rely on val being non-null, else strchr would fault. |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 274 | */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 275 | arg = strchr(val, ','); |
| 276 | if (arg) |
| 277 | *arg = '\0'; |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 278 | n = index_in_strings(conv_words, val); |
| 279 | if (n < 0) |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 280 | bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv"); |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 281 | flags |= (1 << n); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 282 | if (!arg) /* no ',' left, so this was the last specifier */ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 283 | break; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 284 | /* *arg = ','; - to preserve ps listing? */ |
| 285 | val = arg + 1; /* skip this keyword and ',' */ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 286 | } |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 287 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 288 | } |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 289 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 290 | if (what == OP_bs) { |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 291 | ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); |
| 292 | obs = ibs; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 293 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 294 | } |
| 295 | /* These can be large: */ |
| 296 | if (what == OP_count) { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 297 | flags |= FLAG_COUNT; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 298 | count = XATOU_SFX(val, dd_suffixes); |
| 299 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 300 | } |
| 301 | if (what == OP_seek) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 302 | seek = XATOU_SFX(val, dd_suffixes); |
| 303 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 304 | } |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 305 | if (what == OP_skip) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 306 | skip = XATOU_SFX(val, dd_suffixes); |
| 307 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 308 | } |
| 309 | if (what == OP_if) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 310 | infile = val; |
| 311 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 312 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 313 | if (what == OP_of) { |
| 314 | outfile = val; |
| 315 | /*continue;*/ |
| 316 | } |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 317 | } /* end of "for (argv[i])" */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 318 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 319 | //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] | 320 | ibuf = xmalloc(ibs); |
| 321 | obuf = ibuf; |
| 322 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 323 | if (ibs != obs) { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 324 | flags |= FLAG_TWOBUFS; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 325 | obuf = xmalloc(obs); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 326 | } |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 327 | #endif |
Gabor Heja | 4e5b07b | 2009-10-14 00:29:28 +0200 | [diff] [blame] | 328 | |
| 329 | #if ENABLE_FEATURE_DD_SIGNAL_HANDLING |
| 330 | signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status); |
| 331 | #endif |
| 332 | #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE |
| 333 | G.begin_time_us = monotonic_us(); |
| 334 | #endif |
| 335 | |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 336 | if (infile) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 337 | xmove_fd(xopen(infile, O_RDONLY), ifd); |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 338 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 339 | infile = bb_msg_standard_input; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 340 | } |
Denys Vlasenko | 4502bb1 | 2013-08-20 12:08:46 +0200 | [diff] [blame] | 341 | if (outfile) { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 342 | int oflag = O_WRONLY | O_CREAT; |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 344 | if (!seek && !(flags & FLAG_NOTRUNC)) |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 345 | oflag |= O_TRUNC; |
| 346 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 347 | xmove_fd(xopen(outfile, oflag), ofd); |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 348 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 349 | if (seek && !(flags & FLAG_NOTRUNC)) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 350 | if (ftruncate(ofd, seek * obs) < 0) { |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 351 | struct stat st; |
| 352 | |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 353 | if (fstat(ofd, &st) < 0 |
| 354 | || S_ISREG(st.st_mode) |
| 355 | || S_ISDIR(st.st_mode) |
| 356 | ) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 357 | goto die_outfile; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 358 | } |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 359 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 360 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 361 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 362 | outfile = bb_msg_standard_output; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 363 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 364 | if (skip) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 365 | if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) { |
Denys Vlasenko | 1b57fe1 | 2013-08-20 12:45:05 +0200 | [diff] [blame] | 366 | do { |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 367 | ssize_t n = safe_read(ifd, ibuf, ibs); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 368 | if (n < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 369 | goto die_infile; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 370 | if (n == 0) |
| 371 | break; |
Denys Vlasenko | 1b57fe1 | 2013-08-20 12:45:05 +0200 | [diff] [blame] | 372 | } while (--skip != 0); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 373 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 374 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 375 | if (seek) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 376 | if (lseek(ofd, seek * obs, SEEK_CUR) < 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 377 | goto die_outfile; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 378 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 379 | |
Denis Vlasenko | 2b03e5e | 2007-07-29 14:59:06 +0000 | [diff] [blame] | 380 | while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) { |
Denys Vlasenko | 4b1896c | 2013-08-19 09:00:08 +0200 | [diff] [blame] | 381 | ssize_t n; |
| 382 | |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 383 | n = safe_read(ifd, ibuf, ibs); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 384 | if (n == 0) |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 385 | break; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 386 | if (n < 0) { |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 387 | /* "Bad block" */ |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 388 | if (!(flags & FLAG_NOERROR)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 389 | goto die_infile; |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 390 | bb_simple_perror_msg(infile); |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 391 | /* GNU dd with conv=noerror skips over bad blocks */ |
Denys Vlasenko | 334fcc8 | 2009-08-05 23:16:27 +0200 | [diff] [blame] | 392 | xlseek(ifd, ibs, SEEK_CUR); |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 393 | /* conv=noerror,sync writes NULs, |
| 394 | * conv=noerror just ignores input bad blocks */ |
| 395 | n = 0; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 396 | } |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 397 | if (flags & FLAG_SWAB) { |
Denys Vlasenko | 0ff0b32 | 2013-08-20 12:49:28 +0200 | [diff] [blame] | 398 | uint16_t *p16; |
| 399 | ssize_t n2; |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 400 | |
| 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); |
| 405 | prev_read_size = n; |
| 406 | |
Denys Vlasenko | 90e9cfd | 2013-08-20 12:27:19 +0200 | [diff] [blame] | 407 | /* If n is odd, last byte is not swapped: |
Denys Vlasenko | 8395bd3 | 2013-08-19 10:30:55 +0200 | [diff] [blame] | 408 | * echo -n "qwe" | dd conv=swab |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 409 | * prints "wqe". |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 410 | */ |
Denys Vlasenko | 5b9910f | 2013-08-20 02:50:49 +0200 | [diff] [blame] | 411 | p16 = (void*) ibuf; |
Denys Vlasenko | 0ff0b32 | 2013-08-20 12:49:28 +0200 | [diff] [blame] | 412 | n2 = (n >> 1); |
| 413 | while (--n2 >= 0) { |
Denys Vlasenko | b941316 | 2013-08-19 09:01:39 +0200 | [diff] [blame] | 414 | *p16 = bswap_16(*p16); |
| 415 | p16++; |
| 416 | } |
| 417 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 418 | if ((size_t)n == ibs) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 419 | G.in_full++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 420 | else { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 421 | G.in_part++; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 422 | if (flags & FLAG_SYNC) { |
Denys Vlasenko | f00cfdf | 2009-09-18 20:49:36 +0200 | [diff] [blame] | 423 | memset(ibuf + n, 0, ibs - n); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 424 | n = ibs; |
| 425 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 426 | } |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 427 | if (flags & FLAG_TWOBUFS) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 428 | char *tmp = ibuf; |
| 429 | while (n) { |
| 430 | size_t d = obs - oc; |
| 431 | |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 432 | if (d > (size_t)n) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 433 | d = n; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 434 | memcpy(obuf + oc, tmp, d); |
| 435 | n -= d; |
| 436 | tmp += d; |
| 437 | oc += d; |
| 438 | if (oc == obs) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 439 | if (write_and_stats(obuf, obs, obs, outfile)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 440 | goto out_status; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 441 | oc = 0; |
| 442 | } |
| 443 | } |
Denys Vlasenko | f3114a3 | 2013-08-20 12:40:29 +0200 | [diff] [blame] | 444 | } else { |
| 445 | if (write_and_stats(ibuf, n, obs, outfile)) |
| 446 | goto out_status; |
| 447 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 448 | |
| 449 | if (flags & FLAG_FSYNC) { |
| 450 | if (fsync(ofd) < 0) |
| 451 | goto die_outfile; |
| 452 | } |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 453 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 454 | |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 455 | if (ENABLE_FEATURE_DD_IBS_OBS && oc) { |
Denys Vlasenko | f3114a3 | 2013-08-20 12:40:29 +0200 | [diff] [blame] | 456 | if (write_and_stats(obuf, oc, obs, outfile)) |
| 457 | goto out_status; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 458 | } |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 459 | if (close(ifd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 460 | die_infile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 461 | bb_simple_perror_msg_and_die(infile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 462 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 463 | |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 464 | if (close(ofd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 465 | die_outfile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 466 | bb_simple_perror_msg_and_die(outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 467 | } |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 468 | |
| 469 | exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 470 | out_status: |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 471 | dd_output_status(0); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 472 | |
Alexey Soloviev | f922154 | 2011-03-16 10:39:46 +0300 | [diff] [blame] | 473 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 474 | free(obuf); |
| 475 | if (flags & FLAG_TWOBUFS) |
| 476 | free(ibuf); |
| 477 | } |
| 478 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 479 | return exitcode; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 480 | } |