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)
53 int sync_flag = FALSE;
55 int trunc_flag = TRUE;
60 const char *infile = NULL;
61 const char *outfile = NULL;
64 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING)
68 memset(&sa, 0, sizeof(sa));
69 sa.sa_handler = dd_output_status;
70 sa.sa_flags = SA_RESTART;
71 sigemptyset(&sa.sa_mask);
72 sigaction(SIGUSR1, &sa, 0);
75 for (i = 1; i < argc; i++) {
76 if (strncmp("bs=", argv[i], 3) == 0)
77 bs = bb_xparse_number(argv[i]+3, dd_suffixes);
78 else if (strncmp("count=", argv[i], 6) == 0)
79 count = bb_xparse_number(argv[i]+6, dd_suffixes);
80 else if (strncmp("seek=", argv[i], 5) == 0)
81 seek = bb_xparse_number(argv[i]+5, dd_suffixes);
82 else if (strncmp("skip=", argv[i], 5) == 0)
83 skip = bb_xparse_number(argv[i]+5, dd_suffixes);
84 else if (strncmp("if=", argv[i], 3) == 0)
86 else if (strncmp("of=", argv[i], 3) == 0)
88 else if (strncmp("conv=", argv[i], 5) == 0) {
91 if (strncmp("notrunc", buf, 7) == 0) {
94 } else if (strncmp("sync", buf, 4) == 0) {
97 } else if (strncmp("noerror", buf, 7) == 0) {
101 bb_error_msg_and_die("invalid conversion `%s'", argv[i]+5);
114 if (infile != NULL) {
115 ifd = bb_xopen(infile, O_RDONLY);
118 infile = bb_msg_standard_input;
121 if (outfile != NULL) {
122 oflag = O_WRONLY | O_CREAT;
124 if (!seek && trunc_flag) {
128 ofd = bb_xopen3(outfile, oflag, 0666);
130 if (seek && trunc_flag) {
131 if (ftruncate(ofd, seek * bs) < 0) {
134 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
135 S_ISDIR (st.st_mode)) {
136 bb_perror_msg_and_die("%s", outfile);
142 outfile = bb_msg_standard_output;
146 if (lseek(ifd, skip * bs, SEEK_CUR) < 0) {
147 bb_perror_msg_and_die("%s", infile);
152 if (lseek(ofd, seek * bs, SEEK_CUR) < 0) {
153 bb_perror_msg_and_die("%s", outfile);
157 while (in_full + in_part != count) {
159 /* Pre-zero the buffer when doing the noerror thing */
160 memset(buf, '\0', bs);
162 n = safe_read(ifd, buf, bs);
166 bb_perror_msg("%s", infile);
168 bb_perror_msg_and_die("%s", infile);
174 if ((size_t)n == bs) {
180 memset(buf + n, '\0', bs - n);
183 n = bb_full_write(ofd, buf, n);
185 bb_perror_msg_and_die("%s", outfile);
187 if ((size_t)n == bs) {
194 if (close (ifd) < 0) {
195 bb_perror_msg_and_die("%s", infile);
198 if (close (ofd) < 0) {
199 bb_perror_msg_and_die("%s", outfile);