1 /* vi: set sw=4 ts=4: */
3 * Mini dd implementation for busybox
6 * Copyright (C) 2000,2001 Matt Kraai
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 #include <signal.h> /* For FEATURE_DD_SIGNAL_HANDLING */
14 /* This is a NOEXEC applet. Be very careful! */
22 static const struct suffix_mult dd_suffixes[] = {
28 { "K", 1024 }, /* compat with coreutils dd */
37 off_t out_full, out_part, in_full, in_part;
39 #define G (*(struct globals*)&bb_common_bufsiz1)
40 /* We have to zero it out because of NOEXEC */
41 #define INIT_G() memset(&G, 0, sizeof(G))
44 static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
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",
50 G.out_full, G.out_part);
53 static ssize_t full_write_or_warn(const void *buf, size_t len,
54 const char *const filename)
56 ssize_t n = full_write(ofd, buf, len);
58 bb_perror_msg("writing '%s'", filename);
62 static bool write_and_stats(const void *buf, size_t len, size_t obs,
65 ssize_t n = full_write_or_warn(buf, len, filename);
76 #define XATOU_SFX xatoull_sfx
78 #define XATOU_SFX xatoul_sfx
81 int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
82 int dd_main(int argc, char **argv)
86 FLAG_NOERROR = 1 << 1,
87 FLAG_NOTRUNC = 1 << 2,
88 FLAG_TWOBUFS = 1 << 3,
91 static const char keywords[] ALIGN1 =
92 "bs=\0""count=\0""seek=\0""skip=\0""if=\0""of=\0"
93 #if ENABLE_FEATURE_DD_IBS_OBS
94 "ibs=\0""obs=\0""conv=\0""notrunc\0""sync\0""noerror\0"
104 #if ENABLE_FEATURE_DD_IBS_OBS
113 int exitcode = EXIT_FAILURE;
114 size_t ibs = 512, obs = 512;
117 /* And these are all zeroed at once! */
123 const char *infile, *outfile;
125 #define flags (Z.flags )
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)
133 memset(&Z, 0, sizeof(Z));
135 //fflush(NULL); - is this needed because of NOEXEC?
137 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
138 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
141 for (n = 1; n < argc; n++) {
147 //XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
148 //if (*arg == '-' && *++arg == '-' && !*++arg) continue;
149 key = strstr(arg, "=");
152 key_len = key - arg + 1;
153 key = xstrndup(arg, key_len);
154 what = index_in_strings(keywords, key) + 1;
155 if (ENABLE_FEATURE_CLEAN_UP)
160 #if ENABLE_FEATURE_DD_IBS_OBS
161 if (what == OP_ibs) {
162 /* Must fit into positive ssize_t */
163 ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
166 if (what == OP_obs) {
167 obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
170 if (what == OP_conv) {
172 /* find ',', replace them with NUL so we can use arg for
173 * index_in_strings() without copying.
174 * We rely on arg being non-null, else strchr would fault.
176 key = strchr(arg, ',');
179 what = index_in_strings(keywords, arg) + 1;
180 if (what < OP_conv_notrunc)
181 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
182 if (what == OP_conv_notrunc)
183 flags |= FLAG_NOTRUNC;
184 if (what == OP_conv_sync)
186 if (what == OP_conv_noerror)
187 flags |= FLAG_NOERROR;
188 if (!key) /* no ',' left, so this was the last specifier */
190 arg = key + 1; /* skip this keyword and ',' */
196 ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
199 /* These can be large: */
200 if (what == OP_count) {
202 count = XATOU_SFX(arg, dd_suffixes);
205 if (what == OP_seek) {
206 seek = XATOU_SFX(arg, dd_suffixes);
209 if (what == OP_skip) {
210 skip = XATOU_SFX(arg, dd_suffixes);
220 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
221 ibuf = obuf = xmalloc(ibs);
223 flags |= FLAG_TWOBUFS;
227 xmove_fd(xopen(infile, O_RDONLY), ifd);
229 infile = bb_msg_standard_input;
231 if (outfile != NULL) {
232 int oflag = O_WRONLY | O_CREAT;
234 if (!seek && !(flags & FLAG_NOTRUNC))
237 xmove_fd(xopen(outfile, oflag), ofd);
239 if (seek && !(flags & FLAG_NOTRUNC)) {
240 if (ftruncate(ofd, seek * obs) < 0) {
243 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
249 outfile = bb_msg_standard_output;
252 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
254 n = safe_read(ifd, ibuf, ibs);
263 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
267 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
268 if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */
269 memset(ibuf, 0, ibs);
270 n = safe_read(ifd, ibuf, ibs);
274 if (!(flags & FLAG_NOERROR))
277 bb_simple_perror_msg(infile);
279 if ((size_t)n == ibs)
283 if (flags & FLAG_SYNC) {
284 memset(ibuf + n, '\0', ibs - n);
288 if (flags & FLAG_TWOBUFS) {
295 memcpy(obuf + oc, tmp, d);
300 if (write_and_stats(obuf, obs, obs, outfile))
305 } else if (write_and_stats(ibuf, n, obs, outfile))
309 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
310 w = full_write_or_warn(obuf, oc, outfile);
311 if (w < 0) goto out_status;
315 if (close(ifd) < 0) {
317 bb_simple_perror_msg_and_die(infile);
320 if (close(ofd) < 0) {
322 bb_simple_perror_msg_and_die(outfile);
325 exitcode = EXIT_SUCCESS;