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.
13 /* This is a NOEXEC applet. Be very careful! */
21 static const struct suffix_mult dd_suffixes[] = {
27 { "K", 1024 }, /* compat with coreutils dd */
36 off_t out_full, out_part, in_full, in_part;
38 #define G (*(struct globals*)&bb_common_bufsiz1)
39 #define INIT_G() do { \
40 /* we have to zero it out because of NOEXEC */ \
41 memset(&G, 0, sizeof(G)); \
45 static void dd_output_status(int UNUSED_PARAM cur_signal)
47 /* 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",
51 G.out_full, G.out_part);
54 static ssize_t full_write_or_warn(const void *buf, size_t len,
55 const char *const filename)
57 ssize_t n = full_write(ofd, buf, len);
59 bb_perror_msg("writing '%s'", filename);
63 static bool write_and_stats(const void *buf, size_t len, size_t obs,
66 ssize_t n = full_write_or_warn(buf, len, filename);
77 #define XATOU_SFX xatoull_sfx
79 #define XATOU_SFX xatoul_sfx
82 int dd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
83 int dd_main(int argc UNUSED_PARAM, char **argv)
86 /* Must be in the same order as OP_conv_XXX! */
87 /* (see "flags |= (1 << what)" below) */
88 FLAG_NOTRUNC = 1 << 0,
90 FLAG_NOERROR = 1 << 2,
92 /* end of conv flags */
93 FLAG_TWOBUFS = 1 << 4,
96 static const char keywords[] ALIGN1 =
97 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
98 #if ENABLE_FEATURE_DD_IBS_OBS
99 "ibs\0""obs\0""conv\0"
102 #if ENABLE_FEATURE_DD_IBS_OBS
103 static const char conv_words[] ALIGN1 =
104 "notrunc\0""sync\0""noerror\0""fsync\0";
113 #if ENABLE_FEATURE_DD_IBS_OBS
117 /* Must be in the same order as FLAG_XXX! */
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
136 int exitcode = EXIT_FAILURE;
137 size_t ibs = 512, obs = 512;
140 /* And these are all zeroed at once! */
146 const char *infile, *outfile;
148 #define flags (Z.flags )
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)
156 memset(&Z, 0, sizeof(Z));
158 //fflush(NULL); - is this needed because of NOEXEC?
160 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
161 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
164 for (n = 1; argv[n]; n++) {
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')
175 val = strchr(arg, '=');
179 what = index_in_strings(keywords, arg);
182 /* *val = '='; - to preserve ps listing? */
184 #if ENABLE_FEATURE_DD_IBS_OBS
185 if (what == OP_ibs) {
186 /* Must fit into positive ssize_t */
187 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
190 if (what == OP_obs) {
191 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
194 if (what == OP_conv) {
196 /* find ',', replace them with NUL so we can use val for
197 * index_in_strings() without copying.
198 * We rely on val being non-null, else strchr would fault.
200 arg = strchr(val, ',');
203 what = index_in_strings(conv_words, val);
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 */
209 /* *arg = ','; - to preserve ps listing? */
210 val = arg + 1; /* skip this keyword and ',' */
212 continue; /* we trashed 'what', can't fall through */
216 ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
219 /* These can be large: */
220 if (what == OP_count) {
222 count = XATOU_SFX(val, dd_suffixes);
225 if (what == OP_seek) {
226 seek = XATOU_SFX(val, dd_suffixes);
229 if (what == OP_skip) {
230 skip = XATOU_SFX(val, dd_suffixes);
241 } /* end of "for (argv[n])" */
243 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
244 ibuf = obuf = xmalloc(ibs);
246 flags |= FLAG_TWOBUFS;
250 xmove_fd(xopen(infile, O_RDONLY), ifd);
252 infile = bb_msg_standard_input;
254 if (outfile != NULL) {
255 int oflag = O_WRONLY | O_CREAT;
257 if (!seek && !(flags & FLAG_NOTRUNC))
260 xmove_fd(xopen(outfile, oflag), ofd);
262 if (seek && !(flags & FLAG_NOTRUNC)) {
263 if (ftruncate(ofd, seek * obs) < 0) {
266 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
272 outfile = bb_msg_standard_output;
275 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
277 n = safe_read(ifd, ibuf, ibs);
286 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
290 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
291 n = safe_read(ifd, ibuf, ibs);
296 if (!(flags & FLAG_NOERROR))
298 bb_simple_perror_msg(infile);
299 /* GNU dd with conv=noerror skips over bad blocks */
300 xlseek(ifd, ibs, SEEK_CUR);
301 /* conv=noerror,sync writes NULs,
302 * conv=noerror just ignores input bad blocks */
305 if ((size_t)n == ibs)
309 if (flags & FLAG_SYNC) {
310 memset(ibuf + n, 0, ibs - n);
314 if (flags & FLAG_TWOBUFS) {
321 memcpy(obuf + oc, tmp, d);
326 if (write_and_stats(obuf, obs, obs, outfile))
331 } else if (write_and_stats(ibuf, n, obs, outfile))
334 if (flags & FLAG_FSYNC) {
340 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
341 w = full_write_or_warn(obuf, oc, outfile);
342 if (w < 0) goto out_status;
343 if (w > 0) G.out_part++;
345 if (close(ifd) < 0) {
347 bb_simple_perror_msg_and_die(infile);
350 if (close(ofd) < 0) {
352 bb_simple_perror_msg_and_die(outfile);
355 exitcode = EXIT_SUCCESS;