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 ATTRIBUTE_UNUSED, char **argv)
85 /* Must be in the same order as OP_conv_XXX! */
86 /* (see "flags |= (1 << what)" below) */
87 FLAG_NOTRUNC = 1 << 0,
89 FLAG_NOERROR = 1 << 2,
91 /* end of conv flags */
92 FLAG_TWOBUFS = 1 << 4,
95 static const char keywords[] ALIGN1 =
96 "bs\0""count\0""seek\0""skip\0""if\0""of\0"
97 #if ENABLE_FEATURE_DD_IBS_OBS
98 "ibs\0""obs\0""conv\0"
101 #if ENABLE_FEATURE_DD_IBS_OBS
102 static const char conv_words[] ALIGN1 =
103 "notrunc\0""sync\0""noerror\0""fsync\0";
112 #if ENABLE_FEATURE_DD_IBS_OBS
116 /* Must be in the same order as FLAG_XXX! */
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
135 int exitcode = EXIT_FAILURE;
136 size_t ibs = 512, obs = 512;
139 /* And these are all zeroed at once! */
145 const char *infile, *outfile;
147 #define flags (Z.flags )
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)
155 memset(&Z, 0, sizeof(Z));
157 //fflush(NULL); - is this needed because of NOEXEC?
159 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
160 signal_SA_RESTART_empty_mask(SIGUSR1, dd_output_status);
163 for (n = 1; argv[n]; n++) {
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')
174 val = strchr(arg, '=');
178 what = index_in_strings(keywords, arg);
181 /* *val = '='; - to preserve ps listing? */
183 #if ENABLE_FEATURE_DD_IBS_OBS
184 if (what == OP_ibs) {
185 /* Must fit into positive ssize_t */
186 ibs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
189 if (what == OP_obs) {
190 obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
193 if (what == OP_conv) {
195 /* find ',', replace them with NUL so we can use val for
196 * index_in_strings() without copying.
197 * We rely on val being non-null, else strchr would fault.
199 arg = strchr(val, ',');
202 what = index_in_strings(conv_words, val);
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 */
208 /* *arg = ','; - to preserve ps listing? */
209 val = arg + 1; /* skip this keyword and ',' */
211 continue; /* we trashed 'what', can't fall through */
215 ibs = obs = xatoul_range_sfx(val, 1, ((size_t)-1L)/2, dd_suffixes);
218 /* These can be large: */
219 if (what == OP_count) {
221 count = XATOU_SFX(val, dd_suffixes);
224 if (what == OP_seek) {
225 seek = XATOU_SFX(val, dd_suffixes);
228 if (what == OP_skip) {
229 skip = XATOU_SFX(val, dd_suffixes);
240 } /* end of "for (argv[n])" */
242 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
243 ibuf = obuf = xmalloc(ibs);
245 flags |= FLAG_TWOBUFS;
249 xmove_fd(xopen(infile, O_RDONLY), ifd);
251 infile = bb_msg_standard_input;
253 if (outfile != NULL) {
254 int oflag = O_WRONLY | O_CREAT;
256 if (!seek && !(flags & FLAG_NOTRUNC))
259 xmove_fd(xopen(outfile, oflag), ofd);
261 if (seek && !(flags & FLAG_NOTRUNC)) {
262 if (ftruncate(ofd, seek * obs) < 0) {
265 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
271 outfile = bb_msg_standard_output;
274 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
276 n = safe_read(ifd, ibuf, ibs);
285 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
289 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
290 if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */
291 memset(ibuf, 0, ibs);
292 n = safe_read(ifd, ibuf, ibs);
296 if (!(flags & FLAG_NOERROR))
299 bb_simple_perror_msg(infile);
301 if ((size_t)n == ibs)
305 if (flags & FLAG_SYNC) {
306 memset(ibuf + n, '\0', ibs - n);
310 if (flags & FLAG_TWOBUFS) {
317 memcpy(obuf + oc, tmp, d);
322 if (write_and_stats(obuf, obs, obs, outfile))
327 } else if (write_and_stats(ibuf, n, obs, outfile))
330 if (flags & FLAG_FSYNC) {
336 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
337 w = full_write_or_warn(obuf, oc, outfile);
338 if (w < 0) goto out_status;
339 if (w > 0) G.out_part++;
341 if (close(ifd) < 0) {
343 bb_simple_perror_msg_and_die(infile);
346 if (close(ofd) < 0) {
348 bb_simple_perror_msg_and_die(outfile);
351 exitcode = EXIT_SUCCESS;