dd.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini dd implementation for busybox
  4. *
  5. *
  6. * Copyright (C) 2000,2001 Matt Kraai
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  9. */
  10. #include "busybox.h"
  11. #include <signal.h> /* For FEATURE_DD_SIGNAL_HANDLING */
  12. static const struct suffix_mult dd_suffixes[] = {
  13. { "c", 1 },
  14. { "w", 2 },
  15. { "b", 512 },
  16. { "kD", 1000 },
  17. { "k", 1024 },
  18. { "K", 1024 }, // compat with coreutils dd
  19. { "MD", 1000000 },
  20. { "M", 1048576 },
  21. { "GD", 1000000000 },
  22. { "G", 1073741824 },
  23. { NULL, 0 }
  24. };
  25. static off_t out_full, out_part, in_full, in_part;
  26. static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
  27. {
  28. fprintf(stderr, "%"OFF_FMT"d+%"OFF_FMT"d records in\n"
  29. "%"OFF_FMT"d+%"OFF_FMT"d records out\n",
  30. in_full, in_part,
  31. out_full, out_part);
  32. }
  33. static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
  34. const char* filename)
  35. {
  36. ssize_t n = full_write(fd, buf, len);
  37. if (n < 0)
  38. bb_perror_msg("writing '%s'", filename);
  39. return n;
  40. }
  41. #if ENABLE_LFS
  42. #define XATOU_SFX xatoull_sfx
  43. #else
  44. #define XATOU_SFX xatoul_sfx
  45. #endif
  46. int dd_main(int argc, char **argv)
  47. {
  48. enum {
  49. sync_flag = 1 << 0,
  50. noerror = 1 << 1,
  51. trunc_flag = 1 << 2,
  52. twobufs_flag = 1 << 3,
  53. };
  54. int flags = trunc_flag;
  55. size_t oc = 0, ibs = 512, obs = 512;
  56. ssize_t n, w;
  57. off_t seek = 0, skip = 0, count = OFF_T_MAX;
  58. int oflag, ifd, ofd;
  59. const char *infile = NULL, *outfile = NULL;
  60. char *ibuf, *obuf;
  61. if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
  62. struct sigaction sa;
  63. memset(&sa, 0, sizeof(sa));
  64. sa.sa_handler = dd_output_status;
  65. sa.sa_flags = SA_RESTART;
  66. sigemptyset(&sa.sa_mask);
  67. sigaction(SIGUSR1, &sa, 0);
  68. }
  69. for (n = 1; n < argc; n++) {
  70. char *arg = argv[n];
  71. /* Must fit into positive ssize_t */
  72. if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", arg, 4))
  73. ibs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
  74. else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", arg, 4))
  75. obs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
  76. else if (!strncmp("bs=", arg, 3))
  77. ibs = obs = xatoul_range_sfx(arg+3, 0, ((size_t)-1L)/2, dd_suffixes);
  78. /* These can be large: */
  79. else if (!strncmp("count=", arg, 6))
  80. count = XATOU_SFX(arg+6, dd_suffixes);
  81. else if (!strncmp("seek=", arg, 5))
  82. seek = XATOU_SFX(arg+5, dd_suffixes);
  83. else if (!strncmp("skip=", arg, 5))
  84. skip = XATOU_SFX(arg+5, dd_suffixes);
  85. else if (!strncmp("if=", arg, 3))
  86. infile = arg+3;
  87. else if (!strncmp("of=", arg, 3))
  88. outfile = arg+3;
  89. else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", arg, 5)) {
  90. arg += 5;
  91. while (1) {
  92. if (!strncmp("notrunc", arg, 7)) {
  93. flags &= ~trunc_flag;
  94. arg += 7;
  95. } else if (!strncmp("sync", arg, 4)) {
  96. flags |= sync_flag;
  97. arg += 4;
  98. } else if (!strncmp("noerror", arg, 7)) {
  99. flags |= noerror;
  100. arg += 7;
  101. } else {
  102. bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
  103. }
  104. if (arg[0] == '\0') break;
  105. if (*arg++ != ',') bb_show_usage();
  106. }
  107. } else
  108. bb_show_usage();
  109. }
  110. ibuf = obuf = xmalloc(ibs);
  111. if (ibs != obs) {
  112. flags |= twobufs_flag;
  113. obuf = xmalloc(obs);
  114. }
  115. if (infile != NULL)
  116. ifd = xopen(infile, O_RDONLY);
  117. else {
  118. ifd = STDIN_FILENO;
  119. infile = bb_msg_standard_input;
  120. }
  121. if (outfile != NULL) {
  122. oflag = O_WRONLY | O_CREAT;
  123. if (!seek && (flags & trunc_flag))
  124. oflag |= O_TRUNC;
  125. ofd = xopen(outfile, oflag);
  126. if (seek && (flags & trunc_flag)) {
  127. if (ftruncate(ofd, seek * obs) < 0) {
  128. struct stat st;
  129. if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
  130. S_ISDIR(st.st_mode))
  131. goto die_outfile;
  132. }
  133. }
  134. } else {
  135. ofd = STDOUT_FILENO;
  136. outfile = bb_msg_standard_output;
  137. }
  138. if (skip) {
  139. if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
  140. while (skip-- > 0) {
  141. n = safe_read(ifd, ibuf, ibs);
  142. if (n < 0)
  143. bb_perror_msg_and_die("%s", infile);
  144. if (n == 0)
  145. break;
  146. }
  147. }
  148. }
  149. if (seek) {
  150. if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
  151. goto die_outfile;
  152. }
  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);
  157. }
  158. n = safe_read(ifd, ibuf, ibs);
  159. if (n == 0)
  160. break;
  161. if (n < 0) {
  162. if (flags & noerror) {
  163. n = ibs;
  164. bb_perror_msg("%s", infile);
  165. } else
  166. bb_perror_msg_and_die("%s", infile);
  167. }
  168. if ((size_t)n == ibs)
  169. in_full++;
  170. else {
  171. in_part++;
  172. if (flags & sync_flag) {
  173. memset(ibuf + n, '\0', ibs - n);
  174. n = ibs;
  175. }
  176. }
  177. if (flags & twobufs_flag) {
  178. char *tmp = ibuf;
  179. while (n) {
  180. size_t d = obs - oc;
  181. if (d > n)
  182. d = n;
  183. memcpy(obuf + oc, tmp, d);
  184. n -= d;
  185. tmp += d;
  186. oc += d;
  187. if (oc == obs) {
  188. w = full_write_or_warn(ofd, obuf, obs, outfile);
  189. if (w < 0) goto out_status;
  190. if (w == obs)
  191. out_full++;
  192. else if (w > 0)
  193. out_part++;
  194. oc = 0;
  195. }
  196. }
  197. } else {
  198. w = full_write_or_warn(ofd, ibuf, n, outfile);
  199. if (w < 0) goto out_status;
  200. if (w == obs)
  201. out_full++;
  202. else if (w > 0)
  203. out_part++;
  204. }
  205. }
  206. if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
  207. w = full_write_or_warn(ofd, obuf, oc, outfile);
  208. if (w < 0) goto out_status;
  209. if (w > 0)
  210. out_part++;
  211. }
  212. if (close(ifd) < 0) {
  213. bb_perror_msg_and_die("%s", infile);
  214. }
  215. if (close(ofd) < 0) {
  216. die_outfile:
  217. bb_perror_msg_and_die("%s", outfile);
  218. }
  219. out_status:
  220. dd_output_status(0);
  221. return EXIT_SUCCESS;
  222. }