dd.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <signal.h> /* For FEATURE_DD_SIGNAL_HANDLING */
  11. #include "libbb.h"
  12. /* This is a NOEXEC applet. Be very careful! */
  13. static const struct suffix_mult dd_suffixes[] = {
  14. { "c", 1 },
  15. { "w", 2 },
  16. { "b", 512 },
  17. { "kD", 1000 },
  18. { "k", 1024 },
  19. { "K", 1024 }, /* compat with coreutils dd */
  20. { "MD", 1000000 },
  21. { "M", 1048576 },
  22. { "GD", 1000000000 },
  23. { "G", 1073741824 },
  24. { NULL, 0 }
  25. };
  26. struct globals {
  27. off_t out_full, out_part, in_full, in_part;
  28. };
  29. #define G (*(struct globals*)&bb_common_bufsiz1)
  30. static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
  31. {
  32. fprintf(stderr, "%"OFF_FMT"d+%"OFF_FMT"d records in\n"
  33. "%"OFF_FMT"d+%"OFF_FMT"d records out\n",
  34. G.in_full, G.in_part,
  35. G.out_full, G.out_part);
  36. }
  37. static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
  38. const char * const filename)
  39. {
  40. ssize_t n = full_write(fd, buf, len);
  41. if (n < 0)
  42. bb_perror_msg("writing '%s'", filename);
  43. return n;
  44. }
  45. static bool write_and_stats(int fd, const void *buf, size_t len, size_t obs,
  46. const char * const filename)
  47. {
  48. ssize_t n = full_write_or_warn(fd, buf, len, filename);
  49. if (n < 0)
  50. return 1;
  51. if (n == obs)
  52. G.out_full++;
  53. else if (n) /* > 0 */
  54. G.out_part++;
  55. return 0;
  56. }
  57. #if ENABLE_LFS
  58. #define XATOU_SFX xatoull_sfx
  59. #else
  60. #define XATOU_SFX xatoul_sfx
  61. #endif
  62. int dd_main(int argc, char **argv);
  63. int dd_main(int argc, char **argv)
  64. {
  65. enum {
  66. SYNC_FLAG = 1 << 0,
  67. NOERROR = 1 << 1,
  68. TRUNC_FLAG = 1 << 2,
  69. TWOBUFS_FLAG = 1 << 3,
  70. };
  71. static const char * const keywords[] = {
  72. "bs=", "count=", "seek=", "skip=", "if=", "of=",
  73. #if ENABLE_FEATURE_DD_IBS_OBS
  74. "ibs=", "obs=", "conv=", "notrunc", "sync", "noerror",
  75. #endif
  76. NULL
  77. };
  78. enum {
  79. OP_bs = 1,
  80. OP_count,
  81. OP_seek,
  82. OP_skip,
  83. OP_if,
  84. OP_of,
  85. #if ENABLE_FEATURE_DD_IBS_OBS
  86. OP_ibs,
  87. OP_obs,
  88. OP_conv,
  89. OP_conv_notrunc,
  90. OP_conv_sync,
  91. OP_conv_noerror,
  92. #endif
  93. };
  94. int flags = TRUNC_FLAG;
  95. size_t oc = 0, ibs = 512, obs = 512;
  96. ssize_t n, w;
  97. off_t seek = 0, skip = 0, count = OFF_T_MAX;
  98. int ifd, ofd;
  99. const char *infile = NULL, *outfile = NULL;
  100. char *ibuf, *obuf;
  101. memset(&G, 0, sizeof(G)); /* because of NOEXEC */
  102. if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
  103. struct sigaction sa;
  104. memset(&sa, 0, sizeof(sa));
  105. sa.sa_handler = dd_output_status;
  106. sa.sa_flags = SA_RESTART;
  107. sigemptyset(&sa.sa_mask);
  108. sigaction(SIGUSR1, &sa, 0);
  109. }
  110. for (n = 1; n < argc; n++) {
  111. smalluint key_len;
  112. smalluint what;
  113. char *key;
  114. char *arg = argv[n];
  115. //XXX:FIXME: we reject plain "dd --" This would cost ~20 bytes, so..
  116. //if (*arg == '-' && *++arg == '-' && !*++arg) continue;
  117. key = strstr(arg, "=");
  118. if (key == NULL)
  119. bb_show_usage();
  120. key_len = key - arg + 1;
  121. key = xstrndup(arg, key_len);
  122. what = index_in_str_array(keywords, key) + 1;
  123. if (ENABLE_FEATURE_CLEAN_UP)
  124. free(key);
  125. if (what == 0)
  126. bb_show_usage();
  127. arg += key_len;
  128. /* Must fit into positive ssize_t */
  129. #if ENABLE_FEATURE_DD_IBS_OBS
  130. if (what == OP_ibs) {
  131. ibs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
  132. continue;
  133. }
  134. if (what == OP_obs) {
  135. obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
  136. continue;
  137. }
  138. if (what == OP_conv) {
  139. while (1) {
  140. /* find ',', replace them with nil so we can use arg for
  141. * index_in_str_array without copying.
  142. * We rely on arg being non-null, else strchr would fault.
  143. */
  144. key = strchr(arg, ',');
  145. if (key)
  146. *key = '\0';
  147. what = index_in_str_array(keywords, arg) + 1;
  148. if (what < OP_conv_notrunc)
  149. bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
  150. if (what == OP_conv_notrunc)
  151. flags &= ~TRUNC_FLAG;
  152. if (what == OP_conv_sync)
  153. flags |= SYNC_FLAG;
  154. if (what == OP_conv_noerror)
  155. flags |= NOERROR;
  156. if (!key) /* no ',' left, so this was the last specifier */
  157. break;
  158. arg = key + 1; /* skip this keyword and ',' */
  159. }
  160. continue;
  161. }
  162. #endif
  163. if (what == OP_bs) {
  164. ibs = obs = xatoul_range_sfx(arg, 1, ((size_t)-1L)/2, dd_suffixes);
  165. continue;
  166. }
  167. /* These can be large: */
  168. if (what == OP_count) {
  169. count = XATOU_SFX(arg, dd_suffixes);
  170. continue;
  171. }
  172. if (what == OP_seek) {
  173. seek = XATOU_SFX(arg, dd_suffixes);
  174. continue;
  175. }
  176. if (what == OP_skip) {
  177. skip = XATOU_SFX(arg, dd_suffixes);
  178. continue;
  179. }
  180. if (what == OP_if) {
  181. infile = arg;
  182. continue;
  183. }
  184. if (what == OP_of)
  185. outfile = arg;
  186. }
  187. //XXX:FIXME for huge ibs or obs, malloc'ing them isn't the brightest idea ever
  188. ibuf = obuf = xmalloc(ibs);
  189. if (ibs != obs) {
  190. flags |= TWOBUFS_FLAG;
  191. obuf = xmalloc(obs);
  192. }
  193. if (infile != NULL)
  194. ifd = xopen(infile, O_RDONLY);
  195. else {
  196. ifd = STDIN_FILENO;
  197. infile = bb_msg_standard_input;
  198. }
  199. if (outfile != NULL) {
  200. int oflag = O_WRONLY | O_CREAT;
  201. if (!seek && (flags & TRUNC_FLAG))
  202. oflag |= O_TRUNC;
  203. ofd = xopen(outfile, oflag);
  204. if (seek && (flags & TRUNC_FLAG)) {
  205. if (ftruncate(ofd, seek * obs) < 0) {
  206. struct stat st;
  207. if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
  208. S_ISDIR(st.st_mode))
  209. goto die_outfile;
  210. }
  211. }
  212. } else {
  213. ofd = STDOUT_FILENO;
  214. outfile = bb_msg_standard_output;
  215. }
  216. if (skip) {
  217. if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
  218. while (skip-- > 0) {
  219. n = safe_read(ifd, ibuf, ibs);
  220. if (n < 0)
  221. goto die_infile;
  222. if (n == 0)
  223. break;
  224. }
  225. }
  226. }
  227. if (seek) {
  228. if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
  229. goto die_outfile;
  230. }
  231. while (G.in_full + G.in_part != count) {
  232. if (flags & NOERROR) /* Pre-zero the buffer when for NOERROR */
  233. memset(ibuf, '\0', ibs);
  234. n = safe_read(ifd, ibuf, ibs);
  235. if (n == 0)
  236. break;
  237. if (n < 0) {
  238. if (flags & NOERROR) {
  239. n = ibs;
  240. bb_perror_msg("%s", infile);
  241. } else
  242. goto die_infile;
  243. }
  244. if ((size_t)n == ibs)
  245. G.in_full++;
  246. else {
  247. G.in_part++;
  248. if (flags & SYNC_FLAG) {
  249. memset(ibuf + n, '\0', ibs - n);
  250. n = ibs;
  251. }
  252. }
  253. if (flags & TWOBUFS_FLAG) {
  254. char *tmp = ibuf;
  255. while (n) {
  256. size_t d = obs - oc;
  257. if (d > n)
  258. d = n;
  259. memcpy(obuf + oc, tmp, d);
  260. n -= d;
  261. tmp += d;
  262. oc += d;
  263. if (oc == obs) {
  264. if (write_and_stats(ofd, obuf, obs, obs, outfile))
  265. goto out_status;
  266. oc = 0;
  267. }
  268. }
  269. } else if (write_and_stats(ofd, ibuf, n, obs, outfile))
  270. goto out_status;
  271. }
  272. if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
  273. w = full_write_or_warn(ofd, obuf, oc, outfile);
  274. if (w < 0) goto out_status;
  275. if (w > 0)
  276. G.out_part++;
  277. }
  278. if (close(ifd) < 0) {
  279. die_infile:
  280. bb_perror_msg_and_die("%s", infile);
  281. }
  282. if (close(ofd) < 0) {
  283. die_outfile:
  284. bb_perror_msg_and_die("%s", outfile);
  285. }
  286. out_status:
  287. dd_output_status(0);
  288. return EXIT_SUCCESS;
  289. }