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 FILEOFF_TYPE out_full, out_part, in_full, in_part;
30 static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
32 bb_fprintf(stderr, FILEOFF_FMT"+"FILEOFF_FMT" records in\n"
33 FILEOFF_FMT"+"FILEOFF_FMT" records out\n",
38 int dd_main(int argc, char **argv)
40 #define sync_flag (1<<0)
41 #define noerror (1<<1)
42 #define trunc_flag (1<<2)
43 #define twobufs_flag (1<<3)
44 int flags = trunc_flag;
45 size_t oc = 0, ibs = 512, obs = 512;
47 FILEOFF_TYPE seek = 0, skip = 0, count = MAX_FILEOFF_TYPE;
49 const char *infile = NULL, *outfile = NULL;
52 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
55 memset(&sa, 0, sizeof(sa));
56 sa.sa_handler = dd_output_status;
57 sa.sa_flags = SA_RESTART;
58 sigemptyset(&sa.sa_mask);
59 sigaction(SIGUSR1, &sa, 0);
62 for (n = 1; n < argc; n++) {
63 // FIXME: make them capable of eating LARGE numbers
64 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", argv[n], 4)) {
65 ibs = bb_xparse_number(argv[n]+4, dd_suffixes);
66 flags |= twobufs_flag;
67 } else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", argv[n], 4)) {
68 obs = bb_xparse_number(argv[n]+4, dd_suffixes);
69 flags |= twobufs_flag;
70 } else if (!strncmp("bs=", argv[n], 3))
71 ibs = obs = bb_xparse_number(argv[n]+3, dd_suffixes);
72 else if (!strncmp("count=", argv[n], 6))
73 count = bb_xparse_number(argv[n]+6, dd_suffixes);
74 else if (!strncmp("seek=", argv[n], 5))
75 seek = bb_xparse_number(argv[n]+5, dd_suffixes);
76 else if (!strncmp("skip=", argv[n], 5))
77 skip = bb_xparse_number(argv[n]+5, dd_suffixes);
78 else if (!strncmp("if=", argv[n], 3))
80 else if (!strncmp("of=", argv[n], 3))
82 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", argv[n], 5)) {
85 if (!strncmp("notrunc", ibuf, 7)) {
88 } else if (!strncmp("sync", ibuf, 4)) {
91 } else if (!strncmp("noerror", ibuf, 7)) {
95 bb_error_msg_and_die(bb_msg_invalid_arg, argv[n]+5, "conv");
97 if (ibuf[0] == '\0') break;
98 if (ibuf[0] == ',') ibuf++;
105 if (flags & twobufs_flag)
111 ifd = xopen(infile, O_RDONLY | (O_LARGEFILE * ENABLE_LFS));
114 infile = bb_msg_standard_input;
117 if (outfile != NULL) {
118 oflag = O_WRONLY | O_CREAT | (O_LARGEFILE * ENABLE_LFS);
120 if (!seek && (flags & trunc_flag))
123 ofd = xopen3(outfile, oflag, 0666);
125 if (seek && (flags & trunc_flag)) {
126 if (ftruncate(ofd, seek * obs) < 0) {
129 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
136 outfile = bb_msg_standard_output;
140 if (LSEEK(ifd, skip * ibs, SEEK_CUR) < 0) {
142 n = safe_read(ifd, ibuf, ibs);
144 bb_perror_msg_and_die("%s", infile);
152 if (LSEEK(ofd, seek * obs, SEEK_CUR) < 0)
156 while (in_full + in_part != count) {
157 if (flags & noerror) {
158 /* Pre-zero the buffer when doing the noerror thing */
159 memset(ibuf, '\0', ibs);
162 n = safe_read(ifd, ibuf, ibs);
166 if (flags & noerror) {
168 bb_perror_msg("%s", infile);
170 bb_perror_msg_and_die("%s", infile);
172 if ((size_t)n == ibs)
177 memset(ibuf + n, '\0', ibs - n);
181 if (flags & twobufs_flag) {
188 memcpy(obuf + oc, tmp, d);
193 xwrite(ofd, obuf, obs);
199 xwrite(ofd, ibuf, n);
207 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
208 xwrite(ofd, obuf, oc);
211 if (close (ifd) < 0) {
212 bb_perror_msg_and_die("%s", infile);
215 if (close (ofd) < 0) {
217 bb_perror_msg_and_die("%s", outfile);