dd_ibs_and_obs.diff 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. This patch adds support of ibs= and obs= to dd.
  2. ----
  3. Hideki IWAMOTO h-iwamoto@kit.hi-ho.ne.jp
  4. --- a/include/usage.h 29 Mar 2004 08:20:08 -0000 1.198
  5. +++ b/include/usage.h 4 Apr 2004 07:15:21 -0000
  6. @@ -309,13 +309,15 @@
  7. "64\n"
  8. #define dd_trivial_usage \
  9. - "[if=FILE] [of=FILE] [bs=N] [count=N] [skip=N]\n" \
  10. - "\t [seek=N] [conv=notrunc|noerror|sync]"
  11. + "[if=FILE] [of=FILE] [ibs=N] [obs=N] [bs=N] [count=N]\n" \
  12. + "\t [skip=N] [seek=N] [conv=notrunc|noerror|sync]"
  13. #define dd_full_usage \
  14. "Copy a file, converting and formatting according to options\n\n" \
  15. "\tif=FILE\t\tread from FILE instead of stdin\n" \
  16. "\tof=FILE\t\twrite to FILE instead of stdout\n" \
  17. - "\tbs=N\t\tread and write N bytes at a time\n" \
  18. + "\tibs=N\t\tread N bytes at a time\n" \
  19. + "\tobs=N\t\twrite N bytes at a time\n" \
  20. + "\tbs=N\t\tforce ibs=N and obs=N\n" \
  21. "\tcount=N\t\tcopy only N input blocks\n" \
  22. "\tskip=N\t\tskip N input blocks\n" \
  23. "\tseek=N\t\tskip N output blocks\n" \
  24. @@ -323,6 +325,8 @@
  25. "\tconv=noerror\tcontinue after read errors\n" \
  26. "\tconv=sync\tpad blocks with zeros\n" \
  27. "\n" \
  28. + "If the bs= expr operand is not specified, the input is processed and collected\n" \
  29. + "into full-sized output blocks until the end of the input is reached.\n" \
  30. "Numbers may be suffixed by c (x1), w (x2), b (x512), kD (x1000), k (x1024),\n" \
  31. "MD (x1000000), M (x1048576), GD (x1000000000) or G (x1073741824)."
  32. #define dd_example_usage \
  33. --- a/coreutils/dd.c 30 Jan 2004 22:24:32 -0000 1.55
  34. +++ b/coreutils/dd.c 4 Apr 2004 07:15:21 -0000
  35. @@ -30,6 +30,10 @@
  36. #include <fcntl.h>
  37. #include "busybox.h"
  38. +#define C_NOERROR 0x0001
  39. +#define C_TRUNC 0x0002
  40. +#define C_SYNC 0x0004
  41. +#define C_TWOBUFS 0x0008
  42. static const struct suffix_mult dd_suffixes[] = {
  43. { "c", 1 },
  44. @@ -51,13 +55,13 @@
  45. size_t in_full = 0;
  46. size_t in_part = 0;
  47. size_t count = -1;
  48. - size_t bs = 512;
  49. + size_t ibs = 512;
  50. + size_t obs = 512;
  51. + size_t oc = 0;
  52. ssize_t n;
  53. off_t seek = 0;
  54. off_t skip = 0;
  55. - int sync_flag = FALSE;
  56. - int noerror = FALSE;
  57. - int trunc_flag = TRUE;
  58. + unsigned int dd_flags = C_TWOBUFS | C_TRUNC;
  59. int oflag;
  60. int ifd;
  61. int ofd;
  62. @@ -65,11 +69,18 @@
  63. const char *infile = NULL;
  64. const char *outfile = NULL;
  65. char *buf;
  66. + char *ibuf;
  67. + char *obuf;
  68. for (i = 1; i < argc; i++) {
  69. - if (strncmp("bs=", argv[i], 3) == 0)
  70. - bs = bb_xparse_number(argv[i]+3, dd_suffixes);
  71. - else if (strncmp("count=", argv[i], 6) == 0)
  72. + if (strncmp("ibs=", argv[i], 4) == 0)
  73. + ibs = bb_xparse_number(argv[i]+4, dd_suffixes);
  74. + else if (strncmp("obs=", argv[i], 4) == 0)
  75. + obs = bb_xparse_number(argv[i]+4, dd_suffixes);
  76. + else if (strncmp("bs=", argv[i], 3) == 0) {
  77. + ibs = obs = bb_xparse_number(argv[i]+3, dd_suffixes);
  78. + dd_flags &= ~C_TWOBUFS;
  79. + } else if (strncmp("count=", argv[i], 6) == 0)
  80. count = bb_xparse_number(argv[i]+6, dd_suffixes);
  81. else if (strncmp("seek=", argv[i], 5) == 0)
  82. seek = bb_xparse_number(argv[i]+5, dd_suffixes);
  83. @@ -83,13 +94,13 @@
  84. buf = argv[i]+5;
  85. while (1) {
  86. if (strncmp("notrunc", buf, 7) == 0) {
  87. - trunc_flag = FALSE;
  88. + dd_flags &= ~C_TRUNC;
  89. buf += 7;
  90. } else if (strncmp("sync", buf, 4) == 0) {
  91. - sync_flag = TRUE;
  92. + dd_flags |= C_SYNC;
  93. buf += 4;
  94. } else if (strncmp("noerror", buf, 7) == 0) {
  95. - noerror = TRUE;
  96. + dd_flags |= C_NOERROR;
  97. buf += 7;
  98. } else {
  99. bb_error_msg_and_die("invalid conversion `%s'", argv[i]+5);
  100. @@ -103,7 +114,12 @@
  101. bb_show_usage();
  102. }
  103. - buf = xmalloc(bs);
  104. + ibuf = xmalloc(ibs);
  105. +
  106. + if (dd_flags & C_TWOBUFS)
  107. + obuf = xmalloc(obs);
  108. + else
  109. + obuf = ibuf;
  110. if (infile != NULL) {
  111. ifd = bb_xopen(infile, O_RDONLY);
  112. @@ -115,7 +131,7 @@
  113. if (outfile != NULL) {
  114. oflag = O_WRONLY | O_CREAT;
  115. - if (!seek && trunc_flag) {
  116. + if (!seek && (dd_flags & C_TRUNC)) {
  117. oflag |= O_TRUNC;
  118. }
  119. @@ -123,8 +139,8 @@
  120. bb_perror_msg_and_die("%s", outfile);
  121. }
  122. - if (seek && trunc_flag) {
  123. - if (ftruncate(ofd, seek * bs) < 0) {
  124. + if (seek && (dd_flags & C_TRUNC)) {
  125. + if (ftruncate(ofd, seek * obs) < 0) {
  126. struct stat st;
  127. if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
  128. @@ -139,52 +155,88 @@
  129. }
  130. if (skip) {
  131. - if (lseek(ifd, skip * bs, SEEK_CUR) < 0) {
  132. - bb_perror_msg_and_die("%s", infile);
  133. + if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
  134. + while (skip-- > 0) {
  135. + n = safe_read(ifd, ibuf, ibs);
  136. + if (n < 0)
  137. + bb_perror_msg_and_die("%s", infile);
  138. + if (n == 0)
  139. + break;
  140. + }
  141. }
  142. }
  143. if (seek) {
  144. - if (lseek(ofd, seek * bs, SEEK_CUR) < 0) {
  145. + if (lseek(ofd, seek * obs, SEEK_CUR) < 0) {
  146. bb_perror_msg_and_die("%s", outfile);
  147. }
  148. }
  149. while (in_full + in_part != count) {
  150. - if (noerror) {
  151. + if (dd_flags & C_NOERROR) {
  152. /* Pre-zero the buffer when doing the noerror thing */
  153. - memset(buf, '\0', bs);
  154. + memset(ibuf, '\0', ibs);
  155. + }
  156. +
  157. + n = safe_read(ifd, ibuf, ibs);
  158. + if (n == 0) {
  159. + break;
  160. }
  161. - n = safe_read(ifd, buf, bs);
  162. if (n < 0) {
  163. - if (noerror) {
  164. - n = bs;
  165. + if (dd_flags & C_NOERROR) {
  166. + n = ibs;
  167. bb_perror_msg("%s", infile);
  168. } else {
  169. bb_perror_msg_and_die("%s", infile);
  170. }
  171. }
  172. - if (n == 0) {
  173. - break;
  174. - }
  175. - if (n == bs) {
  176. + if (n == ibs) {
  177. in_full++;
  178. } else {
  179. in_part++;
  180. + if (dd_flags & C_SYNC) {
  181. + memset(ibuf + n, '\0', ibs - n);
  182. + n = ibs;
  183. + }
  184. }
  185. - if (sync_flag) {
  186. - memset(buf + n, '\0', bs - n);
  187. - n = bs;
  188. +
  189. + if (dd_flags & C_TWOBUFS) {
  190. + size_t d;
  191. + char *tmp = ibuf;
  192. +
  193. + while (n) {
  194. + d = obs - oc;
  195. + if (d > n)
  196. + d = n;
  197. + memcpy(obuf + oc, tmp, d);
  198. + n -= d;
  199. + tmp += d;
  200. + oc += d;
  201. + if (oc == obs) {
  202. + if (bb_full_write(ofd, obuf, obs) < 0) {
  203. + bb_perror_msg_and_die("%s", outfile);
  204. + }
  205. + out_full++;
  206. + oc = 0;
  207. + }
  208. + }
  209. + } else {
  210. + if (bb_full_write(ofd, ibuf, n) < 0) {
  211. + bb_perror_msg_and_die("%s", outfile);
  212. + }
  213. + if (n == ibs) {
  214. + out_full++;
  215. + } else {
  216. + out_part++;
  217. + }
  218. }
  219. - n = bb_full_write(ofd, buf, n);
  220. - if (n < 0) {
  221. + }
  222. +
  223. + if (oc) {
  224. + if (bb_full_write(ofd, obuf, oc) < 0) {
  225. bb_perror_msg_and_die("%s", outfile);
  226. }
  227. - if (n == bs) {
  228. - out_full++;
  229. - } else {
  230. - out_part++;
  231. - }
  232. + out_part++;
  233. }
  234. if (close (ifd) < 0) {