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[] = {
27 static size_t out_full, out_part, in_full, in_part;
29 static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
31 bb_fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n",
32 (long)in_full, (long)in_part,
33 (long)out_full, (long)out_part);
36 int dd_main(int argc, char **argv)
38 #define sync_flag (1<<0)
39 #define noerror (1<<1)
40 #define trunc_flag (1<<2)
41 #define twobufs_flag (1<<3)
42 int flags = trunc_flag;
43 size_t count = -1, oc = 0, ibs = 512, obs = 512;
45 off_t seek = 0, skip = 0;
47 const char *infile = NULL, *outfile = NULL;
50 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
53 memset(&sa, 0, sizeof(sa));
54 sa.sa_handler = dd_output_status;
55 sa.sa_flags = SA_RESTART;
56 sigemptyset(&sa.sa_mask);
57 sigaction(SIGUSR1, &sa, 0);
60 for (n = 1; n < argc; n++) {
61 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", argv[n], 4)) {
62 ibs = bb_xparse_number(argv[n]+4, dd_suffixes);
63 flags |= twobufs_flag;
64 } else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", argv[n], 4)) {
65 obs = bb_xparse_number(argv[n]+4, dd_suffixes);
66 flags |= twobufs_flag;
67 } else if (!strncmp("bs=", argv[n], 3))
68 ibs = obs = bb_xparse_number(argv[n]+3, dd_suffixes);
69 else if (!strncmp("count=", argv[n], 6))
70 count = bb_xparse_number(argv[n]+6, dd_suffixes);
71 else if (!strncmp("seek=", argv[n], 5))
72 seek = bb_xparse_number(argv[n]+5, dd_suffixes);
73 else if (!strncmp("skip=", argv[n], 5))
74 skip = bb_xparse_number(argv[n]+5, dd_suffixes);
75 else if (!strncmp("if=", argv[n], 3))
77 else if (!strncmp("of=", argv[n], 3))
79 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", argv[n], 5)) {
82 if (!strncmp("notrunc", ibuf, 7)) {
85 } else if (!strncmp("sync", ibuf, 4)) {
88 } else if (!strncmp("noerror", ibuf, 7)) {
92 bb_error_msg_and_die(bb_msg_invalid_arg, argv[n]+5, "conv");
94 if (ibuf[0] == '\0') break;
95 if (ibuf[0] == ',') ibuf++;
102 if (flags & twobufs_flag)
108 ifd = xopen(infile, O_RDONLY);
111 infile = bb_msg_standard_input;
114 if (outfile != NULL) {
115 oflag = O_WRONLY | O_CREAT;
117 if (!seek && (flags & trunc_flag))
120 ofd = xopen3(outfile, oflag, 0666);
122 if (seek && (flags & trunc_flag)) {
123 if (ftruncate(ofd, seek * obs) < 0) {
126 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
133 outfile = bb_msg_standard_output;
137 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
139 n = safe_read(ifd, ibuf, ibs);
141 bb_perror_msg_and_die("%s", infile);
149 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
153 while (in_full + in_part != count) {
154 if (flags & noerror) {
155 /* Pre-zero the buffer when doing the noerror thing */
156 memset(ibuf, '\0', ibs);
159 n = safe_read(ifd, ibuf, ibs);
163 if (flags & noerror) {
165 bb_perror_msg("%s", infile);
167 bb_perror_msg_and_die("%s", infile);
169 if ((size_t)n == ibs)
174 memset(ibuf + n, '\0', ibs - n);
178 if (flags & twobufs_flag) {
185 memcpy(obuf + oc, tmp, d);
190 xwrite(ofd, obuf, obs);
196 xwrite(ofd, ibuf, n);
204 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
205 xwrite(ofd, obuf, oc);
208 if (close (ifd) < 0) {
209 bb_perror_msg_and_die("%s", infile);
212 if (close (ofd) < 0) {
214 bb_perror_msg_and_die("%s", outfile);