blob: 01f37abeb56d9fdbe3e3724c3be4ec1e5c812adf [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Eric Andersenc4996011999-10-20 22:08:37 +00003 * Mini dd implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 *
Matt Kraai3eeab3b2001-12-07 16:27:37 +00006 * Copyright (C) 2000,2001 Matt Kraai
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +00009 */
10
Eric Andersencbe31da2001-02-20 06:14:08 +000011#include "busybox.h"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000012#include <signal.h> /* For FEATURE_DD_SIGNAL_HANDLING */
Eric Andersencbe31da2001-02-20 06:14:08 +000013
Matt Kraaia164c642001-02-05 17:50:03 +000014static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000015 { "c", 1 },
16 { "w", 2 },
17 { "b", 512 },
18 { "kD", 1000 },
19 { "k", 1024 },
Denis Vlasenko56254152006-10-07 16:24:46 +000020 { "K", 1024 }, // compat with coreutils dd
Matt Kraai24ac0172000-12-18 21:38:57 +000021 { "MD", 1000000 },
22 { "M", 1048576 },
23 { "GD", 1000000000 },
24 { "G", 1073741824 },
25 { NULL, 0 }
26};
27
Denis Vlasenko7039a662006-10-08 17:54:47 +000028static off_t out_full, out_part, in_full, in_part;
Rob Landleyc5598172006-05-02 22:44:04 +000029
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000030static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal)
Rob Landleyc5598172006-05-02 22:44:04 +000031{
Denis Vlasenkocf30cc82006-11-24 14:53:18 +000032 fprintf(stderr, "%"OFF_FMT"d+%"OFF_FMT"d records in\n"
33 "%"OFF_FMT"d+%"OFF_FMT"d records out\n",
Denis Vlasenko56254152006-10-07 16:24:46 +000034 in_full, in_part,
35 out_full, out_part);
Rob Landleyc5598172006-05-02 22:44:04 +000036}
37
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000038static ssize_t full_write_or_warn(int fd, const void *buf, size_t len,
39 const char* filename)
40{
41 ssize_t n = full_write(fd, buf, len);
42 if (n < 0)
43 bb_perror_msg("writing '%s'", filename);
44 return n;
45}
46
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000047#if ENABLE_LFS
48#define XATOU_SFX xatoull_sfx
49#else
50#define XATOU_SFX xatoul_sfx
51#endif
52
Matt Kraai24ac0172000-12-18 21:38:57 +000053int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000054{
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000055 enum {
56 sync_flag = 1 << 0,
57 noerror = 1 << 1,
58 trunc_flag = 1 << 2,
59 twobufs_flag = 1 << 3,
60 };
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000061 int flags = trunc_flag;
Denis Vlasenko56254152006-10-07 16:24:46 +000062 size_t oc = 0, ibs = 512, obs = 512;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000063 ssize_t n, w;
Denis Vlasenko7039a662006-10-08 17:54:47 +000064 off_t seek = 0, skip = 0, count = OFF_T_MAX;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000065 int oflag, ifd, ofd;
Rob Landley2686d3b2006-05-16 16:52:12 +000066 const char *infile = NULL, *outfile = NULL;
67 char *ibuf, *obuf;
Eric Andersenddea3682000-11-29 22:33:02 +000068
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000069 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
Rob Landleyc5598172006-05-02 22:44:04 +000070 struct sigaction sa;
71
72 memset(&sa, 0, sizeof(sa));
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000073 sa.sa_handler = dd_output_status;
Rob Landleyc5598172006-05-02 22:44:04 +000074 sa.sa_flags = SA_RESTART;
75 sigemptyset(&sa.sa_mask);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000076 sigaction(SIGUSR1, &sa, 0);
Rob Landleyc5598172006-05-02 22:44:04 +000077 }
78
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000079 for (n = 1; n < argc; n++) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000080 char *arg = argv[n];
81 /* Must fit into positive ssize_t */
82 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", arg, 4))
83 ibs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
84 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", arg, 4))
85 obs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
86 else if (!strncmp("bs=", arg, 3))
87 ibs = obs = xatoul_range_sfx(arg+3, 0, ((size_t)-1L)/2, dd_suffixes);
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000088 /* These can be large: */
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000089 else if (!strncmp("count=", arg, 6))
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000090 count = XATOU_SFX(arg+6, dd_suffixes);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000091 else if (!strncmp("seek=", arg, 5))
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000092 seek = XATOU_SFX(arg+5, dd_suffixes);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000093 else if (!strncmp("skip=", arg, 5))
Denis Vlasenko5dd27b12006-11-25 14:46:21 +000094 skip = XATOU_SFX(arg+5, dd_suffixes);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000095
96 else if (!strncmp("if=", arg, 3))
97 infile = arg+3;
98 else if (!strncmp("of=", arg, 3))
99 outfile = arg+3;
100 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", arg, 5)) {
101 arg += 5;
Matt Kraai24ac0172000-12-18 21:38:57 +0000102 while (1) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000103 if (!strncmp("notrunc", arg, 7)) {
Denis Vlasenko56254152006-10-07 16:24:46 +0000104 flags &= ~trunc_flag;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000105 arg += 7;
106 } else if (!strncmp("sync", arg, 4)) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000107 flags |= sync_flag;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000108 arg += 4;
109 } else if (!strncmp("noerror", arg, 7)) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000110 flags |= noerror;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000111 arg += 7;
Matt Kraai24ac0172000-12-18 21:38:57 +0000112 } else {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000113 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
Matt Kraai24ac0172000-12-18 21:38:57 +0000114 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000115 if (arg[0] == '\0') break;
116 if (*arg++ != ',') bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000118 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 bb_show_usage();
Eric Andersen3cf52d11999-10-12 22:26:06 +0000120 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000121
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000122 ibuf = obuf = xmalloc(ibs);
123 if (ibs != obs) {
124 flags |= twobufs_flag;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000125 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000126 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000128 if (infile != NULL)
Denis Vlasenko7039a662006-10-08 17:54:47 +0000129 ifd = xopen(infile, O_RDONLY);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000130 else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000131 ifd = STDIN_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000132 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000133 }
134
Matt Kraai24ac0172000-12-18 21:38:57 +0000135 if (outfile != NULL) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000136 oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000137
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000138 if (!seek && (flags & trunc_flag))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000139 oflag |= O_TRUNC;
140
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000141 ofd = xopen(outfile, oflag);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000142
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000143 if (seek && (flags & trunc_flag)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000144 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000145 struct stat st;
146
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000147 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
148 S_ISDIR(st.st_mode))
149 goto die_outfile;
Matt Kraaieb834782002-02-05 15:28:54 +0000150 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000151 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000152 } else {
153 ofd = STDOUT_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155 }
156
Matt Kraai24ac0172000-12-18 21:38:57 +0000157 if (skip) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000158 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000159 while (skip-- > 0) {
160 n = safe_read(ifd, ibuf, ibs);
161 if (n < 0)
162 bb_perror_msg_and_die("%s", infile);
163 if (n == 0)
164 break;
165 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000166 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000167 }
Eric Andersenddea3682000-11-29 22:33:02 +0000168
Matt Kraai24ac0172000-12-18 21:38:57 +0000169 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000170 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000171 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000172 }
Eric Andersenddea3682000-11-29 22:33:02 +0000173
Matt Kraai24ac0172000-12-18 21:38:57 +0000174 while (in_full + in_part != count) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000175 if (flags & noerror) {
Eric Andersenef38b392002-04-27 01:31:43 +0000176 /* Pre-zero the buffer when doing the noerror thing */
Rob Landley2686d3b2006-05-16 16:52:12 +0000177 memset(ibuf, '\0', ibs);
Eric Andersenef38b392002-04-27 01:31:43 +0000178 }
Rob Landley2686d3b2006-05-16 16:52:12 +0000179
180 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000181 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000182 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000183 if (n < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000184 if (flags & noerror) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000185 n = ibs;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000186 bb_perror_msg("%s", infile);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000187 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 bb_perror_msg_and_die("%s", infile);
Eric Andersenef38b392002-04-27 01:31:43 +0000189 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000190 if ((size_t)n == ibs)
Matt Kraai24ac0172000-12-18 21:38:57 +0000191 in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000192 else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000193 in_part++;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000194 if (flags & sync_flag) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000195 memset(ibuf + n, '\0', ibs - n);
196 n = ibs;
197 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000198 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000199 if (flags & twobufs_flag) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000200 char *tmp = ibuf;
201 while (n) {
202 size_t d = obs - oc;
203
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000204 if (d > n)
205 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000206 memcpy(obuf + oc, tmp, d);
207 n -= d;
208 tmp += d;
209 oc += d;
210 if (oc == obs) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000211 w = full_write_or_warn(ofd, obuf, obs, outfile);
212 if (w < 0) goto out_status;
213 if (w == obs)
214 out_full++;
215 else if (w > 0)
216 out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000217 oc = 0;
218 }
219 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000220 } else {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000221 w = full_write_or_warn(ofd, ibuf, n, outfile);
222 if (w < 0) goto out_status;
223 if (w == obs)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000224 out_full++;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000225 else if (w > 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000226 out_part++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000227 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000228 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000229
Rob Landley2686d3b2006-05-16 16:52:12 +0000230 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000231 w = full_write_or_warn(ofd, obuf, oc, outfile);
232 if (w < 0) goto out_status;
233 if (w > 0)
234 out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000235 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000236 if (close(ifd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000237 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000238 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000239
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000240 if (close(ofd) < 0) {
241 die_outfile:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000242 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000243 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000244 out_status:
Rob Landleyc5598172006-05-02 22:44:04 +0000245 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000246
Matt Kraai3e856ce2000-12-01 02:55:13 +0000247 return EXIT_SUCCESS;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000248}