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]\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 stout\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"
51 "BYTES may be suffixed by w (x2), k (x1024), b (x512), or m (x1024^2).\n";
55 extern int dd_main (int argc, char **argv)
57 const char *inFile = NULL;
58 const char *outFile = NULL;
64 size_t blockSize = 512;
65 //uintmax_t skipBlocks = 0;
66 uintmax_t count = (uintmax_t)-1;
74 /* Parse any options */
76 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
77 inFile=((strchr(*argv, '='))+1);
78 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
79 outFile=((strchr(*argv, '='))+1);
80 else if (strncmp("count", *argv, 5) == 0) {
81 count = getNum ((strchr(*argv, '='))+1);
83 fprintf (stderr, "Bad count value %s\n", *argv);
87 else if (strncmp(*argv, "bs", 2) == 0) {
88 blockSize = getNum ((strchr(*argv, '='))+1);
90 fprintf (stderr, "Bad block size value %s\n", *argv);
95 else if (strncmp(*argv, "skip", 4) == 0) {
96 skipBlocks = atoi( *argv);
97 if (skipBlocks <= 0) {
98 fprintf (stderr, "Bad skip value %d\n", skipBlocks);
111 buf = malloc (blockSize);
113 fprintf (stderr, "Cannot allocate buffer\n");
121 inFd = fileno(stdin);
123 inFd = open (inFile, 0);
132 outFd = fileno(stdout);
134 outFd = creat (outFile, 0666);
143 //lseek(inFd, skipBlocks*blockSize, SEEK_SET);
145 //TODO: Convert to using fullRead & fullWrite
148 while (outTotal < count * blockSize) {
149 inCc = read (inFd, buf, blockSize);
153 } else if (inCc == 0) {
159 while (intotal > outTotal) {
160 if (outTotal + inCc > count * blockSize)
161 inCc = count * blockSize - outTotal;
162 outCc = write (outFd, cp, inCc);
166 } else if (outCc == 0) {
184 printf ("%ld+%d records in\n", (long)(intotal / blockSize),
185 (intotal % blockSize) != 0);
186 printf ("%ld+%d records out\n", (long)(outTotal / blockSize),
187 (outTotal % blockSize) != 0);