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
28 static off_t out_full, out_part, in_full, in_part;
30 static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
32 fprintf(stderr, "%"OFF_FMT"d+%"OFF_FMT"d records in\n"
33 "%"OFF_FMT"d+%"OFF_FMT"d records out\n",
38 static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
41 ssize_t n = full_write(fd, buf, len);
43 bb_perror_msg("writing '%s'", filename);
48 #define XATOU_SFX xatoull_sfx
50 #define XATOU_SFX xatoul_sfx
53 int dd_main(int argc, char **argv)
59 twobufs_flag = 1 << 3,
61 int flags = trunc_flag;
62 size_t oc = 0, ibs = 512, obs = 512;
64 off_t seek = 0, skip = 0, count = OFF_T_MAX;
66 const char *infile = NULL, *outfile = NULL;
69 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
72 memset(&sa, 0, sizeof(sa));
73 sa.sa_handler = dd_output_status;
74 sa.sa_flags = SA_RESTART;
75 sigemptyset(&sa.sa_mask);
76 sigaction(SIGUSR1, &sa, 0);
79 for (n = 1; n < argc; n++) {
81 /* Must fit into positive ssize_t */
82 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", arg, 4))
83 ibs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
84 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", arg, 4))
85 obs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
86 else if (!strncmp("bs=", arg, 3))
87 ibs = obs = xatoul_range_sfx(arg+3, 0, ((size_t)-1L)/2, dd_suffixes);
88 /* These can be large: */
89 else if (!strncmp("count=", arg, 6))
90 count = XATOU_SFX(arg+6, dd_suffixes);
91 else if (!strncmp("seek=", arg, 5))
92 seek = XATOU_SFX(arg+5, dd_suffixes);
93 else if (!strncmp("skip=", arg, 5))
94 skip = XATOU_SFX(arg+5, dd_suffixes);
96 else if (!strncmp("if=", arg, 3))
98 else if (!strncmp("of=", arg, 3))
100 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", arg, 5)) {
103 if (!strncmp("notrunc", arg, 7)) {
104 flags &= ~trunc_flag;
106 } else if (!strncmp("sync", arg, 4)) {
109 } else if (!strncmp("noerror", arg, 7)) {
113 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
115 if (arg[0] == '\0') break;
116 if (*arg++ != ',') bb_show_usage();
122 ibuf = obuf = xmalloc(ibs);
124 flags |= twobufs_flag;
129 ifd = xopen(infile, O_RDONLY);
132 infile = bb_msg_standard_input;
135 if (outfile != NULL) {
136 oflag = O_WRONLY | O_CREAT;
138 if (!seek && (flags & trunc_flag))
141 ofd = xopen3(outfile, oflag, 0666);
143 if (seek && (flags & trunc_flag)) {
144 if (ftruncate(ofd, seek * obs) < 0) {
147 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
154 outfile = bb_msg_standard_output;
158 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
160 n = safe_read(ifd, ibuf, ibs);
162 bb_perror_msg_and_die("%s", infile);
170 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
174 while (in_full + in_part != count) {
175 if (flags & noerror) {
176 /* Pre-zero the buffer when doing the noerror thing */
177 memset(ibuf, '\0', ibs);
180 n = safe_read(ifd, ibuf, ibs);
184 if (flags & noerror) {
186 bb_perror_msg("%s", infile);
188 bb_perror_msg_and_die("%s", infile);
190 if ((size_t)n == ibs)
194 if (flags & sync_flag) {
195 memset(ibuf + n, '\0', ibs - n);
199 if (flags & twobufs_flag) {
206 memcpy(obuf + oc, tmp, d);
211 w = full_write_or_warn(ofd, obuf, obs, outfile);
212 if (w < 0) goto out_status;
221 w = full_write_or_warn(ofd, ibuf, n, outfile);
222 if (w < 0) goto out_status;
230 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
231 w = full_write_or_warn(ofd, obuf, oc, outfile);
232 if (w < 0) goto out_status;
236 if (close(ifd) < 0) {
237 bb_perror_msg_and_die("%s", infile);
240 if (close(ofd) < 0) {
242 bb_perror_msg_and_die("%s", outfile);