1 This patch adds support of ibs= and obs= to dd.
3 Hideki IWAMOTO h-iwamoto@kit.hi-ho.ne.jp
6 --- a/include/usage.h 29 Mar 2004 08:20:08 -0000 1.198
7 +++ b/include/usage.h 4 Apr 2004 07:15:21 -0000
11 #define dd_trivial_usage \
12 - "[if=FILE] [of=FILE] [bs=N] [count=N] [skip=N]\n" \
13 - "\t [seek=N] [conv=notrunc|noerror|sync]"
14 + "[if=FILE] [of=FILE] [ibs=N] [obs=N] [bs=N] [count=N]\n" \
15 + "\t [skip=N] [seek=N] [conv=notrunc|noerror|sync]"
16 #define dd_full_usage \
17 "Copy a file, converting and formatting according to options\n\n" \
18 "\tif=FILE\t\tread from FILE instead of stdin\n" \
19 "\tof=FILE\t\twrite to FILE instead of stdout\n" \
20 - "\tbs=N\t\tread and write N bytes at a time\n" \
21 + "\tibs=N\t\tread N bytes at a time\n" \
22 + "\tobs=N\t\twrite N bytes at a time\n" \
23 + "\tbs=N\t\tforce ibs=N and obs=N\n" \
24 "\tcount=N\t\tcopy only N input blocks\n" \
25 "\tskip=N\t\tskip N input blocks\n" \
26 "\tseek=N\t\tskip N output blocks\n" \
28 "\tconv=noerror\tcontinue after read errors\n" \
29 "\tconv=sync\tpad blocks with zeros\n" \
31 + "If the bs= expr operand is not specified, the input is processed and collected\n" \
32 + "into full-sized output blocks until the end of the input is reached.\n" \
33 "Numbers may be suffixed by c (x1), w (x2), b (x512), kD (x1000), k (x1024),\n" \
34 "MD (x1000000), M (x1048576), GD (x1000000000) or G (x1073741824)."
35 #define dd_example_usage \
36 --- a/coreutils/dd.c 30 Jan 2004 22:24:32 -0000 1.55
37 +++ b/coreutils/dd.c 4 Apr 2004 07:15:21 -0000
42 +#define C_NOERROR 0x0001
43 +#define C_TRUNC 0x0002
44 +#define C_SYNC 0x0004
45 +#define C_TWOBUFS 0x0008
47 static const struct suffix_mult dd_suffixes[] = {
60 - int sync_flag = FALSE;
61 - int noerror = FALSE;
62 - int trunc_flag = TRUE;
63 + unsigned int dd_flags = C_TWOBUFS | C_TRUNC;
68 const char *infile = NULL;
69 const char *outfile = NULL;
74 for (i = 1; i < argc; i++) {
75 - if (strncmp("bs=", argv[i], 3) == 0)
76 - bs = bb_xparse_number(argv[i]+3, dd_suffixes);
77 - else if (strncmp("count=", argv[i], 6) == 0)
78 + if (strncmp("ibs=", argv[i], 4) == 0)
79 + ibs = bb_xparse_number(argv[i]+4, dd_suffixes);
80 + else if (strncmp("obs=", argv[i], 4) == 0)
81 + obs = bb_xparse_number(argv[i]+4, dd_suffixes);
82 + else if (strncmp("bs=", argv[i], 3) == 0) {
83 + ibs = obs = bb_xparse_number(argv[i]+3, dd_suffixes);
84 + dd_flags &= ~C_TWOBUFS;
85 + } else if (strncmp("count=", argv[i], 6) == 0)
86 count = bb_xparse_number(argv[i]+6, dd_suffixes);
87 else if (strncmp("seek=", argv[i], 5) == 0)
88 seek = bb_xparse_number(argv[i]+5, dd_suffixes);
92 if (strncmp("notrunc", buf, 7) == 0) {
94 + dd_flags &= ~C_TRUNC;
96 } else if (strncmp("sync", buf, 4) == 0) {
100 } else if (strncmp("noerror", buf, 7) == 0) {
102 + dd_flags |= C_NOERROR;
105 bb_error_msg_and_die("invalid conversion `%s'", argv[i]+5);
111 + ibuf = xmalloc(ibs);
113 + if (dd_flags & C_TWOBUFS)
114 + obuf = xmalloc(obs);
118 if (infile != NULL) {
119 ifd = bb_xopen(infile, O_RDONLY);
121 if (outfile != NULL) {
122 oflag = O_WRONLY | O_CREAT;
124 - if (!seek && trunc_flag) {
125 + if (!seek && (dd_flags & C_TRUNC)) {
130 bb_perror_msg_and_die("%s", outfile);
133 - if (seek && trunc_flag) {
134 - if (ftruncate(ofd, seek * bs) < 0) {
135 + if (seek && (dd_flags & C_TRUNC)) {
136 + if (ftruncate(ofd, seek * obs) < 0) {
139 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
140 @@ -139,52 +155,88 @@
144 - if (lseek(ifd, skip * bs, SEEK_CUR) < 0) {
145 - bb_perror_msg_and_die("%s", infile);
146 + if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
147 + while (skip-- > 0) {
148 + n = safe_read(ifd, ibuf, ibs);
150 + bb_perror_msg_and_die("%s", infile);
158 - if (lseek(ofd, seek * bs, SEEK_CUR) < 0) {
159 + if (lseek(ofd, seek * obs, SEEK_CUR) < 0) {
160 bb_perror_msg_and_die("%s", outfile);
164 while (in_full + in_part != count) {
166 + if (dd_flags & C_NOERROR) {
167 /* Pre-zero the buffer when doing the noerror thing */
168 - memset(buf, '\0', bs);
169 + memset(ibuf, '\0', ibs);
172 + n = safe_read(ifd, ibuf, ibs);
176 - n = safe_read(ifd, buf, bs);
180 + if (dd_flags & C_NOERROR) {
182 bb_perror_msg("%s", infile);
184 bb_perror_msg_and_die("%s", infile);
195 + if (dd_flags & C_SYNC) {
196 + memset(ibuf + n, '\0', ibs - n);
201 - memset(buf + n, '\0', bs - n);
204 + if (dd_flags & C_TWOBUFS) {
212 + memcpy(obuf + oc, tmp, d);
217 + if (bb_full_write(ofd, obuf, obs) < 0) {
218 + bb_perror_msg_and_die("%s", outfile);
225 + if (bb_full_write(ofd, ibuf, n) < 0) {
226 + bb_perror_msg_and_die("%s", outfile);
234 - n = bb_full_write(ofd, buf, n);
239 + if (bb_full_write(ofd, obuf, oc) < 0) {
240 bb_perror_msg_and_die("%s", outfile);
250 if (close (ifd) < 0) {