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);
47 int dd_main(int argc, char **argv)
53 twobufs_flag = 1 << 3,
55 int flags = trunc_flag;
56 size_t oc = 0, ibs = 512, obs = 512;
58 off_t seek = 0, skip = 0, count = OFF_T_MAX;
60 const char *infile = NULL, *outfile = NULL;
63 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
66 memset(&sa, 0, sizeof(sa));
67 sa.sa_handler = dd_output_status;
68 sa.sa_flags = SA_RESTART;
69 sigemptyset(&sa.sa_mask);
70 sigaction(SIGUSR1, &sa, 0);
73 for (n = 1; n < argc; n++) {
75 /* Must fit into positive ssize_t */
76 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", arg, 4))
77 ibs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
78 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", arg, 4))
79 obs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
80 else if (!strncmp("bs=", arg, 3))
81 ibs = obs = xatoul_range_sfx(arg+3, 0, ((size_t)-1L)/2, dd_suffixes);
82 // FIXME: make them capable of eating LARGE numbers
83 else if (!strncmp("count=", arg, 6))
84 count = xatoul_sfx(arg+6, dd_suffixes);
85 else if (!strncmp("seek=", arg, 5))
86 seek = xatoul_sfx(arg+5, dd_suffixes);
87 else if (!strncmp("skip=", arg, 5))
88 skip = xatoul_sfx(arg+5, dd_suffixes);
90 else if (!strncmp("if=", arg, 3))
92 else if (!strncmp("of=", arg, 3))
94 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", arg, 5)) {
97 if (!strncmp("notrunc", arg, 7)) {
100 } else if (!strncmp("sync", arg, 4)) {
103 } else if (!strncmp("noerror", arg, 7)) {
107 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
109 if (arg[0] == '\0') break;
110 if (*arg++ != ',') bb_show_usage();
116 ibuf = obuf = xmalloc(ibs);
118 flags |= twobufs_flag;
123 ifd = xopen(infile, O_RDONLY);
126 infile = bb_msg_standard_input;
129 if (outfile != NULL) {
130 oflag = O_WRONLY | O_CREAT;
132 if (!seek && (flags & trunc_flag))
135 ofd = xopen3(outfile, oflag, 0666);
137 if (seek && (flags & trunc_flag)) {
138 if (ftruncate(ofd, seek * obs) < 0) {
141 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
148 outfile = bb_msg_standard_output;
152 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
154 n = safe_read(ifd, ibuf, ibs);
156 bb_perror_msg_and_die("%s", infile);
164 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
168 while (in_full + in_part != count) {
169 if (flags & noerror) {
170 /* Pre-zero the buffer when doing the noerror thing */
171 memset(ibuf, '\0', ibs);
174 n = safe_read(ifd, ibuf, ibs);
178 if (flags & noerror) {
180 bb_perror_msg("%s", infile);
182 bb_perror_msg_and_die("%s", infile);
184 if ((size_t)n == ibs)
188 if (flags & sync_flag) {
189 memset(ibuf + n, '\0', ibs - n);
193 if (flags & twobufs_flag) {
200 memcpy(obuf + oc, tmp, d);
205 w = full_write_or_warn(ofd, obuf, obs, outfile);
206 if (w < 0) goto out_status;
215 w = full_write_or_warn(ofd, ibuf, n, outfile);
216 if (w < 0) goto out_status;
224 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
225 w = full_write_or_warn(ofd, obuf, oc, outfile);
226 if (w < 0) goto out_status;
230 if (close(ifd) < 0) {
231 bb_perror_msg_and_die("%s", infile);
234 if (close(ofd) < 0) {
236 bb_perror_msg_and_die("%s", outfile);