blob: 96ea32f06600672892cb26ecc522e6b8090cae04 [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
Matt Kraai24ac0172000-12-18 21:38:57 +000047int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000048{
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000049 enum {
50 sync_flag = 1 << 0,
51 noerror = 1 << 1,
52 trunc_flag = 1 << 2,
53 twobufs_flag = 1 << 3,
54 };
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000055 int flags = trunc_flag;
Denis Vlasenko56254152006-10-07 16:24:46 +000056 size_t oc = 0, ibs = 512, obs = 512;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000057 ssize_t n, w;
Denis Vlasenko7039a662006-10-08 17:54:47 +000058 off_t seek = 0, skip = 0, count = OFF_T_MAX;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000059 int oflag, ifd, ofd;
Rob Landley2686d3b2006-05-16 16:52:12 +000060 const char *infile = NULL, *outfile = NULL;
61 char *ibuf, *obuf;
Eric Andersenddea3682000-11-29 22:33:02 +000062
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000063 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) {
Rob Landleyc5598172006-05-02 22:44:04 +000064 struct sigaction sa;
65
66 memset(&sa, 0, sizeof(sa));
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000067 sa.sa_handler = dd_output_status;
Rob Landleyc5598172006-05-02 22:44:04 +000068 sa.sa_flags = SA_RESTART;
69 sigemptyset(&sa.sa_mask);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000070 sigaction(SIGUSR1, &sa, 0);
Rob Landleyc5598172006-05-02 22:44:04 +000071 }
72
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000073 for (n = 1; n < argc; n++) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000074 char *arg = argv[n];
75 /* Must fit into positive ssize_t */
76 if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", arg, 4))
77 ibs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
78 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", arg, 4))
79 obs = xatoul_range_sfx(arg+4, 0, ((size_t)-1L)/2, dd_suffixes);
80 else if (!strncmp("bs=", arg, 3))
81 ibs = obs = xatoul_range_sfx(arg+3, 0, ((size_t)-1L)/2, dd_suffixes);
Denis Vlasenko56254152006-10-07 16:24:46 +000082 // FIXME: make them capable of eating LARGE numbers
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000083 else if (!strncmp("count=", arg, 6))
84 count = xatoul_sfx(arg+6, dd_suffixes);
85 else if (!strncmp("seek=", arg, 5))
86 seek = xatoul_sfx(arg+5, dd_suffixes);
87 else if (!strncmp("skip=", arg, 5))
88 skip = xatoul_sfx(arg+5, dd_suffixes);
89
90 else if (!strncmp("if=", arg, 3))
91 infile = arg+3;
92 else if (!strncmp("of=", arg, 3))
93 outfile = arg+3;
94 else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", arg, 5)) {
95 arg += 5;
Matt Kraai24ac0172000-12-18 21:38:57 +000096 while (1) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000097 if (!strncmp("notrunc", arg, 7)) {
Denis Vlasenko56254152006-10-07 16:24:46 +000098 flags &= ~trunc_flag;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +000099 arg += 7;
100 } else if (!strncmp("sync", arg, 4)) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000101 flags |= sync_flag;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000102 arg += 4;
103 } else if (!strncmp("noerror", arg, 7)) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000104 flags |= noerror;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000105 arg += 7;
Matt Kraai24ac0172000-12-18 21:38:57 +0000106 } else {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000107 bb_error_msg_and_die(bb_msg_invalid_arg, arg, "conv");
Matt Kraai24ac0172000-12-18 21:38:57 +0000108 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000109 if (arg[0] == '\0') break;
110 if (*arg++ != ',') bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000112 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000113 bb_show_usage();
Eric Andersen3cf52d11999-10-12 22:26:06 +0000114 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000115
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000116 ibuf = obuf = xmalloc(ibs);
117 if (ibs != obs) {
118 flags |= twobufs_flag;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000119 obuf = xmalloc(obs);
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000120 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000122 if (infile != NULL)
Denis Vlasenko7039a662006-10-08 17:54:47 +0000123 ifd = xopen(infile, O_RDONLY);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000124 else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000125 ifd = STDIN_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 }
128
Matt Kraai24ac0172000-12-18 21:38:57 +0000129 if (outfile != NULL) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000130 oflag = O_WRONLY | O_CREAT;
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000131
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000132 if (!seek && (flags & trunc_flag))
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000133 oflag |= O_TRUNC;
134
Rob Landleyd921b2e2006-08-03 15:41:12 +0000135 ofd = xopen3(outfile, oflag, 0666);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000136
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000137 if (seek && (flags & trunc_flag)) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000138 if (ftruncate(ofd, seek * obs) < 0) {
Matt Kraaieb834782002-02-05 15:28:54 +0000139 struct stat st;
140
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000141 if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) ||
142 S_ISDIR(st.st_mode))
143 goto die_outfile;
Matt Kraaieb834782002-02-05 15:28:54 +0000144 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000145 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000146 } else {
147 ofd = STDOUT_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 }
150
Matt Kraai24ac0172000-12-18 21:38:57 +0000151 if (skip) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000152 if (lseek(ifd, skip * ibs, SEEK_CUR) < 0) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000153 while (skip-- > 0) {
154 n = safe_read(ifd, ibuf, ibs);
155 if (n < 0)
156 bb_perror_msg_and_die("%s", infile);
157 if (n == 0)
158 break;
159 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000160 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000161 }
Eric Andersenddea3682000-11-29 22:33:02 +0000162
Matt Kraai24ac0172000-12-18 21:38:57 +0000163 if (seek) {
Denis Vlasenko7039a662006-10-08 17:54:47 +0000164 if (lseek(ofd, seek * obs, SEEK_CUR) < 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000165 goto die_outfile;
Matt Kraai24ac0172000-12-18 21:38:57 +0000166 }
Eric Andersenddea3682000-11-29 22:33:02 +0000167
Matt Kraai24ac0172000-12-18 21:38:57 +0000168 while (in_full + in_part != count) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000169 if (flags & noerror) {
Eric Andersenef38b392002-04-27 01:31:43 +0000170 /* Pre-zero the buffer when doing the noerror thing */
Rob Landley2686d3b2006-05-16 16:52:12 +0000171 memset(ibuf, '\0', ibs);
Eric Andersenef38b392002-04-27 01:31:43 +0000172 }
Rob Landley2686d3b2006-05-16 16:52:12 +0000173
174 n = safe_read(ifd, ibuf, ibs);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000175 if (n == 0)
Rob Landley2686d3b2006-05-16 16:52:12 +0000176 break;
Eric Andersenef38b392002-04-27 01:31:43 +0000177 if (n < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000178 if (flags & noerror) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000179 n = ibs;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 bb_perror_msg("%s", infile);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000181 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 bb_perror_msg_and_die("%s", infile);
Eric Andersenef38b392002-04-27 01:31:43 +0000183 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000184 if ((size_t)n == ibs)
Matt Kraai24ac0172000-12-18 21:38:57 +0000185 in_full++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000186 else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000187 in_part++;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000188 if (flags & sync_flag) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000189 memset(ibuf + n, '\0', ibs - n);
190 n = ibs;
191 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000192 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000193 if (flags & twobufs_flag) {
Rob Landley2686d3b2006-05-16 16:52:12 +0000194 char *tmp = ibuf;
195 while (n) {
196 size_t d = obs - oc;
197
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000198 if (d > n)
199 d = n;
Rob Landley2686d3b2006-05-16 16:52:12 +0000200 memcpy(obuf + oc, tmp, d);
201 n -= d;
202 tmp += d;
203 oc += d;
204 if (oc == obs) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000205 w = full_write_or_warn(ofd, obuf, obs, outfile);
206 if (w < 0) goto out_status;
207 if (w == obs)
208 out_full++;
209 else if (w > 0)
210 out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000211 oc = 0;
212 }
213 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000214 } else {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000215 w = full_write_or_warn(ofd, ibuf, n, outfile);
216 if (w < 0) goto out_status;
217 if (w == obs)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000218 out_full++;
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000219 else if (w > 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000220 out_part++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000221 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000222 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000223
Rob Landley2686d3b2006-05-16 16:52:12 +0000224 if (ENABLE_FEATURE_DD_IBS_OBS && oc) {
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000225 w = full_write_or_warn(ofd, obuf, oc, outfile);
226 if (w < 0) goto out_status;
227 if (w > 0)
228 out_part++;
Rob Landley2686d3b2006-05-16 16:52:12 +0000229 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000230 if (close(ifd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000231 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000232 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000233
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000234 if (close(ofd) < 0) {
235 die_outfile:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000236 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000237 }
Denis Vlasenko3b8ff682006-10-31 15:55:56 +0000238 out_status:
Rob Landleyc5598172006-05-02 22:44:04 +0000239 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000240
Matt Kraai3e856ce2000-12-01 02:55:13 +0000241 return EXIT_SUCCESS;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000242}