blob: c5c9476afc483e6dce789a264a271b977a98e45f [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Eric Andersenc4996011999-10-20 22:08:37 +00003 * Mini dd implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 *
Matt Kraai3eeab3b2001-12-07 16:27:37 +00006 * Copyright (C) 2000,2001 Matt Kraai
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +00009 */
10
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +000012
13/* This is a NOEXEC applet. Be very careful! */
14
Eric Andersencbe31da2001-02-20 06:14:08 +000015
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000016enum {
17 ifd = STDIN_FILENO,
18 ofd = STDOUT_FILENO,
19};
20
Matt Kraaia164c642001-02-05 17:50:03 +000021static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000022 { "c", 1 },
23 { "w", 2 },
24 { "b", 512 },
25 { "kD", 1000 },
26 { "k", 1024 },
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000027 { "K", 1024 }, /* compat with coreutils dd */
Matt Kraai24ac0172000-12-18 21:38:57 +000028 { "MD", 1000000 },
29 { "M", 1048576 },
30 { "GD", 1000000000 },
31 { "G", 1073741824 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +020032 { "", 0 }
Matt Kraai24ac0172000-12-18 21:38:57 +000033};
34
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000035struct globals {
36 off_t out_full, out_part, in_full, in_part;
37};
38#define G (*(struct globals*)&bb_common_bufsiz1)
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +020039#define INIT_G() do { \
40 /* we have to zero it out because of NOEXEC */ \
41 memset(&G, 0, sizeof(G)); \
42} while (0)
Denis Vlasenko75878702007-07-27 15:02:00 +000043
Rob Landleyc5598172006-05-02 22:44:04 +000044
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000045static void dd_output_status(int UNUSED_PARAM cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +000046{
Denis Vlasenko980864d2007-07-30 10:58:09 +000047 /* Deliberately using %u, not %d */
48 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
49 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000050 G.in_full, G.in_part,
51 G.out_full, G.out_part);
Rob Landleyc5598172006-05-02 22:44:04 +000052}
53
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000054static ssize_t full_write_or_warn(const void *buf, size_t len,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000055 const char *const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000056{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000057 ssize_t n = full_write(ofd, buf, len);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000058 if (n < 0)
59 bb_perror_msg("writing '%s'", filename);
60 return n;
61}
62
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000063static bool write_and_stats(const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +000064 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000065{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000066 ssize_t n = full_write_or_warn(buf, len, filename);
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000067 if (n < 0)
68 return 1;
Denis Vlasenko77ad97f2008-05-13 02:27:31 +000069 if ((size_t)n == obs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000070 G.out_full++;
Denis Vlasenkod1801a42007-04-19 20:08:19 +000071 else if (n) /* > 0 */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000072 G.out_part++;
73 return 0;
74}
75
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000076#if ENABLE_LFS
77#define XATOU_SFX xatoull_sfx
78#else
79#define XATOU_SFX xatoul_sfx
80#endif
81
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000082int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000083int dd_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000084{
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000085 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +000086 /* Must be in the same order as OP_conv_XXX! */
87 /* (see "flags |= (1 << what)" below) */
88 FLAG_NOTRUNC = 1 << 0,
89 FLAG_SYNC = 1 << 1,
90 FLAG_NOERROR = 1 << 2,
91 FLAG_FSYNC = 1 << 3,
92 /* end of conv flags */
93 FLAG_TWOBUFS = 1 << 4,
94 FLAG_COUNT = 1 << 5,
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000095 };
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000096 static const char keywords[] ALIGN1 =
Denis Vlasenko9d4a17e2008-04-23 05:44:59 +000097 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000098#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko9d4a17e2008-04-23 05:44:59 +000099 "ibs\0""obs\0""conv\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000100#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000101 ;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000102#if ENABLE_FEATURE_DD_IBS_OBS
103 static const char conv_words[] ALIGN1 =
104 "notrunc\0""sync\0""noerror\0""fsync\0";
105#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000106 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000107 OP_bs = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000108 OP_count,
109 OP_seek,
110 OP_skip,
111 OP_if,
112 OP_of,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000113#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000114 OP_ibs,
115 OP_obs,
116 OP_conv,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000117 /* Must be in the same order as FLAG_XXX! */
118 OP_conv_notrunc = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000119 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000120 OP_conv_noerror,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000121 OP_conv_fsync,
122 /* Unimplemented conv=XXX: */
123 //nocreat do not create the output file
124 //excl fail if the output file already exists
125 //fdatasync physically write output file data before finishing
126 //swab swap every pair of input bytes
127 //lcase change upper case to lower case
128 //ucase change lower case to upper case
129 //block pad newline-terminated records with spaces to cbs-size
130 //unblock replace trailing spaces in cbs-size records with newline
131 //ascii from EBCDIC to ASCII
132 //ebcdic from ASCII to EBCDIC
133 //ibm from ASCII to alternate EBCDIC
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000134#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000135 };
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000136 int exitcode = EXIT_FAILURE;
Denis Vlasenko75878702007-07-27 15:02:00 +0000137 size_t ibs = 512, obs = 512;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000138 ssize_t n, w;
Rob Landley2686d3b2006-05-16 16:52:12 +0000139 char *ibuf, *obuf;
Denis Vlasenko75878702007-07-27 15:02:00 +0000140 /* And these are all zeroed at once! */
141 struct {
142 int flags;
Denis Vlasenko75878702007-07-27 15:02:00 +0000143 size_t oc;
144 off_t count;
145 off_t seek, skip;
146 const char *infile, *outfile;
Denis Vlasenko75878702007-07-27 15:02:00 +0000147 } Z;
148#define flags (Z.flags )
Denis Vlasenko75878702007-07-27 15:02:00 +0000149#define oc (Z.oc )
150#define count (Z.count )
151#define seek (Z.seek )
152#define skip (Z.skip )
153#define infile (Z.infile )
154#define outfile (Z.outfile)
Eric Andersenddea3682000-11-29 22:33:02 +0000155
Denis Vlasenko75878702007-07-27 15:02:00 +0000156 memset(&Z, 0, sizeof(Z));
157 INIT_G();
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000158 //fflush(NULL); - is this needed because of NOEXEC?
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000159
Denis Vlasenko75878702007-07-27 15:02:00 +0000160#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000161 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
Denis Vlasenko75878702007-07-27 15:02:00 +0000162#endif
Rob Landleyc5598172006-05-02 22:44:04 +0000163
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000164 for (n = 1; argv[n]; n++) {
165 int what;
166 char *val;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000167 char *arg = argv[n];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000168
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000169#if ENABLE_DESKTOP
170 /* "dd --". NB: coreutils 6.9 will complain if they see
171 * more than one of them. We wouldn't. */
172 if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0')
173 continue;
174#endif
175 val = strchr(arg, '=');
176 if (val == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000178 *val = '\0';
179 what = index_in_strings(keywords, arg);
180 if (what < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000181 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000182 /* *val = '='; - to preserve ps listing? */
183 val++;
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000184#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000185 if (what == OP_ibs) {
186 /* Must fit into positive ssize_t */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000187 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
188 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000189 }
190 if (what == OP_obs) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000191 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
192 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000193 }
194 if (what == OP_conv) {
195 while (1) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000196 /* find ',', replace them with NUL so we can use val for
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000197 * index_in_strings() without copying.
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000198 * We rely on val being non-null, else strchr would fault.
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000199 */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000200 arg = strchr(val, ',');
201 if (arg)
202 *arg = '\0';
203 what = index_in_strings(conv_words, val);
204 if (what < 0)
205 bb_error_msg_and_die(bb_msg_invalid_arg, val, "conv");
206 flags |= (1 << what);
207 if (!arg) /* no ',' left, so this was the last specifier */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000208 break;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000209 /* *arg = ','; - to preserve ps listing? */
210 val = arg + 1; /* skip this keyword and ',' */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000211 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000212 continue; /* we trashed 'what', can't fall through */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000213 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000214#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000215 if (what == OP_bs) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000216 ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
217 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000218 }
219 /* These can be large: */
220 if (what == OP_count) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000221 flags |= FLAG_COUNT;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000222 count = XATOU_SFX(val, dd_suffixes);
223 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000224 }
225 if (what == OP_seek) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000226 seek = XATOU_SFX(val, dd_suffixes);
227 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000228 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000229 if (what == OP_skip) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000230 skip = XATOU_SFX(val, dd_suffixes);
231 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000232 }
233 if (what == OP_if) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000234 infile = val;
235 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000236 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000237 if (what == OP_of) {
238 outfile = val;
239 /*continue;*/
240 }
241 } /* end of "for (argv[n])" */
242
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000243//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000244 ibuf = obuf = xmalloc(ibs);
245 if (ibs != obs) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000246 flags |= FLAG_TWOBUFS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000247 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000248 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000249 if (infile != NULL)
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000250 xmove_fd(xopen(infile, O_RDONLY), ifd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000251 else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000252 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000254 if (outfile != NULL) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000255 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000256
Denis Vlasenko75878702007-07-27 15:02:00 +0000257 if (!seek && !(flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000258 oflag |= O_TRUNC;
259
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000260 xmove_fd(xopen(outfile, oflag), ofd);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000261
Denis Vlasenko75878702007-07-27 15:02:00 +0000262 if (seek && !(flags & FLAG_NOTRUNC)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000263 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000264 struct stat st;
265
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000266 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
267 S_ISDIR(st.st_mode))
268 goto die_outfile;
Matt Kraaieb834782002-02-05 15:28:54 +0000269 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000270 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000271 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000272 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000274 if (skip) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000275 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000276 while (skip-- > 0) {
277 n = safe_read(ifd, ibuf, ibs);
278 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000279 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000280 if (n == 0)
281 break;
282 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000283 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000284 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000285 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000286 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000287 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000288 }
Eric Andersenddea3682000-11-29 22:33:02 +0000289
Denis Vlasenko2b03e5e2007-07-29 14:59:06 +0000290 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000291 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000292 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000293 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000294 if (n < 0) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200295 /* "Bad block" */
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000296 if (!(flags & FLAG_NOERROR))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000297 goto die_infile;
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000298 bb_simple_perror_msg(infile);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200299 /* GNU dd with conv=noerror skips over bad blocks */
Denys Vlasenko334fcc82009-08-05 23:16:27 +0200300 xlseek(ifd, ibs, SEEK_CUR);
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200301 /* conv=noerror,sync writes NULs,
302 * conv=noerror just ignores input bad blocks */
303 n = 0;
Eric Andersenef38b392002-04-27 01:31:43 +0000304 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000305 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000306 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000307 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000308 G.in_part++;
Denis Vlasenko75878702007-07-27 15:02:00 +0000309 if (flags & FLAG_SYNC) {
Denys Vlasenkof00cfdf2009-09-18 20:49:36 +0200310 memset(ibuf + n, 0, ibs - n);
Rob Landley2686d3b2006-05-16 16:52:12 +0000311 n = ibs;
312 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000313 }
Denis Vlasenko75878702007-07-27 15:02:00 +0000314 if (flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000315 char *tmp = ibuf;
316 while (n) {
317 size_t d = obs - oc;
318
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000319 if (d > (size_t)n)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000320 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000321 memcpy(obuf + oc, tmp, d);
322 n -= d;
323 tmp += d;
324 oc += d;
325 if (oc == obs) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000326 if (write_and_stats(obuf, obs, obs, outfile))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000327 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000328 oc = 0;
329 }
330 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000331 } else if (write_and_stats(ibuf, n, obs, outfile))
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000332 goto out_status;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000333
334 if (flags & FLAG_FSYNC) {
335 if (fsync(ofd) < 0)
336 goto die_outfile;
337 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000338 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000339
Rob Landley2686d3b2006-05-16 16:52:12 +0000340 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000341 w = full_write_or_warn(obuf, oc, outfile);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000342 if (w < 0) goto out_status;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000343 if (w > 0) G.out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000344 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000345 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000346 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000347 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000348 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000349
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000350 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000351 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000352 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000353 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000354
355 exitcode = EXIT_SUCCESS;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000356 out_status:
Rob Landleyc5598172006-05-02 22:44:04 +0000357 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000358
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000359 return exitcode;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000360}