blob: 4d1ef0b76434fe46ea5e71e0d11226574bd1d47e [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
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000011#include <signal.h> /* For FEATURE_DD_SIGNAL_HANDLING */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000012#include "libbb.h"
Denis Vlasenko99912ca2007-04-10 15:43:37 +000013
14/* This is a NOEXEC applet. Be very careful! */
15
Eric Andersencbe31da2001-02-20 06:14:08 +000016
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000017enum {
18 ifd = STDIN_FILENO,
19 ofd = STDOUT_FILENO,
20};
21
Matt Kraaia164c642001-02-05 17:50:03 +000022static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000023 { "c", 1 },
24 { "w", 2 },
25 { "b", 512 },
26 { "kD", 1000 },
27 { "k", 1024 },
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000028 { "K", 1024 }, /* compat with coreutils dd */
Matt Kraai24ac0172000-12-18 21:38:57 +000029 { "MD", 1000000 },
30 { "M", 1048576 },
31 { "GD", 1000000000 },
32 { "G", 1073741824 },
Denis Vlasenkof8689632007-07-27 15:06:25 +000033 { }
Matt Kraai24ac0172000-12-18 21:38:57 +000034};
35
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000036struct globals {
37 off_t out_full, out_part, in_full, in_part;
38};
39#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko75878702007-07-27 15:02:00 +000040/* We have to zero it out because of NOEXEC */
41#define INIT_G() memset(&G, 0, sizeof(G))
42
Rob Landleyc5598172006-05-02 22:44:04 +000043
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000044static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +000045{
Denis Vlasenko980864d2007-07-30 10:58:09 +000046 /* 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-Fischer2a47dea2007-04-04 14:01:23 +000049 G.in_full, G.in_part,
50 G.out_full, G.out_part);
Rob Landleyc5598172006-05-02 22:44:04 +000051}
52
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000053static ssize_t full_write_or_warn(const void *buf, size_t len,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000054 const char *const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000055{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000056 ssize_t n = full_write(ofd, buf, len);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000057 if (n < 0)
58 bb_perror_msg("writing '%s'", filename);
59 return n;
60}
61
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000062static bool write_and_stats(const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +000063 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000064{
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +000065 ssize_t n = full_write_or_warn(buf, len, filename);
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000066 if (n < 0)
67 return 1;
68 if (n == obs)
69 G.out_full++;
Denis Vlasenkod1801a42007-04-19 20:08:19 +000070 else if (n) /* > 0 */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000071 G.out_part++;
72 return 0;
73}
74
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000075#if ENABLE_LFS
76#define XATOU_SFX xatoull_sfx
77#else
78#define XATOU_SFX xatoul_sfx
79#endif
80
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000081int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +000082int dd_main(int argc ATTRIBUTE_UNUSED, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000083{
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000084 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +000085 /* 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 Vlasenko3b8ff682006-10-31 15:55:56 +000094 };
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000095 static const char keywords[] ALIGN1 =
Denis Vlasenko9d4a17e2008-04-23 05:44:59 +000096 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000097#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko9d4a17e2008-04-23 05:44:59 +000098 "ibs\0""obs\0""conv\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000099#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000100 ;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000101#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-Fischer99003b82007-04-10 19:30:50 +0000105 enum {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000106 OP_bs = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000107 OP_count,
108 OP_seek,
109 OP_skip,
110 OP_if,
111 OP_of,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000112#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000113 OP_ibs,
114 OP_obs,
115 OP_conv,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000116 /* Must be in the same order as FLAG_XXX! */
117 OP_conv_notrunc = 0,
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000118 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000119 OP_conv_noerror,
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000120 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-Fischere468ef22007-04-16 12:21:05 +0000133#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000134 };
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000135 int exitcode = EXIT_FAILURE;
Denis Vlasenko75878702007-07-27 15:02:00 +0000136 size_t ibs = 512, obs = 512;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000137 ssize_t n, w;
Rob Landley2686d3b2006-05-16 16:52:12 +0000138 char *ibuf, *obuf;
Denis Vlasenko75878702007-07-27 15:02:00 +0000139 /* And these are all zeroed at once! */
140 struct {
141 int flags;
Denis Vlasenko75878702007-07-27 15:02:00 +0000142 size_t oc;
143 off_t count;
144 off_t seek, skip;
145 const char *infile, *outfile;
Denis Vlasenko75878702007-07-27 15:02:00 +0000146 } Z;
147#define flags (Z.flags )
Denis Vlasenko75878702007-07-27 15:02:00 +0000148#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 Andersenddea3682000-11-29 22:33:02 +0000154
Denis Vlasenko75878702007-07-27 15:02:00 +0000155 memset(&Z, 0, sizeof(Z));
156 INIT_G();
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000157 //fflush(NULL); - is this needed because of NOEXEC?
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000158
Denis Vlasenko75878702007-07-27 15:02:00 +0000159#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000160 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
Denis Vlasenko75878702007-07-27 15:02:00 +0000161#endif
Rob Landleyc5598172006-05-02 22:44:04 +0000162
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000163 for (n = 1; argv[n]; n++) {
164 int what;
165 char *val;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000166 char *arg = argv[n];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000167
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000168#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 cad53642003-03-19 09:13:01 +0000176 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000177 *val = '\0';
178 what = index_in_strings(keywords, arg);
179 if (what < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000180 bb_show_usage();
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000181 /* *val = '='; - to preserve ps listing? */
182 val++;
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000183#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000184 if (what == OP_ibs) {
185 /* Must fit into positive ssize_t */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000186 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
187 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000188 }
189 if (what == OP_obs) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000190 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
191 /*continue;*/
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000192 }
193 if (what == OP_conv) {
194 while (1) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000195 /* find ',', replace them with NUL so we can use val for
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000196 * index_in_strings() without copying.
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000197 * We rely on val being non-null, else strchr would fault.
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000198 */
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000199 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 Vlasenko400d8bb2008-02-24 13:36:01 +0000207 break;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000208 /* *arg = ','; - to preserve ps listing? */
209 val = arg + 1; /* skip this keyword and ',' */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000210 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000211 continue; /* we trashed 'what', can't fall through */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000212 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000213#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000214 if (what == OP_bs) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000215 ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
216 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000217 }
218 /* These can be large: */
219 if (what == OP_count) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000220 flags |= FLAG_COUNT;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000221 count = XATOU_SFX(val, dd_suffixes);
222 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000223 }
224 if (what == OP_seek) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000225 seek = XATOU_SFX(val, dd_suffixes);
226 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000227 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000228 if (what == OP_skip) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000229 skip = XATOU_SFX(val, dd_suffixes);
230 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000231 }
232 if (what == OP_if) {
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000233 infile = val;
234 /*continue;*/
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000235 }
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000236 if (what == OP_of) {
237 outfile = val;
238 /*continue;*/
239 }
240 } /* end of "for (argv[n])" */
241
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000242//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000243 ibuf = obuf = xmalloc(ibs);
244 if (ibs != obs) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000245 flags |= FLAG_TWOBUFS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000246 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000247 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000248 if (infile != NULL)
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000249 xmove_fd(xopen(infile, O_RDONLY), ifd);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000250 else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000251 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000253 if (outfile != NULL) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000254 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000255
Denis Vlasenko75878702007-07-27 15:02:00 +0000256 if (!seek && !(flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000257 oflag |= O_TRUNC;
258
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000259 xmove_fd(xopen(outfile, oflag), ofd);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000260
Denis Vlasenko75878702007-07-27 15:02:00 +0000261 if (seek && !(flags & FLAG_NOTRUNC)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000262 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000263 struct stat st;
264
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000265 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
266 S_ISDIR(st.st_mode))
267 goto die_outfile;
Matt Kraaieb834782002-02-05 15:28:54 +0000268 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000269 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000270 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000271 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000273 if (skip) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000274 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000275 while (skip-- > 0) {
276 n = safe_read(ifd, ibuf, ibs);
277 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000278 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000279 if (n == 0)
280 break;
281 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000282 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000283 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000284 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000285 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000286 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000287 }
Eric Andersenddea3682000-11-29 22:33:02 +0000288
Denis Vlasenko2b03e5e2007-07-29 14:59:06 +0000289 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000290 if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */
291 memset(ibuf, 0, ibs);
Rob Landley2686d3b2006-05-16 16:52:12 +0000292 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000293 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000294 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000295 if (n < 0) {
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 n = ibs;
299 bb_simple_perror_msg(infile);
Eric Andersenef38b392002-04-27 01:31:43 +0000300 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000301 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000302 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000303 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000304 G.in_part++;
Denis Vlasenko75878702007-07-27 15:02:00 +0000305 if (flags & FLAG_SYNC) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000306 memset(ibuf + n, '\0', ibs - n);
307 n = ibs;
308 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000309 }
Denis Vlasenko75878702007-07-27 15:02:00 +0000310 if (flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000311 char *tmp = ibuf;
312 while (n) {
313 size_t d = obs - oc;
314
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000315 if (d > n)
316 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000317 memcpy(obuf + oc, tmp, d);
318 n -= d;
319 tmp += d;
320 oc += d;
321 if (oc == obs) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000322 if (write_and_stats(obuf, obs, obs, outfile))
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000323 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000324 oc = 0;
325 }
326 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000327 } else if (write_and_stats(ibuf, n, obs, outfile))
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000328 goto out_status;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000329
330 if (flags & FLAG_FSYNC) {
331 if (fsync(ofd) < 0)
332 goto die_outfile;
333 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000334 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000335
Rob Landley2686d3b2006-05-16 16:52:12 +0000336 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000337 w = full_write_or_warn(obuf, oc, outfile);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000338 if (w < 0) goto out_status;
Denis Vlasenko7b6d9d62008-04-02 21:20:35 +0000339 if (w > 0) G.out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000340 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000341 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000342 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000343 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000344 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000345
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000346 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000347 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000348 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000349 }
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000350
351 exitcode = EXIT_SUCCESS;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000352 out_status:
Rob Landleyc5598172006-05-02 22:44:04 +0000353 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000354
Denis Vlasenkoe2532ab2007-12-02 01:44:42 +0000355 return exitcode;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000356}