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! */
17 static const struct suffix_mult dd_suffixes[] = {
23 { "K", 1024 }, /* compat with coreutils dd */
32 off_t out_full, out_part, in_full, in_part;
34 #define G (*(struct globals*)&bb_common_bufsiz1)
35 /* We have to zero it out because of NOEXEC */
36 #define INIT_G() memset(&G, 0, sizeof(G))
39 static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
41 /* 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",
45 G.out_full, G.out_part);
48 static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
49 const char * const filename)
51 ssize_t n = full_write(fd, buf, len);
53 bb_perror_msg("writing '%s'", filename);
57 static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
60 ssize_t n = full_write_or_warn(fd, buf, len, filename);
71 #define XATOU_SFX xatoull_sfx
73 #define XATOU_SFX xatoul_sfx
76 int dd_main(int argc, char **argv);
77 int dd_main(int argc, char **argv)
81 FLAG_NOERROR = 1 << 1,
82 FLAG_NOTRUNC = 1 << 2,
83 FLAG_TWOBUFS = 1 << 3,
86 static const char keywords[] =
87 "bs=\0""count=\0""seek=\0""skip=\0""if=\0""of=\0"
88 #if ENABLE_FEATURE_DD_IBS_OBS
89 "ibs=\0""obs=\0""conv=\0""notrunc\0""sync\0""noerror\0"
99 #if ENABLE_FEATURE_DD_IBS_OBS
108 size_t ibs = 512, obs = 512;
111 /* And these are all zeroed at once! */
118 const char *infile, *outfile;
119 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
120 struct sigaction sigact;
123 #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)
132 #define sigact (Z.sigact )
134 memset(&Z, 0, sizeof(Z));
137 #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);
144 for (n = 1; n < argc; n++) {
150 //XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
151 //if (*arg == '-' && *++arg == '-' && !*++arg) continue;
152 key = strstr(arg, "=");
155 key_len = key - arg + 1;
156 key = xstrndup(arg, key_len);
157 what = index_in_strings(keywords, key) + 1;
158 if (ENABLE_FEATURE_CLEAN_UP)
163 /* Must fit into positive ssize_t */
164 #if ENABLE_FEATURE_DD_IBS_OBS
165 if (what == OP_ibs) {
166 ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
169 if (what == OP_obs) {
170 obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
173 if (what == OP_conv) {
175 /* find ',', replace them with nil so we can use arg for
176 * index_in_strings() without copying.
177 * We rely on arg being non-null, else strchr would fault.
179 key = strchr(arg, ',');
182 what = index_in_strings(keywords, arg) + 1;
183 if (what < OP_conv_notrunc)
184 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
185 if (what == OP_conv_notrunc)
186 flags |= FLAG_NOTRUNC;
187 if (what == OP_conv_sync)
189 if (what == OP_conv_noerror)
190 flags |= FLAG_NOERROR;
191 if (!key) /* no ',' left, so this was the last specifier */
193 arg = key + 1; /* skip this keyword and ',' */
199 ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
202 /* These can be large: */
203 if (what == OP_count) {
205 count = XATOU_SFX(arg, dd_suffixes);
208 if (what == OP_seek) {
209 seek = XATOU_SFX(arg, dd_suffixes);
212 if (what == OP_skip) {
213 skip = XATOU_SFX(arg, dd_suffixes);
223 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
224 ibuf = obuf = xmalloc(ibs);
226 flags |= FLAG_TWOBUFS;
230 ifd = xopen(infile, O_RDONLY);
232 /* ifd = STDIN_FILENO; - it's zero and it's already there */
233 infile = bb_msg_standard_input;
235 if (outfile != NULL) {
236 int oflag = O_WRONLY | O_CREAT;
238 if (!seek && !(flags & FLAG_NOTRUNC))
241 ofd = xopen(outfile, oflag);
243 if (seek && !(flags & FLAG_NOTRUNC)) {
244 if (ftruncate(ofd, seek * obs) < 0) {
247 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
254 outfile = bb_msg_standard_output;
257 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
259 n = safe_read(ifd, ibuf, ibs);
268 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
272 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
273 if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */
274 memset(ibuf, 0, ibs);
275 n = safe_read(ifd, ibuf, ibs);
279 if (flags & FLAG_NOERROR) {
281 bb_perror_msg("%s", infile);
285 if ((size_t)n == ibs)
289 if (flags & FLAG_SYNC) {
290 memset(ibuf + n, '\0', ibs - n);
294 if (flags & FLAG_TWOBUFS) {
301 memcpy(obuf + oc, tmp, d);
306 if (write_and_stats(ofd, obuf, obs, obs, outfile))
311 } else if (write_and_stats(ofd, ibuf, n, obs, outfile))
315 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
316 w = full_write_or_warn(ofd, obuf, oc, outfile);
317 if (w < 0) goto out_status;
321 if (close(ifd) < 0) {
323 bb_perror_msg_and_die("%s", infile);
326 if (close(ofd) < 0) {
328 bb_perror_msg_and_die("%s", outfile);