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 <sys/types.h>
18 #include <signal.h> // For FEATURE_DD_SIGNAL_HANDLING
21 static const struct suffix_mult dd_suffixes[] = {
34 static size_t out_full;
35 static size_t out_part;
36 static size_t in_full;
37 static size_t in_part;
39 static void dd_output_status(int cur_signal)
41 fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n",
42 (long)in_full, (long)in_part,
43 (long)out_full, (long)out_part);
46 int dd_main(int argc, char **argv)
48 size_t count = -1, oc = 0, ibs = 512, obs = 512;
50 off_t seek = 0, skip = 0;
51 int sync_flag = FALSE, noerror = FALSE, trunc_flag = TRUE, twobufs_flag = 0,
53 const char *infile = NULL, *outfile = NULL;
56 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING)
60 memset(&sa, 0, sizeof(sa));
61 sa.sa_handler = dd_output_status;
62 sa.sa_flags = SA_RESTART;
63 sigemptyset(&sa.sa_mask);
64 sigaction(SIGUSR1, &sa, 0);
67 for (i = 1; i < argc; i++) {
68 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", argv[i], 4)) {
69 ibs = bb_xparse_number(argv[i]+4, dd_suffixes);
71 } else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", argv[i], 4)) {
72 obs = bb_xparse_number(argv[i]+4, dd_suffixes);
74 } else if (!strncmp("bs=", argv[i], 3)) {
75 ibs = obs = bb_xparse_number(argv[i]+3, dd_suffixes);
76 } else if (!strncmp("count=", argv[i], 6))
77 count = bb_xparse_number(argv[i]+6, dd_suffixes);
78 else if (!strncmp("seek=", argv[i], 5))
79 seek = bb_xparse_number(argv[i]+5, dd_suffixes);
80 else if (!strncmp("skip=", argv[i], 5))
81 skip = bb_xparse_number(argv[i]+5, dd_suffixes);
82 else if (!strncmp("if=", argv[i], 3))
84 else if (!strncmp("of=", argv[i], 3))
86 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", argv[i], 5)) {
89 if (!strncmp("notrunc", ibuf, 7)) {
92 } else if (!strncmp("sync", ibuf, 4)) {
95 } else if (!strncmp("noerror", ibuf, 7)) {
99 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i]+5, "conv");
101 if (ibuf[0] == '\0') break;
102 if (ibuf[0] == ',') ibuf++;
109 if (twobufs_flag) obuf = xmalloc(obs);
112 if (infile != NULL) {
113 ifd = bb_xopen(infile, O_RDONLY);
116 infile = bb_msg_standard_input;
119 if (outfile != NULL) {
120 oflag = O_WRONLY | O_CREAT;
122 if (!seek && trunc_flag) {
126 ofd = bb_xopen3(outfile, oflag, 0666);
128 if (seek && trunc_flag) {
129 if (ftruncate(ofd, seek * obs) < 0) {
132 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
133 S_ISDIR (st.st_mode)) {
134 bb_perror_msg_and_die("%s", outfile);
140 outfile = bb_msg_standard_output;
144 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
146 n = safe_read(ifd, ibuf, ibs);
148 bb_perror_msg_and_die("%s", infile);
156 if (lseek(ofd, seek * obs, SEEK_CUR) < 0) {
157 bb_perror_msg_and_die("%s", outfile);
161 while (in_full + in_part != count) {
163 /* Pre-zero the buffer when doing the noerror thing */
164 memset(ibuf, '\0', ibs);
167 n = safe_read(ifd, ibuf, ibs);
174 bb_perror_msg("%s", infile);
176 bb_perror_msg_and_die("%s", infile);
179 if ((size_t)n == ibs) {
184 memset(ibuf + n, '\0', ibs - n);
194 memcpy(obuf + oc, tmp, d);
199 if (bb_full_write(ofd, obuf, obs) < 0) {
200 bb_perror_msg_and_die("%s", outfile);
207 if ((n = bb_full_write(ofd, ibuf, n)) < 0) {
208 bb_perror_msg_and_die("%s", outfile);
210 if (n == ibs) out_full++;
215 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
216 if (bb_full_write(ofd, obuf, oc) < 0) {
217 bb_perror_msg_and_die("%s", outfile);
221 if (close (ifd) < 0) {
222 bb_perror_msg_and_die("%s", infile);
225 if (close (ofd) < 0) {
226 bb_perror_msg_and_die("%s", outfile);