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; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 82 | int dd_main(int argc, 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 | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 85 | FLAG_SYNC = 1 << 0, |
| 86 | FLAG_NOERROR = 1 << 1, |
| 87 | FLAG_NOTRUNC = 1 << 2, |
| 88 | FLAG_TWOBUFS = 1 << 3, |
| 89 | FLAG_COUNT = 1 << 4, |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 90 | }; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 91 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 92 | "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] | 93 | #if ENABLE_FEATURE_DD_IBS_OBS |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 94 | "ibs=\0""obs=\0""conv=\0""notrunc\0""sync\0""noerror\0" |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 95 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 96 | ; |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 97 | enum { |
| 98 | OP_bs = 1, |
| 99 | OP_count, |
| 100 | OP_seek, |
| 101 | OP_skip, |
| 102 | OP_if, |
| 103 | OP_of, |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 104 | #if ENABLE_FEATURE_DD_IBS_OBS |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 105 | OP_ibs, |
| 106 | OP_obs, |
| 107 | OP_conv, |
| 108 | OP_conv_notrunc, |
| 109 | OP_conv_sync, |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 110 | OP_conv_noerror, |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 111 | #endif |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 112 | }; |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 113 | int exitcode = EXIT_FAILURE; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 114 | size_t ibs = 512, obs = 512; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 115 | ssize_t n, w; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 116 | char *ibuf, *obuf; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 117 | /* And these are all zeroed at once! */ |
| 118 | struct { |
| 119 | int flags; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 120 | size_t oc; |
| 121 | off_t count; |
| 122 | off_t seek, skip; |
| 123 | const char *infile, *outfile; |
| 124 | #if ENABLE_FEATURE_DD_SIGNAL_HANDLING |
| 125 | struct sigaction sigact; |
| 126 | #endif |
| 127 | } Z; |
| 128 | #define flags (Z.flags ) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 129 | #define oc (Z.oc ) |
| 130 | #define count (Z.count ) |
| 131 | #define seek (Z.seek ) |
| 132 | #define skip (Z.skip ) |
| 133 | #define infile (Z.infile ) |
| 134 | #define outfile (Z.outfile) |
| 135 | #define sigact (Z.sigact ) |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 137 | memset(&Z, 0, sizeof(Z)); |
| 138 | INIT_G(); |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 139 | //fflush(NULL); - is this needed because of NOEXEC? |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 141 | #if ENABLE_FEATURE_DD_SIGNAL_HANDLING |
| 142 | sigact.sa_handler = dd_output_status; |
| 143 | sigact.sa_flags = SA_RESTART; |
| 144 | sigemptyset(&sigact.sa_mask); |
| 145 | sigaction(SIGUSR1, &sigact, NULL); |
| 146 | #endif |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 147 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 148 | for (n = 1; n < argc; n++) { |
Bernhard Reutner-Fischer | 99003b8 | 2007-04-10 19:30:50 +0000 | [diff] [blame] | 149 | smalluint key_len; |
| 150 | smalluint what; |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 151 | char *key; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 152 | char *arg = argv[n]; |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 153 | |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 154 | //XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so.. |
| 155 | //if (*arg == '-' && *++arg == '-' && !*++arg) continue; |
| 156 | key = strstr(arg, "="); |
| 157 | if (key == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 158 | bb_show_usage(); |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 159 | key_len = key - arg + 1; |
| 160 | key = xstrndup(arg, key_len); |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 161 | what = index_in_strings(keywords, key) + 1; |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 162 | if (ENABLE_FEATURE_CLEAN_UP) |
| 163 | free(key); |
| 164 | if (what == 0) |
| 165 | bb_show_usage(); |
| 166 | arg += key_len; |
| 167 | /* Must fit into positive ssize_t */ |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 168 | #if ENABLE_FEATURE_DD_IBS_OBS |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 169 | if (what == OP_ibs) { |
| 170 | ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes); |
| 171 | continue; |
| 172 | } |
| 173 | if (what == OP_obs) { |
| 174 | obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes); |
| 175 | continue; |
| 176 | } |
| 177 | if (what == OP_conv) { |
| 178 | while (1) { |
| 179 | /* find ',', replace them with nil so we can use arg for |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 180 | * index_in_strings() without copying. |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 181 | * We rely on arg being non-null, else strchr would fault. |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 182 | */ |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 183 | key = strchr(arg, ','); |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 184 | if (key) |
| 185 | *key = '\0'; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 186 | what = index_in_strings(keywords, arg) + 1; |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 187 | if (what < OP_conv_notrunc) |
| 188 | bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv"); |
| 189 | if (what == OP_conv_notrunc) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 190 | flags |= FLAG_NOTRUNC; |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 191 | if (what == OP_conv_sync) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 192 | flags |= FLAG_SYNC; |
Denis Vlasenko | f6250a3 | 2007-04-19 20:16:14 +0000 | [diff] [blame] | 193 | if (what == OP_conv_noerror) |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 194 | flags |= FLAG_NOERROR; |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 195 | if (!key) /* no ',' left, so this was the last specifier */ |
| 196 | break; |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 197 | arg = key + 1; /* skip this keyword and ',' */ |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 198 | } |
| 199 | continue; |
| 200 | } |
Bernhard Reutner-Fischer | e468ef2 | 2007-04-16 12:21:05 +0000 | [diff] [blame] | 201 | #endif |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 202 | if (what == OP_bs) { |
| 203 | ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes); |
| 204 | continue; |
| 205 | } |
| 206 | /* These can be large: */ |
| 207 | if (what == OP_count) { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 208 | flags |= FLAG_COUNT; |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 209 | count = XATOU_SFX(arg, dd_suffixes); |
| 210 | continue; |
| 211 | } |
| 212 | if (what == OP_seek) { |
| 213 | seek = XATOU_SFX(arg, dd_suffixes); |
| 214 | continue; |
| 215 | } |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 216 | if (what == OP_skip) { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 217 | skip = XATOU_SFX(arg, dd_suffixes); |
| 218 | continue; |
| 219 | } |
| 220 | if (what == OP_if) { |
| 221 | infile = arg; |
| 222 | continue; |
| 223 | } |
| 224 | if (what == OP_of) |
| 225 | outfile = arg; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 226 | } |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 227 | //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] | 228 | ibuf = obuf = xmalloc(ibs); |
| 229 | if (ibs != obs) { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 230 | flags |= FLAG_TWOBUFS; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 231 | obuf = xmalloc(obs); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 232 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 233 | if (infile != NULL) |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 234 | xmove_fd(xopen(infile, O_RDONLY), ifd); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 235 | else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 236 | infile = bb_msg_standard_input; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 237 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 238 | if (outfile != NULL) { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 239 | int oflag = O_WRONLY | O_CREAT; |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 240 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 241 | if (!seek && !(flags & FLAG_NOTRUNC)) |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 242 | oflag |= O_TRUNC; |
| 243 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 244 | xmove_fd(xopen(outfile, oflag), ofd); |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 245 | |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 246 | if (seek && !(flags & FLAG_NOTRUNC)) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 247 | if (ftruncate(ofd, seek * obs) < 0) { |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 248 | struct stat st; |
| 249 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 250 | if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) || |
| 251 | S_ISDIR(st.st_mode)) |
| 252 | goto die_outfile; |
Matt Kraai | eb83478 | 2002-02-05 15:28:54 +0000 | [diff] [blame] | 253 | } |
Matt Kraai | c9acf8c | 2001-01-17 00:21:05 +0000 | [diff] [blame] | 254 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 255 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 256 | outfile = bb_msg_standard_output; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 257 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 258 | if (skip) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 259 | if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 260 | while (skip-- > 0) { |
| 261 | n = safe_read(ifd, ibuf, ibs); |
| 262 | if (n < 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 263 | goto die_infile; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 264 | if (n == 0) |
| 265 | break; |
| 266 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 267 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 268 | } |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 269 | if (seek) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 270 | if (lseek(ofd, seek * obs, SEEK_CUR) < 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 271 | goto die_outfile; |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 272 | } |
Eric Andersen | ddea368 | 2000-11-29 22:33:02 +0000 | [diff] [blame] | 273 | |
Denis Vlasenko | 2b03e5e | 2007-07-29 14:59:06 +0000 | [diff] [blame] | 274 | while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) { |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 275 | if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */ |
| 276 | memset(ibuf, 0, ibs); |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 277 | n = safe_read(ifd, ibuf, ibs); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 278 | if (n == 0) |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 279 | break; |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 280 | if (n < 0) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 281 | if (!(flags & FLAG_NOERROR)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 282 | goto die_infile; |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 283 | n = ibs; |
| 284 | bb_simple_perror_msg(infile); |
Eric Andersen | ef38b39 | 2002-04-27 01:31:43 +0000 | [diff] [blame] | 285 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 286 | if ((size_t)n == ibs) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 287 | G.in_full++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 288 | else { |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 289 | G.in_part++; |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 290 | if (flags & FLAG_SYNC) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 291 | memset(ibuf + n, '\0', ibs - n); |
| 292 | n = ibs; |
| 293 | } |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 294 | } |
Denis Vlasenko | 7587870 | 2007-07-27 15:02:00 +0000 | [diff] [blame] | 295 | if (flags & FLAG_TWOBUFS) { |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 296 | char *tmp = ibuf; |
| 297 | while (n) { |
| 298 | size_t d = obs - oc; |
| 299 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 300 | if (d > n) |
| 301 | d = n; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 302 | memcpy(obuf + oc, tmp, d); |
| 303 | n -= d; |
| 304 | tmp += d; |
| 305 | oc += d; |
| 306 | if (oc == obs) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 307 | if (write_and_stats(obuf, obs, obs, outfile)) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 308 | goto out_status; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 309 | oc = 0; |
| 310 | } |
| 311 | } |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 312 | } else if (write_and_stats(ibuf, n, obs, outfile)) |
Denis Vlasenko | d1801a4 | 2007-04-19 20:08:19 +0000 | [diff] [blame] | 313 | goto out_status; |
Glenn L McGrath | f0b073f | 2000-09-11 00:32:13 +0000 | [diff] [blame] | 314 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 315 | |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 316 | if (ENABLE_FEATURE_DD_IBS_OBS && oc) { |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 317 | w = full_write_or_warn(obuf, oc, outfile); |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 318 | if (w < 0) goto out_status; |
| 319 | if (w > 0) |
Bernhard Reutner-Fischer | 2a47dea | 2007-04-04 14:01:23 +0000 | [diff] [blame] | 320 | G.out_part++; |
Rob Landley | 2686d3b | 2006-05-16 16:52:12 +0000 | [diff] [blame] | 321 | } |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 322 | if (close(ifd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 323 | die_infile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 324 | bb_simple_perror_msg_and_die(infile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 325 | } |
Matt Kraai | 3eeab3b | 2001-12-07 16:27:37 +0000 | [diff] [blame] | 326 | |
Denis Vlasenko | 3b8ff68 | 2006-10-31 15:55:56 +0000 | [diff] [blame] | 327 | if (close(ofd) < 0) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 328 | die_outfile: |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 329 | bb_simple_perror_msg_and_die(outfile); |
Glenn L McGrath | eaed78a | 2002-11-28 11:05:28 +0000 | [diff] [blame] | 330 | } |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 331 | |
| 332 | exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 333 | out_status: |
Rob Landley | c559817 | 2006-05-02 22:44:04 +0000 | [diff] [blame] | 334 | dd_output_status(0); |
Matt Kraai | 24ac017 | 2000-12-18 21:38:57 +0000 | [diff] [blame] | 335 | |
Denis Vlasenko | e2532ab | 2007-12-02 01:44:42 +0000 | [diff] [blame^] | 336 | return exitcode; |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 337 | } |