blob: fd4e7e8a2a0fcf334c57c076b0c29d4183ffe147 [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
Matt Kraaia164c642001-02-05 17:50:03 +000017static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000018 { "c", 1 },
19 { "w", 2 },
20 { "b", 512 },
21 { "kD", 1000 },
22 { "k", 1024 },
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000023 { "K", 1024 }, /* compat with coreutils dd */
Matt Kraai24ac0172000-12-18 21:38:57 +000024 { "MD", 1000000 },
25 { "M", 1048576 },
26 { "GD", 1000000000 },
27 { "G", 1073741824 },
Denis Vlasenkof8689632007-07-27 15:06:25 +000028 { }
Matt Kraai24ac0172000-12-18 21:38:57 +000029};
30
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000031struct globals {
32 off_t out_full, out_part, in_full, in_part;
33};
34#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko75878702007-07-27 15:02:00 +000035/* We have to zero it out because of NOEXEC */
36#define INIT_G() memset(&G, 0, sizeof(G))
37
Rob Landleyc5598172006-05-02 22:44:04 +000038
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000039static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +000040{
Denis Vlasenko980864d2007-07-30 10:58:09 +000041 /* Deliberately using %u, not %d */
42 fprintf(stderr, "%"OFF_FMT"u+%"OFF_FMT"u records in\n"
43 "%"OFF_FMT"u+%"OFF_FMT"u records out\n",
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000044 G.in_full, G.in_part,
45 G.out_full, G.out_part);
Rob Landleyc5598172006-05-02 22:44:04 +000046}
47
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000048static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000049 const char *const filename)
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000050{
51 ssize_t n = full_write(fd, buf, len);
52 if (n < 0)
53 bb_perror_msg("writing '%s'", filename);
54 return n;
55}
56
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000057static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
Denis Vlasenko990d0f62007-07-24 15:54:42 +000058 const char *filename)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000059{
60 ssize_t n = full_write_or_warn(fd, buf, len, filename);
61 if (n < 0)
62 return 1;
63 if (n == obs)
64 G.out_full++;
Denis Vlasenkod1801a42007-04-19 20:08:19 +000065 else if (n) /* > 0 */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000066 G.out_part++;
67 return 0;
68}
69
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000070#if ENABLE_LFS
71#define XATOU_SFX xatoull_sfx
72#else
73#define XATOU_SFX xatoul_sfx
74#endif
75
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000076int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Matt Kraai24ac0172000-12-18 21:38:57 +000077int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000078{
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000079 enum {
Denis Vlasenko75878702007-07-27 15:02:00 +000080 FLAG_SYNC = 1 << 0,
81 FLAG_NOERROR = 1 << 1,
82 FLAG_NOTRUNC = 1 << 2,
83 FLAG_TWOBUFS = 1 << 3,
84 FLAG_COUNT = 1 << 4,
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000085 };
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000086 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +000087 "bs=\0""count=\0""seek=\0""skip=\0""if=\0""of=\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000088#if ENABLE_FEATURE_DD_IBS_OBS
Denis Vlasenko990d0f62007-07-24 15:54:42 +000089 "ibs=\0""obs=\0""conv=\0""notrunc\0""sync\0""noerror\0"
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +000090#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +000091 ;
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +000092 enum {
93 OP_bs = 1,
94 OP_count,
95 OP_seek,
96 OP_skip,
97 OP_if,
98 OP_of,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +000099#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000100 OP_ibs,
101 OP_obs,
102 OP_conv,
103 OP_conv_notrunc,
104 OP_conv_sync,
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000105 OP_conv_noerror,
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000106#endif
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000107 };
Denis Vlasenko75878702007-07-27 15:02:00 +0000108 size_t ibs = 512, obs = 512;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000109 ssize_t n, w;
Rob Landley2686d3b2006-05-16 16:52:12 +0000110 char *ibuf, *obuf;
Denis Vlasenko75878702007-07-27 15:02:00 +0000111 /* And these are all zeroed at once! */
112 struct {
113 int flags;
114 int ifd, ofd;
115 size_t oc;
116 off_t count;
117 off_t seek, skip;
118 const char *infile, *outfile;
119#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
120 struct sigaction sigact;
121#endif
122 } Z;
123#define flags (Z.flags )
124#define ifd (Z.ifd )
125#define ofd (Z.ofd )
126#define oc (Z.oc )
127#define count (Z.count )
128#define seek (Z.seek )
129#define skip (Z.skip )
130#define infile (Z.infile )
131#define outfile (Z.outfile)
132#define sigact (Z.sigact )
Eric Andersenddea3682000-11-29 22:33:02 +0000133
Denis Vlasenko75878702007-07-27 15:02:00 +0000134 memset(&Z, 0, sizeof(Z));
135 INIT_G();
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000136
Denis Vlasenko75878702007-07-27 15:02:00 +0000137#if ENABLE_FEATURE_DD_SIGNAL_HANDLING
138 sigact.sa_handler = dd_output_status;
139 sigact.sa_flags = SA_RESTART;
140 sigemptyset(&sigact.sa_mask);
141 sigaction(SIGUSR1, &sigact, NULL);
142#endif
Rob Landleyc5598172006-05-02 22:44:04 +0000143
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000144 for (n = 1; n < argc; n++) {
Bernhard Reutner-Fischer99003b82007-04-10 19:30:50 +0000145 smalluint key_len;
146 smalluint what;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000147 char *key;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000148 char *arg = argv[n];
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000149
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000150//XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
151//if (*arg == '-' && *++arg == '-' && !*++arg) continue;
152 key = strstr(arg, "=");
153 if (key == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154 bb_show_usage();
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000155 key_len = key - arg + 1;
156 key = xstrndup(arg, key_len);
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000157 what = index_in_strings(keywords, key) + 1;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000158 if (ENABLE_FEATURE_CLEAN_UP)
159 free(key);
160 if (what == 0)
161 bb_show_usage();
162 arg += key_len;
163 /* Must fit into positive ssize_t */
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000164#if ENABLE_FEATURE_DD_IBS_OBS
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000165 if (what == OP_ibs) {
166 ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
167 continue;
168 }
169 if (what == OP_obs) {
170 obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
171 continue;
172 }
173 if (what == OP_conv) {
174 while (1) {
175 /* find ',', replace them with nil so we can use arg for
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000176 * index_in_strings() without copying.
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000177 * We rely on arg being non-null, else strchr would fault.
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000178 */
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000179 key = strchr(arg, ',');
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000180 if (key)
181 *key = '\0';
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000182 what = index_in_strings(keywords, arg) + 1;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000183 if (what < OP_conv_notrunc)
184 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
185 if (what == OP_conv_notrunc)
Denis Vlasenko75878702007-07-27 15:02:00 +0000186 flags |= FLAG_NOTRUNC;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000187 if (what == OP_conv_sync)
Denis Vlasenko75878702007-07-27 15:02:00 +0000188 flags |= FLAG_SYNC;
Denis Vlasenkof6250a32007-04-19 20:16:14 +0000189 if (what == OP_conv_noerror)
Denis Vlasenko75878702007-07-27 15:02:00 +0000190 flags |= FLAG_NOERROR;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000191 if (!key) /* no ',' left, so this was the last specifier */
192 break;
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000193 arg = key + 1; /* skip this keyword and ',' */
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000194 }
195 continue;
196 }
Bernhard Reutner-Fischere468ef22007-04-16 12:21:05 +0000197#endif
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000198 if (what == OP_bs) {
199 ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
200 continue;
201 }
202 /* These can be large: */
203 if (what == OP_count) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000204 flags |= FLAG_COUNT;
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000205 count = XATOU_SFX(arg, dd_suffixes);
206 continue;
207 }
208 if (what == OP_seek) {
209 seek = XATOU_SFX(arg, dd_suffixes);
210 continue;
211 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000212 if (what == OP_skip) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000213 skip = XATOU_SFX(arg, dd_suffixes);
214 continue;
215 }
216 if (what == OP_if) {
217 infile = arg;
218 continue;
219 }
220 if (what == OP_of)
221 outfile = arg;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000222 }
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000223//XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000224 ibuf = obuf = xmalloc(ibs);
225 if (ibs != obs) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000226 flags |= FLAG_TWOBUFS;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000227 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000228 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000229 if (infile != NULL)
Denis Vlasenko7039a662006-10-08 17:54:47 +0000230 ifd = xopen(infile, O_RDONLY);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000231 else {
Denis Vlasenko75878702007-07-27 15:02:00 +0000232 /* ifd = STDIN_FILENO; - it's zero and it's already there */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000234 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000235 if (outfile != NULL) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000236 int oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000237
Denis Vlasenko75878702007-07-27 15:02:00 +0000238 if (!seek && !(flags & FLAG_NOTRUNC))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000239 oflag |= O_TRUNC;
240
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000241 ofd = xopen(outfile, oflag);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000242
Denis Vlasenko75878702007-07-27 15:02:00 +0000243 if (seek && !(flags & FLAG_NOTRUNC)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000244 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000245 struct stat st;
246
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000247 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
248 S_ISDIR(st.st_mode))
249 goto die_outfile;
Matt Kraaieb834782002-02-05 15:28:54 +0000250 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000251 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000252 } else {
253 ofd = STDOUT_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000254 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000256 if (skip) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000257 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000258 while (skip-- > 0) {
259 n = safe_read(ifd, ibuf, ibs);
260 if (n < 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000261 goto die_infile;
Rob Landley2686d3b2006-05-16 16:52:12 +0000262 if (n == 0)
263 break;
264 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000265 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000266 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000267 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000268 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000269 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000270 }
Eric Andersenddea3682000-11-29 22:33:02 +0000271
Denis Vlasenko2b03e5e2007-07-29 14:59:06 +0000272 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000273 if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */
274 memset(ibuf, 0, ibs);
Rob Landley2686d3b2006-05-16 16:52:12 +0000275 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000276 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000277 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000278 if (n < 0) {
Denis Vlasenko75878702007-07-27 15:02:00 +0000279 if (flags & FLAG_NOERROR) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000280 n = ibs;
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000281 bb_simple_perror_msg(infile);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000282 } else
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000283 goto die_infile;
Eric Andersenef38b392002-04-27 01:31:43 +0000284 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000285 if ((size_t)n == ibs)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000286 G.in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000287 else {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000288 G.in_part++;
Denis Vlasenko75878702007-07-27 15:02:00 +0000289 if (flags & FLAG_SYNC) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000290 memset(ibuf + n, '\0', ibs - n);
291 n = ibs;
292 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000293 }
Denis Vlasenko75878702007-07-27 15:02:00 +0000294 if (flags & FLAG_TWOBUFS) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000295 char *tmp = ibuf;
296 while (n) {
297 size_t d = obs - oc;
298
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000299 if (d > n)
300 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000301 memcpy(obuf + oc, tmp, d);
302 n -= d;
303 tmp += d;
304 oc += d;
305 if (oc == obs) {
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000306 if (write_and_stats(ofd, obuf, obs, obs, outfile))
307 goto out_status;
Rob Landley2686d3b2006-05-16 16:52:12 +0000308 oc = 0;
309 }
310 }
Denis Vlasenkod1801a42007-04-19 20:08:19 +0000311 } else if (write_and_stats(ofd, ibuf, n, obs, outfile))
312 goto out_status;
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000313 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000314
Rob Landley2686d3b2006-05-16 16:52:12 +0000315 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000316 w = full_write_or_warn(ofd, obuf, oc, outfile);
317 if (w < 0) goto out_status;
318 if (w > 0)
Bernhard Reutner-Fischer2a47dea2007-04-04 14:01:23 +0000319 G.out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000320 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000321 if (close(ifd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000322 die_infile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000323 bb_simple_perror_msg_and_die(infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000324 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000325
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000326 if (close(ofd) < 0) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000327 die_outfile:
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000328 bb_simple_perror_msg_and_die(outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000329 }
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000330 out_status:
Rob Landleyc5598172006-05-02 22:44:04 +0000331 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000332
Matt Kraai3e856ce2000-12-01 02:55:13 +0000333 return EXIT_SUCCESS;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000334}