blob: 11508614fbc661cb75165c0ed056f95e606a700f [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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
Eric Andersencc8ed391999-10-05 16:24:54 +000022 */
23
Matt Kraai24ac0172000-12-18 21:38:57 +000024#include <sys/types.h>
Matt Kraaieb834782002-02-05 15:28:54 +000025#include <sys/stat.h>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <stdlib.h>
27#include <stdio.h>
28#include <unistd.h>
29#include <string.h>
Eric Andersen3cf52d11999-10-12 22:26:06 +000030#include <fcntl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000031#include "busybox.h"
32
Eric Andersencc8ed391999-10-05 16:24:54 +000033
Matt Kraaia164c642001-02-05 17:50:03 +000034static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000035 { "c", 1 },
36 { "w", 2 },
37 { "b", 512 },
38 { "kD", 1000 },
39 { "k", 1024 },
40 { "MD", 1000000 },
41 { "M", 1048576 },
42 { "GD", 1000000000 },
43 { "G", 1073741824 },
44 { NULL, 0 }
45};
46
47int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000048{
Glenn L McGratheaed78a2002-11-28 11:05:28 +000049 size_t out_full = 0;
50 size_t out_part = 0;
51 size_t in_full = 0;
52 size_t in_part = 0;
53 size_t count = -1;
54 size_t bs = 512;
Matt Kraai24ac0172000-12-18 21:38:57 +000055 ssize_t n;
Glenn L McGratheaed78a2002-11-28 11:05:28 +000056 off_t seek = 0;
57 off_t skip = 0;
58 int sync_flag = FALSE;
59 int noerror = FALSE;
60 int trunc = TRUE;
61 int oflag;
62 int ifd;
63 int ofd;
64 int i;
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 const char *infile = NULL;
66 const char *outfile = NULL;
Glenn L McGratheaed78a2002-11-28 11:05:28 +000067 char *buf;
Eric Andersenddea3682000-11-29 22:33:02 +000068
Matt Kraai24ac0172000-12-18 21:38:57 +000069 for (i = 1; i < argc; i++) {
70 if (strncmp("bs=", argv[i], 3) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 bs = bb_xparse_number(argv[i]+3, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000072 else if (strncmp("count=", argv[i], 6) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 count = bb_xparse_number(argv[i]+6, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000074 else if (strncmp("seek=", argv[i], 5) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 seek = bb_xparse_number(argv[i]+5, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000076 else if (strncmp("skip=", argv[i], 5) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 skip = bb_xparse_number(argv[i]+5, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000078 else if (strncmp("if=", argv[i], 3) == 0)
79 infile = argv[i]+3;
80 else if (strncmp("of=", argv[i], 3) == 0)
81 outfile = argv[i]+3;
82 else if (strncmp("conv=", argv[i], 5) == 0) {
83 buf = argv[i]+5;
84 while (1) {
85 if (strncmp("notrunc", buf, 7) == 0) {
86 trunc = FALSE;
87 buf += 7;
88 } else if (strncmp("sync", buf, 4) == 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +000089 sync_flag = TRUE;
Matt Kraai24ac0172000-12-18 21:38:57 +000090 buf += 4;
Eric Andersenef38b392002-04-27 01:31:43 +000091 } else if (strncmp("noerror", buf, 7) == 0) {
92 noerror = TRUE;
93 buf += 7;
Matt Kraai24ac0172000-12-18 21:38:57 +000094 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 bb_error_msg_and_die("invalid conversion `%s'", argv[i]+5);
Matt Kraai24ac0172000-12-18 21:38:57 +000096 }
97 if (buf[0] == '\0')
98 break;
99 if (buf[0] == ',')
100 buf++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000102 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 bb_show_usage();
Eric Andersen3cf52d11999-10-12 22:26:06 +0000104 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000105
Matt Kraai24ac0172000-12-18 21:38:57 +0000106 buf = xmalloc(bs);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107
Matt Kraai24ac0172000-12-18 21:38:57 +0000108 if (infile != NULL) {
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000109 if ((ifd = open(infile, O_RDONLY)) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000111 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000112 } else {
113 ifd = STDIN_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115 }
116
Matt Kraai24ac0172000-12-18 21:38:57 +0000117 if (outfile != NULL) {
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000118 oflag = O_WRONLY | O_CREAT;
119
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000120 if (!seek && trunc) {
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000121 oflag |= O_TRUNC;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000122 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000123
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000124 if ((ofd = open(outfile, oflag, 0666)) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000126 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000127
128 if (seek && trunc) {
Matt Kraaieb834782002-02-05 15:28:54 +0000129 if (ftruncate(ofd, seek * bs) < 0) {
130 struct stat st;
131
132 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000133 S_ISDIR (st.st_mode)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000135 }
Matt Kraaieb834782002-02-05 15:28:54 +0000136 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000137 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000138 } else {
139 ofd = STDOUT_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 }
142
Matt Kraai24ac0172000-12-18 21:38:57 +0000143 if (skip) {
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000144 if (lseek(ifd, skip * bs, SEEK_CUR) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000146 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000147 }
Eric Andersenddea3682000-11-29 22:33:02 +0000148
Matt Kraai24ac0172000-12-18 21:38:57 +0000149 if (seek) {
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000150 if (lseek(ofd, seek * bs, SEEK_CUR) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000152 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000153 }
Eric Andersenddea3682000-11-29 22:33:02 +0000154
Matt Kraai24ac0172000-12-18 21:38:57 +0000155 while (in_full + in_part != count) {
Eric Andersenef38b392002-04-27 01:31:43 +0000156 if (noerror) {
157 /* Pre-zero the buffer when doing the noerror thing */
158 memset(buf, '\0', bs);
159 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000160 n = safe_read(ifd, buf, bs);
Eric Andersenef38b392002-04-27 01:31:43 +0000161 if (n < 0) {
162 if (noerror) {
163 n = bs;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164 bb_perror_msg("%s", infile);
Eric Andersenef38b392002-04-27 01:31:43 +0000165 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166 bb_perror_msg_and_die("%s", infile);
Eric Andersenef38b392002-04-27 01:31:43 +0000167 }
168 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000169 if (n == 0) {
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000170 break;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000171 }
172 if (n == bs) {
Matt Kraai24ac0172000-12-18 21:38:57 +0000173 in_full++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000174 } else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000175 in_part++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000176 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000177 if (sync_flag) {
Matt Kraai24ac0172000-12-18 21:38:57 +0000178 memset(buf + n, '\0', bs - n);
179 n = bs;
Eric Andersenddea3682000-11-29 22:33:02 +0000180 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000181 n = bb_full_write(ofd, buf, n);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000182 if (n < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000184 }
185 if (n == bs) {
Matt Kraai24ac0172000-12-18 21:38:57 +0000186 out_full++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000187 } else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000188 out_part++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000189 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000190 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000191
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000192 if (close (ifd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000193 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000194 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000195
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000196 if (close (ofd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000197 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000198 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000199
Manuel Novoa III cad53642003-03-19 09:13:01 +0000200 fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n",
201 (long)in_full, (long)in_part,
202 (long)out_full, (long)out_part);
Matt Kraai24ac0172000-12-18 21:38:57 +0000203
Matt Kraai3e856ce2000-12-01 02:55:13 +0000204 return EXIT_SUCCESS;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000205}