2 * Mini dd implementation for busybox
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6 * based in part on code taken from sash.
8 * Copyright (c) 1999 by David I. Bell
9 * Permission is granted to use, distribute, or modify this source,
10 * provided that this copyright notice remains intact.
12 * Permission to distribute this code under the GPL has been granted.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
39 typedef unsigned long long int uintmax_t;
42 static const char dd_usage[] =
43 "dd [if=name] [of=name] [bs=n] [count=n] [skip=n] [seek=n]\n\n"
44 "Copy a file, converting and formatting according to options\n\n"
45 "\tif=FILE\tread from FILE instead of stdin\n"
46 "\tof=FILE\twrite to FILE instead of stdout\n"
47 "\tbs=n\tread and write n bytes at a time\n"
48 "\tcount=n\tcopy only n input blocks\n"
49 "\tskip=n\tskip n input blocks\n"
50 "\tseek=n\tskip n output blocks\n"
52 "Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n";
56 extern int dd_main (int argc, char **argv)
58 const char *inFile = NULL;
59 const char *outFile = NULL;
66 uintmax_t skipBlocks = 0;
67 uintmax_t seekBlocks = 0;
68 uintmax_t count = (uintmax_t)-1;
76 /* Parse any options */
78 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
79 inFile=((strchr(*argv, '='))+1);
80 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
81 outFile=((strchr(*argv, '='))+1);
82 else if (strncmp("count", *argv, 5) == 0) {
83 count = getNum ((strchr(*argv, '='))+1);
85 fprintf (stderr, "Bad count value %s\n", *argv);
89 else if (strncmp(*argv, "bs", 2) == 0) {
90 blockSize = getNum ((strchr(*argv, '='))+1);
92 fprintf (stderr, "Bad block size value %s\n", *argv);
96 else if (strncmp(*argv, "skip", 4) == 0) {
97 skipBlocks = getNum ((strchr(*argv, '='))+1);
98 if (skipBlocks <= 0) {
99 fprintf (stderr, "Bad skip value %s\n", *argv);
104 else if (strncmp(*argv, "seek", 4) == 0) {
105 seekBlocks = getNum ((strchr(*argv, '='))+1);
106 if (seekBlocks <= 0) {
107 fprintf (stderr, "Bad seek value %s\n", *argv);
119 buf = malloc (blockSize);
121 fprintf (stderr, "Cannot allocate buffer\n");
129 inFd = fileno(stdin);
131 inFd = open (inFile, 0);
140 outFd = fileno(stdout);
142 outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
151 lseek(inFd, skipBlocks*blockSize, SEEK_SET);
152 lseek(outFd, seekBlocks*blockSize, SEEK_SET);
154 //TODO: Convert to using fullRead & fullWrite
157 while (outTotal < count * blockSize) {
158 inCc = read (inFd, buf, blockSize);
162 } else if (inCc == 0) {
168 while (intotal > outTotal) {
169 if (outTotal + inCc > count * blockSize)
170 inCc = count * blockSize - outTotal;
171 outCc = write (outFd, cp, inCc);
175 } else if (outCc == 0) {
193 printf ("%ld+%d records in\n", (long)(intotal / blockSize),
194 (intotal % blockSize) != 0);
195 printf ("%ld+%d records out\n", (long)(outTotal / blockSize),
196 (outTotal % blockSize) != 0);