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);
54 int dd_main(int argc, char **argv)
60 twobufs_flag = 1 << 3,
62 int flags = trunc_flag;
63 size_t oc = 0, ibs = 512, obs = 512;
65 off_t seek = 0, skip = 0, count = OFF_T_MAX;
67 const char *infile = NULL, *outfile = NULL;
70 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
73 memset(&sa, 0, sizeof(sa));
74 sa.sa_handler = dd_output_status;
75 sa.sa_flags = SA_RESTART;
76 sigemptyset(&sa.sa_mask);
77 sigaction(SIGUSR1, &sa, 0);
80 for (n = 1; n < argc; n++) {
82 /* Must fit into positive ssize_t */
83 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", arg, 4))
84 ibs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
85 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", arg, 4))
86 obs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
87 else if (!strncmp("bs=", arg, 3))
88 ibs = obs = xatoul_range_sfx(arg+3, 0, ((size_t)-1L)/2, dd_suffixes);
89 /* These can be large: */
90 else if (!strncmp("count=", arg, 6))
91 count = XATOU_SFX(arg+6, dd_suffixes);
92 else if (!strncmp("seek=", arg, 5))
93 seek = XATOU_SFX(arg+5, dd_suffixes);
94 else if (!strncmp("skip=", arg, 5))
95 skip = XATOU_SFX(arg+5, dd_suffixes);
97 else if (!strncmp("if=", arg, 3))
99 else if (!strncmp("of=", arg, 3))
101 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", arg, 5)) {
104 if (!strncmp("notrunc", arg, 7)) {
105 flags &= ~trunc_flag;
107 } else if (!strncmp("sync", arg, 4)) {
110 } else if (!strncmp("noerror", arg, 7)) {
114 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
116 if (arg[0] == '\0') break;
117 if (*arg++ != ',') bb_show_usage();
123 ibuf = obuf = xmalloc(ibs);
125 flags |= twobufs_flag;
130 ifd = xopen(infile, O_RDONLY);
133 infile = bb_msg_standard_input;
136 if (outfile != NULL) {
137 oflag = O_WRONLY | O_CREAT;
139 if (!seek && (flags & trunc_flag))
142 ofd = xopen(outfile, oflag);
144 if (seek && (flags & trunc_flag)) {
145 if (ftruncate(ofd, seek * obs) < 0) {
148 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
155 outfile = bb_msg_standard_output;
159 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
161 n = safe_read(ifd, ibuf, ibs);
163 bb_perror_msg_and_die("%s", infile);
171 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
175 while (in_full + in_part != count) {
176 if (flags & noerror) {
177 /* Pre-zero the buffer when doing the noerror thing */
178 memset(ibuf, '\0', ibs);
181 n = safe_read(ifd, ibuf, ibs);
185 if (flags & noerror) {
187 bb_perror_msg("%s", infile);
189 bb_perror_msg_and_die("%s", infile);
191 if ((size_t)n == ibs)
195 if (flags & sync_flag) {
196 memset(ibuf + n, '\0', ibs - n);
200 if (flags & twobufs_flag) {
207 memcpy(obuf + oc, tmp, d);
212 w = full_write_or_warn(ofd, obuf, obs, outfile);
213 if (w < 0) goto out_status;
222 w = full_write_or_warn(ofd, ibuf, n, outfile);
223 if (w < 0) goto out_status;
231 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
232 w = full_write_or_warn(ofd, obuf, oc, outfile);
233 if (w < 0) goto out_status;
237 if (close(ifd) < 0) {
238 bb_perror_msg_and_die("%s", infile);
241 if (close(ofd) < 0) {
243 bb_perror_msg_and_die("%s", outfile);