dd.c 7.0 KB

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