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 | * |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 11 | #include <signal.h> /* For FEATURE_DD_SIGNAL_HANDLING */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 13 | |
| 14 | /* This is a NOEXEC applet. Be very careful! */ |
| 15 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 17 | enum { |
| 18 | ifd = STDIN_FILENO, |
| 19 | ofd = STDOUT_FILENO, |
| 20 | }; |
| 21 | |
Matt Kraai | a164c64 | 2001-02-05 17:50:03 +0000 | [diff] [blame] | 22 | static const struct suffix_mult dd_suffixes[] = { |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 23 | { "c", 1 }, |
| 24 | { "w", 2 }, |
| 25 | { "b", 512 }, |
| 26 | { "kD", 1000 }, |
| 27 | { "k", 1024 }, |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 28 | { "K", 1024 }, /* compat with coreutils dd */ |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 29 | { "MD", 1000000 }, |
| 30 | { "M", 1048576 }, |
| 31 | { "GD", 1000000000 }, |
| 32 | { "G", 1073741824 }, |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 33 | { } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 36 | struct globals { |
| 37 | off_t out_full, out_part, in_full, in_part; |
| 38 | }; |
| 39 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 40 | /* We have to zero it out because of NOEXEC */ |
| 41 | #define INIT_G() memset(&G, 0, sizeof(G)) |
| 42 | |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 43 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 44 | static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal) |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 45 | { |
Denis Vlasenko | 980864d | 2007-07-30 10:58:09 +0000 | [diff] [blame] | 46 | /* Deliberately using %u, not %d */ |
| 47 | fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n" |
| 48 | "%"OFF_FMT"u+%"OFF_FMT"u records out\n", |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 49 | G.in_full, G.in_part, |
| 50 | G.out_full, G.out_part); |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 53 | 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] | 54 | const char *const filename) |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 55 | { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 56 | ssize_t n = full_write(ofd, buf, len); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 57 | if (n < 0) |
| 58 | bb_perror_msg("writing '%s'", filename); |
| 59 | return n; |
| 60 | } |
| 61 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 62 | 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] | 63 | const char *filename) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 64 | { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 65 | ssize_t n = full_write_or_warn(buf, len, filename); |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 66 | if (n < 0) |
| 67 | return 1; |
| 68 | if (n == obs) |
| 69 | G.out_full++; |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 70 | else if (n) /* > 0 */ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 71 | G.out_part++; |
| 72 | return 0; |
| 73 | } |
| 74 | |
Denis Vlasenko | 5dd27b1 | 2006-11-25 14:46:21 +0000 | [diff] [blame] | 75 | #if ENABLE_LFS |
| 76 | #define XATOU_SFX xatoull_sfx |
| 77 | #else |
| 78 | #define XATOU_SFX xatoul_sfx |
| 79 | #endif |
| 80 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 81 | int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 82 | int dd_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 83 | { |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 84 | enum { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 85 | /* Must be in the same order as OP_conv_XXX! */ |
| 86 | /* (see "flags |= (1 << what)" below) */ |
| 87 | FLAG_NOTRUNC = 1 << 0, |
| 88 | FLAG_SYNC = 1 << 1, |
| 89 | FLAG_NOERROR = 1 << 2, |
| 90 | FLAG_FSYNC = 1 << 3, |
| 91 | /* end of conv flags */ |
| 92 | FLAG_TWOBUFS = 1 << 4, |
| 93 | FLAG_COUNT = 1 << 5, |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 94 | }; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 95 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 9d4a17e | 2008-04-23 05:44:59 +0000 | [diff] [blame] | 96 | "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] | 97 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 9d4a17e | 2008-04-23 05:44:59 +0000 | [diff] [blame] | 98 | "ibs\0""obs\0""conv\0" |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 99 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 100 | ; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 101 | #if ENABLE_FEATURE_DD_IBS_OBS |
| 102 | static const char conv_words[] ALIGN1 = |
| 103 | "notrunc\0""sync\0""noerror\0""fsync\0"; |
| 104 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 105 | enum { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 106 | OP_bs = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 107 | OP_count, |
| 108 | OP_seek, |
| 109 | OP_skip, |
| 110 | OP_if, |
| 111 | OP_of, |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 112 | #if ENABLE_FEATURE_DD_IBS_OBS |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 113 | OP_ibs, |
| 114 | OP_obs, |
| 115 | OP_conv, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 116 | /* Must be in the same order as FLAG_XXX! */ |
| 117 | OP_conv_notrunc = 0, |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 118 | OP_conv_sync, |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 119 | OP_conv_noerror, |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 120 | OP_conv_fsync, |
| 121 | /* Unimplemented conv=XXX: */ |
| 122 | //nocreat do not create the output file |
| 123 | //excl fail if the output file already exists |
| 124 | //fdatasync physically write output file data before finishing |
| 125 | //swab swap every pair of input bytes |
| 126 | //lcase change upper case to lower case |
| 127 | //ucase change lower case to upper case |
| 128 | //block pad newline-terminated records with spaces to cbs-size |
| 129 | //unblock replace trailing spaces in cbs-size records with newline |
| 130 | //ascii from EBCDIC to ASCII |
| 131 | //ebcdic from ASCII to EBCDIC |
| 132 | //ibm from ASCII to alternate EBCDIC |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 133 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 134 | }; |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 135 | int exitcode = EXIT_FAILURE; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 136 | size_t ibs = 512, obs = 512; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 137 | ssize_t n, w; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 138 | char *ibuf, *obuf; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 139 | /* And these are all zeroed at once! */ |
| 140 | struct { |
| 141 | int flags; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 142 | size_t oc; |
| 143 | off_t count; |
| 144 | off_t seek, skip; |
| 145 | const char *infile, *outfile; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 146 | } Z; |
| 147 | #define flags (Z.flags ) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 148 | #define oc (Z.oc ) |
| 149 | #define count (Z.count ) |
| 150 | #define seek (Z.seek ) |
| 151 | #define skip (Z.skip ) |
| 152 | #define infile (Z.infile ) |
| 153 | #define outfile (Z.outfile) |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 154 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 155 | memset(&Z, 0, sizeof(Z)); |
| 156 | INIT_G(); |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 157 | //fflush(NULL); - is this needed because of NOEXEC? |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 158 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 159 | #if ENABLE_FEATURE_DD_SIGNAL_HANDLING |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 160 | signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status); |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 161 | #endif |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 163 | for (n = 1; argv[n]; n++) { |
| 164 | int what; |
| 165 | char *val; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 166 | char *arg = argv[n]; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 167 | |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 168 | #if ENABLE_DESKTOP |
| 169 | /* "dd --". NB: coreutils 6.9 will complain if they see |
| 170 | * more than one of them. We wouldn't. */ |
| 171 | if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0') |
| 172 | continue; |
| 173 | #endif |
| 174 | val = strchr(arg, '='); |
| 175 | if (val == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 176 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 177 | *val = '\0'; |
| 178 | what = index_in_strings(keywords, arg); |
| 179 | if (what < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 180 | bb_show_usage(); |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 181 | /* *val = '='; - to preserve ps listing? */ |
| 182 | val++; |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 183 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 184 | if (what == OP_ibs) { |
| 185 | /* Must fit into positive ssize_t */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 186 | ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); |
| 187 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 188 | } |
| 189 | if (what == OP_obs) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 190 | obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); |
| 191 | /*continue;*/ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 192 | } |
| 193 | if (what == OP_conv) { |
| 194 | while (1) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 195 | /* find ',', replace them with NUL so we can use val for |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 196 | * index_in_strings() without copying. |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 197 | * We rely on val being non-null, else strchr would fault. |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 198 | */ |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 199 | arg = strchr(val, ','); |
| 200 | if (arg) |
| 201 | *arg = '\0'; |
| 202 | what = index_in_strings(conv_words, val); |
| 203 | if (what < 0) |
| 204 | bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv"); |
| 205 | flags |= (1 << what); |
| 206 | if (!arg) /* no ',' left, so this was the last specifier */ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 207 | break; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 208 | /* *arg = ','; - to preserve ps listing? */ |
| 209 | val = arg + 1; /* skip this keyword and ',' */ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 210 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 211 | continue; /* we trashed 'what', can't fall through */ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 212 | } |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 213 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 214 | if (what == OP_bs) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 215 | ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes); |
| 216 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 217 | } |
| 218 | /* These can be large: */ |
| 219 | if (what == OP_count) { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 220 | flags |= FLAG_COUNT; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 221 | count = XATOU_SFX(val, dd_suffixes); |
| 222 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 223 | } |
| 224 | if (what == OP_seek) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 225 | seek = XATOU_SFX(val, dd_suffixes); |
| 226 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 227 | } |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 228 | if (what == OP_skip) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 229 | skip = XATOU_SFX(val, dd_suffixes); |
| 230 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 231 | } |
| 232 | if (what == OP_if) { |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 233 | infile = val; |
| 234 | /*continue;*/ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 235 | } |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 236 | if (what == OP_of) { |
| 237 | outfile = val; |
| 238 | /*continue;*/ |
| 239 | } |
| 240 | } /* end of "for (argv[n])" */ |
| 241 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 242 | //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 243 | ibuf = obuf = xmalloc(ibs); |
| 244 | if (ibs != obs) { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 245 | flags |= FLAG_TWOBUFS; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 246 | obuf = xmalloc(obs); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 247 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 248 | if (infile != NULL) |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 249 | xmove_fd(xopen(infile, O_RDONLY), ifd); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 250 | else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 251 | infile = bb_msg_standard_input; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 252 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 253 | if (outfile != NULL) { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 254 | int oflag = O_WRONLY | O_CREAT; |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 255 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 256 | if (!seek && !(flags & FLAG_NOTRUNC)) |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 257 | oflag |= O_TRUNC; |
| 258 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 259 | xmove_fd(xopen(outfile, oflag), ofd); |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 260 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 261 | if (seek && !(flags & FLAG_NOTRUNC)) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 262 | if (ftruncate(ofd, seek * obs) < 0) { |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 263 | struct stat st; |
| 264 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 265 | if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) || |
| 266 | S_ISDIR(st.st_mode)) |
| 267 | goto die_outfile; |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 268 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 269 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 270 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 271 | outfile = bb_msg_standard_output; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 272 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 273 | if (skip) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 274 | if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 275 | while (skip-- > 0) { |
| 276 | n = safe_read(ifd, ibuf, ibs); |
| 277 | if (n < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 278 | goto die_infile; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 279 | if (n == 0) |
| 280 | break; |
| 281 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 282 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 283 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 284 | if (seek) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 285 | if (lseek(ofd, seek * obs, SEEK_CUR) < 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 286 | goto die_outfile; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 287 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 288 | |
Denis Vlasenko | 2b03e5e | 2007-07-29 14:59:06 +0000 | [diff] [blame] | 289 | while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 290 | if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */ |
| 291 | memset(ibuf, 0, ibs); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 292 | n = safe_read(ifd, ibuf, ibs); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 293 | if (n == 0) |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 294 | break; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 295 | if (n < 0) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 296 | if (!(flags & FLAG_NOERROR)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 297 | goto die_infile; |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 298 | n = ibs; |
| 299 | bb_simple_perror_msg(infile); |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 300 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 301 | if ((size_t)n == ibs) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 302 | G.in_full++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 303 | else { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 304 | G.in_part++; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 305 | if (flags & FLAG_SYNC) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 306 | memset(ibuf + n, '\0', ibs - n); |
| 307 | n = ibs; |
| 308 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 309 | } |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 310 | if (flags & FLAG_TWOBUFS) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 311 | char *tmp = ibuf; |
| 312 | while (n) { |
| 313 | size_t d = obs - oc; |
| 314 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 315 | if (d > n) |
| 316 | d = n; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 317 | memcpy(obuf + oc, tmp, d); |
| 318 | n -= d; |
| 319 | tmp += d; |
| 320 | oc += d; |
| 321 | if (oc == obs) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 322 | if (write_and_stats(obuf, obs, obs, outfile)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 323 | goto out_status; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 324 | oc = 0; |
| 325 | } |
| 326 | } |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 327 | } else if (write_and_stats(ibuf, n, obs, outfile)) |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 328 | goto out_status; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 329 | |
| 330 | if (flags & FLAG_FSYNC) { |
| 331 | if (fsync(ofd) < 0) |
| 332 | goto die_outfile; |
| 333 | } |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 334 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 335 | |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 336 | if (ENABLE_FEATURE_DD_IBS_OBS && oc) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 337 | w = full_write_or_warn(obuf, oc, outfile); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 338 | if (w < 0) goto out_status; |
Denis Vlasenko | 7b6d9d6 | 2008-04-02 21:20:35 +0000 | [diff] [blame] | 339 | if (w > 0) G.out_part++; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 340 | } |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 341 | if (close(ifd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 342 | die_infile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 343 | bb_simple_perror_msg_and_die(infile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 344 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 345 | |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 346 | if (close(ofd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 347 | die_outfile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 348 | bb_simple_perror_msg_and_die(outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 349 | } |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 350 | |
| 351 | exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 352 | out_status: |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 353 | dd_output_status(0); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 354 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame] | 355 | return exitcode; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 356 | } |