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)
36 static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
38 fprintf(stderr, "%"OFF_FMT"d+%"OFF_FMT"d records in\n"
39 "%"OFF_FMT"d+%"OFF_FMT"d records out\n",
41 G.out_full, G.out_part);
44 static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
45 const char * const filename)
47 ssize_t n = full_write(fd, buf, len);
49 bb_perror_msg("writing '%s'", filename);
53 static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
54 const char * const filename)
56 ssize_t n = full_write_or_warn(fd, buf, len, filename);
67 #define XATOU_SFX xatoull_sfx
69 #define XATOU_SFX xatoul_sfx
72 int dd_main(int argc, char **argv);
73 int dd_main(int argc, char **argv)
79 twobufs_flag = 1 << 3,
81 static const char * const keywords[] = {
82 "bs=", "count=", "seek=", "skip=", "if=", "of=",
83 #if ENABLE_FEATURE_DD_IBS_OBS
84 "ibs=", "obs=", "conv=", "notrunc", "sync", "noerror",
95 USE_FEATURE_DD_IBS_OBS(
104 int flags = trunc_flag;
105 size_t oc = 0, ibs = 512, obs = 512;
107 off_t seek = 0, skip = 0, count = OFF_T_MAX;
109 const char *infile = NULL, *outfile = NULL;
112 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
115 memset(&sa, 0, sizeof(sa));
116 sa.sa_handler = dd_output_status;
117 sa.sa_flags = SA_RESTART;
118 sigemptyset(&sa.sa_mask);
119 sigaction(SIGUSR1, &sa, 0);
122 for (n = 1; n < argc; n++) {
128 //XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
129 //if (*arg == '-' && *++arg == '-' && !*++arg) continue;
130 key = strstr(arg, "=");
133 key_len = key - arg + 1;
134 key = xstrndup(arg, key_len);
135 what = index_in_str_array(keywords, key) + 1;
136 if (ENABLE_FEATURE_CLEAN_UP)
141 /* Must fit into positive ssize_t */
142 if (ENABLE_FEATURE_DD_IBS_OBS) {
143 if (what == OP_ibs) {
144 ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
147 if (what == OP_obs) {
148 obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
151 if (what == OP_conv) {
153 /* find ',', replace them with nil so we can use arg for
154 * index_in_str_array without copying.
155 * We rely on arg being non-null, else strstr would fault.
157 key = strstr(arg, ",");
160 what = index_in_str_array(keywords, arg) + 1;
161 if (what < OP_conv_notrunc)
162 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
163 if (what == OP_conv_notrunc)
164 flags &= ~trunc_flag;
165 if (what == OP_conv_sync)
167 if (what == OP_conv_noerror)
169 if (!key) /* no ',' left, so this was the last specifier */
171 arg += key - arg + 1; /* skip this keyword plus ',' */
177 ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
180 /* These can be large: */
181 if (what == OP_count) {
182 count = XATOU_SFX(arg, dd_suffixes);
185 if (what == OP_seek) {
186 seek = XATOU_SFX(arg, dd_suffixes);
190 skip = XATOU_SFX(arg, dd_suffixes);
200 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
201 ibuf = obuf = xmalloc(ibs);
203 flags |= twobufs_flag;
207 ifd = xopen(infile, O_RDONLY);
210 infile = bb_msg_standard_input;
212 if (outfile != NULL) {
213 int oflag = O_WRONLY | O_CREAT;
215 if (!seek && (flags & trunc_flag))
218 ofd = xopen(outfile, oflag);
220 if (seek && (flags & trunc_flag)) {
221 if (ftruncate(ofd, seek * obs) < 0) {
224 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
231 outfile = bb_msg_standard_output;
234 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
236 n = safe_read(ifd, ibuf, ibs);
245 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
249 while (G.in_full + G.in_part != count) {
250 if (flags & noerror) /* Pre-zero the buffer when for noerror */
251 memset(ibuf, '\0', ibs);
252 n = safe_read(ifd, ibuf, ibs);
256 if (flags & noerror) {
258 bb_perror_msg("%s", infile);
262 if ((size_t)n == ibs)
266 if (flags & sync_flag) {
267 memset(ibuf + n, '\0', ibs - n);
271 if (flags & twobufs_flag) {
278 memcpy(obuf + oc, tmp, d);
283 if (write_and_stats(ofd, obuf, obs, obs, outfile))
289 if (write_and_stats(ofd, ibuf, n, obs, outfile))
293 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
294 w = full_write_or_warn(ofd, obuf, oc, outfile);
295 if (w < 0) goto out_status;
299 if (close(ifd) < 0) {
301 bb_perror_msg_and_die("%s", infile);
304 if (close(ofd) < 0) {
306 bb_perror_msg_and_die("%s", outfile);