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.
12 #include <signal.h> /* For FEATURE_DD_SIGNAL_HANDLING */
14 static const struct suffix_mult dd_suffixes[] = {
20 { "K", 1024 }, /* compat with coreutils dd */
29 off_t out_full, out_part, in_full, in_part;
31 #define G (*(struct globals*)&bb_common_bufsiz1)
33 static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
35 fprintf(stderr, "%"OFF_FMT"d+%"OFF_FMT"d records in\n"
36 "%"OFF_FMT"d+%"OFF_FMT"d records out\n",
38 G.out_full, G.out_part);
41 static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
42 const char * const filename)
44 ssize_t n = full_write(fd, buf, len);
46 bb_perror_msg("writing '%s'", filename);
50 static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
51 const char * const filename)
53 ssize_t n = full_write_or_warn(fd, buf, len, filename);
64 #define XATOU_SFX xatoull_sfx
66 #define XATOU_SFX xatoul_sfx
69 int dd_main(int argc, char **argv);
70 int dd_main(int argc, char **argv)
76 twobufs_flag = 1 << 3,
78 const char * const keywords[] = {
79 "bs=", "count=", "seek=", "skip=", "if=", "of=",
80 #if ENABLE_FEATURE_DD_IBS_OBS
81 "ibs=", "obs=", "conv=", "notrunc", "sync", "noerror",
86 #define OP_count OP_bs + 1
87 #define OP_seek OP_count + 1
88 #define OP_skip OP_seek + 1
89 #define OP_if OP_skip + 1
90 #define OP_of OP_if + 1
91 #define OP_ibs OP_of + ENABLE_FEATURE_DD_IBS_OBS
92 #define OP_obs OP_ibs + ENABLE_FEATURE_DD_IBS_OBS
93 #define OP_conv OP_obs + ENABLE_FEATURE_DD_IBS_OBS
94 #define OP_conv_notrunc OP_conv + ENABLE_FEATURE_DD_IBS_OBS
95 #define OP_conv_sync OP_conv_notrunc + ENABLE_FEATURE_DD_IBS_OBS
96 #define OP_conv_noerror OP_conv_sync + ENABLE_FEATURE_DD_IBS_OBS
98 int flags = trunc_flag;
99 size_t oc = 0, ibs = 512, obs = 512;
101 off_t seek = 0, skip = 0, count = OFF_T_MAX;
103 const char *infile = NULL, *outfile = NULL;
106 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
109 memset(&sa, 0, sizeof(sa));
110 sa.sa_handler = dd_output_status;
111 sa.sa_flags = SA_RESTART;
112 sigemptyset(&sa.sa_mask);
113 sigaction(SIGUSR1, &sa, 0);
116 for (n = 1; n < argc; n++) {
117 smalluint key_len, what;
121 //XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
122 //if (*arg == '-' && *++arg == '-' && !*++arg) continue;
123 key = strstr(arg, "=");
126 key_len = key - arg + 1;
127 key = xstrndup(arg, key_len);
128 what = index_in_str_array(keywords, key) + 1;
129 if (ENABLE_FEATURE_CLEAN_UP)
134 /* Must fit into positive ssize_t */
135 if (ENABLE_FEATURE_DD_IBS_OBS) {
136 if (what == OP_ibs) {
137 ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
140 if (what == OP_obs) {
141 obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
144 if (what == OP_conv) {
146 /* find ',', replace them with nil so we can use arg for
147 * index_in_str_array without copying.
148 * We rely on arg being non-null, else strstr would fault.
150 key = strstr(arg, ",");
153 what = index_in_str_array(keywords, arg) + 1;
154 if (what < OP_conv_notrunc)
155 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
156 if (what == OP_conv_notrunc)
157 flags &= ~trunc_flag;
158 if (what == OP_conv_sync)
160 if (what == OP_conv_noerror)
162 if (!key) /* no ',' left, so this was the last specifier */
164 arg += key - arg + 1; /* skip this keyword plus ',' */
170 ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
173 /* These can be large: */
174 if (what == OP_count) {
175 count = XATOU_SFX(arg, dd_suffixes);
178 if (what == OP_seek) {
179 seek = XATOU_SFX(arg, dd_suffixes);
183 skip = XATOU_SFX(arg, dd_suffixes);
193 //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
194 ibuf = obuf = xmalloc(ibs);
196 flags |= twobufs_flag;
200 ifd = xopen(infile, O_RDONLY);
203 infile = bb_msg_standard_input;
205 if (outfile != NULL) {
206 int oflag = O_WRONLY | O_CREAT;
208 if (!seek && (flags & trunc_flag))
211 ofd = xopen(outfile, oflag);
213 if (seek && (flags & trunc_flag)) {
214 if (ftruncate(ofd, seek * obs) < 0) {
217 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
224 outfile = bb_msg_standard_output;
227 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
229 n = safe_read(ifd, ibuf, ibs);
238 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
242 while (G.in_full + G.in_part != count) {
243 if (flags & noerror) /* Pre-zero the buffer when for noerror */
244 memset(ibuf, '\0', ibs);
245 n = safe_read(ifd, ibuf, ibs);
249 if (flags & noerror) {
251 bb_perror_msg("%s", infile);
255 if ((size_t)n == ibs)
259 if (flags & sync_flag) {
260 memset(ibuf + n, '\0', ibs - n);
264 if (flags & twobufs_flag) {
271 memcpy(obuf + oc, tmp, d);
276 if (write_and_stats(ofd, obuf, obs, obs, outfile))
282 if (write_and_stats(ofd, ibuf, n, obs, outfile))
286 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
287 w = full_write_or_warn(ofd, obuf, oc, outfile);
288 if (w < 0) goto out_status;
292 if (close(ifd) < 0) {
294 bb_perror_msg_and_die("%s", infile);
297 if (close(ofd) < 0) {
299 bb_perror_msg_and_die("%s", outfile);