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 fprintf(stderr, "%"OFF_FMT"d+%"OFF_FMT"d records in\n"
42 "%"OFF_FMT"d+%"OFF_FMT"d records out\n",
44 G.out_full, G.out_part);
47 static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
48 const char * const filename)
50 ssize_t n = full_write(fd, buf, len);
52 bb_perror_msg("writing '%s'", filename);
56 static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
59 ssize_t n = full_write_or_warn(fd, buf, len, filename);
70 #define XATOU_SFX xatoull_sfx
72 #define XATOU_SFX xatoul_sfx
75 int dd_main(int argc, char **argv);
76 int dd_main(int argc, char **argv)
80 FLAG_NOERROR = 1 << 1,
81 FLAG_NOTRUNC = 1 << 2,
82 FLAG_TWOBUFS = 1 << 3,
85 static const char keywords[] =
86 "bs=\0""count=\0""seek=\0""skip=\0""if=\0""of=\0"
87 #if ENABLE_FEATURE_DD_IBS_OBS
88 "ibs=\0""obs=\0""conv=\0""notrunc\0""sync\0""noerror\0"
98 #if ENABLE_FEATURE_DD_IBS_OBS
107 size_t ibs = 512, obs = 512;
110 /* And these are all zeroed at once! */
117 const char *infile, *outfile;
118 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
119 struct sigaction sigact;
122 #define flags (Z.flags )
126 #define count (Z.count )
127 #define seek (Z.seek )
128 #define skip (Z.skip )
129 #define infile (Z.infile )
130 #define outfile (Z.outfile)
131 #define sigact (Z.sigact )
133 memset(&Z, 0, sizeof(Z));
136 #if ENABLE_FEATURE_DD_SIGNAL_HANDLING
137 sigact.sa_handler = dd_output_status;
138 sigact.sa_flags = SA_RESTART;
139 sigemptyset(&sigact.sa_mask);
140 sigaction(SIGUSR1, &sigact, NULL);
143 for (n = 1; n < argc; n++) {
149 //XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
150 //if (*arg == '-' && *++arg == '-' && !*++arg) continue;
151 key = strstr(arg, "=");
154 key_len = key - arg + 1;
155 key = xstrndup(arg, key_len);
156 what = index_in_strings(keywords, key) + 1;
157 if (ENABLE_FEATURE_CLEAN_UP)
162 /* Must fit into positive ssize_t */
163 #if ENABLE_FEATURE_DD_IBS_OBS
164 if (what == OP_ibs) {
165 ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
168 if (what == OP_obs) {
169 obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
172 if (what == OP_conv) {
174 /* find ',', replace them with nil so we can use arg for
175 * index_in_strings() without copying.
176 * We rely on arg being non-null, else strchr would fault.
178 key = strchr(arg, ',');
181 what = index_in_strings(keywords, arg) + 1;
182 if (what < OP_conv_notrunc)
183 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
184 if (what == OP_conv_notrunc)
185 flags |= FLAG_NOTRUNC;
186 if (what == OP_conv_sync)
188 if (what == OP_conv_noerror)
189 flags |= FLAG_NOERROR;
190 if (!key) /* no ',' left, so this was the last specifier */
192 arg = key + 1; /* skip this keyword and ',' */
198 ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
201 /* These can be large: */
202 if (what == OP_count) {
204 count = XATOU_SFX(arg, dd_suffixes);
207 if (what == OP_seek) {
208 seek = XATOU_SFX(arg, dd_suffixes);
211 if (what == OP_skip) {
212 skip = XATOU_SFX(arg, dd_suffixes);
222 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
223 ibuf = obuf = xmalloc(ibs);
225 flags |= FLAG_TWOBUFS;
229 ifd = xopen(infile, O_RDONLY);
231 /* ifd = STDIN_FILENO; - it's zero and it's already there */
232 infile = bb_msg_standard_input;
234 if (outfile != NULL) {
235 int oflag = O_WRONLY | O_CREAT;
237 if (!seek && !(flags & FLAG_NOTRUNC))
240 ofd = xopen(outfile, oflag);
242 if (seek && !(flags & FLAG_NOTRUNC)) {
243 if (ftruncate(ofd, seek * obs) < 0) {
246 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
253 outfile = bb_msg_standard_output;
256 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
258 n = safe_read(ifd, ibuf, ibs);
267 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
271 while (!(flags & FLAG_COUNT) || (G.in_full + G.in_part != count)) {
272 if (flags & FLAG_NOERROR) /* Pre-zero the buffer if conv=noerror */
273 memset(ibuf, 0, ibs);
274 n = safe_read(ifd, ibuf, ibs);
278 if (flags & FLAG_NOERROR) {
280 bb_perror_msg("%s", infile);
284 if ((size_t)n == ibs)
288 if (flags & FLAG_SYNC) {
289 memset(ibuf + n, '\0', ibs - n);
293 if (flags & FLAG_TWOBUFS) {
300 memcpy(obuf + oc, tmp, d);
305 if (write_and_stats(ofd, obuf, obs, obs, outfile))
310 } else if (write_and_stats(ofd, ibuf, n, obs, outfile))
314 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
315 w = full_write_or_warn(ofd, obuf, oc, outfile);
316 if (w < 0) goto out_status;
320 if (close(ifd) < 0) {
322 bb_perror_msg_and_die("%s", infile);
325 if (close(ofd) < 0) {
327 bb_perror_msg_and_die("%s", outfile);