1 /* vi: set sw=4 ts=4: */
3 * Mini dd implementation for busybox
5 * Copyright (C) 1999, 2000 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8 * Based in part on code taken from sash.
9 * Copyright (c) 1999 by David I. Bell
10 * Permission is granted to use, distribute, or modify this source,
11 * provided that this copyright notice remains intact.
13 * Permission to distribute this code under the GPL has been granted.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
40 typedef unsigned long long int uintmax_t;
43 static const char dd_usage[] =
44 "dd [if=FILE] [of=FILE] [bs=N] [count=N] [skip=N] [seek=N]\n"
45 #ifndef BB_FEATURE_TRIVIAL_HELP
46 "\nCopy a file, converting and formatting according to options\n\n"
47 "\tif=FILE\tread from FILE instead of stdin\n"
48 "\tof=FILE\twrite to FILE instead of stdout\n"
49 "\tbs=N\tread and write N bytes at a time\n"
50 "\tcount=N\tcopy only N input blocks\n"
51 "\tskip=N\tskip N input blocks\n"
52 "\tseek=N\tskip N output blocks\n"
54 "Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n"
60 extern int dd_main(int argc, char **argv)
70 uintmax_t skipBlocks = 0;
71 uintmax_t seekBlocks = 0;
72 uintmax_t count = (uintmax_t) - 1;
80 /* Parse any options */
82 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
83 inFile = ((strchr(*argv, '=')) + 1);
84 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
85 outFile = ((strchr(*argv, '=')) + 1);
86 else if (strncmp("count", *argv, 5) == 0) {
87 count = getNum((strchr(*argv, '=')) + 1);
89 fprintf(stderr, "Bad count value %s\n", *argv);
92 } else if (strncmp(*argv, "bs", 2) == 0) {
93 blockSize = getNum((strchr(*argv, '=')) + 1);
95 fprintf(stderr, "Bad block size value %s\n", *argv);
98 } else if (strncmp(*argv, "skip", 4) == 0) {
99 skipBlocks = getNum((strchr(*argv, '=')) + 1);
100 if (skipBlocks <= 0) {
101 fprintf(stderr, "Bad skip value %s\n", *argv);
105 } else if (strncmp(*argv, "seek", 4) == 0) {
106 seekBlocks = getNum((strchr(*argv, '=')) + 1);
107 if (seekBlocks <= 0) {
108 fprintf(stderr, "Bad seek value %s\n", *argv);
119 buf = xmalloc(blockSize);
125 inFd = fileno(stdin);
127 inFd = open(inFile, 0);
130 /* Note that we are not freeing buf or closing
131 * files here to save a few bytes. This exits
139 outFd = fileno(stdout);
141 outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
144 /* Note that we are not freeing buf or closing
145 * files here to save a few bytes. This exits
150 fatalError( outFile);
153 lseek(inFd, skipBlocks * blockSize, SEEK_SET);
154 lseek(outFd, seekBlocks * blockSize, SEEK_SET);
156 //TODO: Convert to using fullRead & fullWrite
159 while (outTotal < count * blockSize) {
160 inCc = read(inFd, buf, blockSize);
164 } else if (inCc == 0) {
170 while (intotal > outTotal) {
171 if (outTotal + inCc > count * blockSize)
172 inCc = count * blockSize - outTotal;
173 outCc = write(outFd, cp, inCc);
177 } else if (outCc == 0) {
191 /* Note that we are not freeing memory or closing
192 * files here, to save a few bytes. */
199 printf("%ld+%d records in\n", (long) (intotal / blockSize),
200 (intotal % blockSize) != 0);
201 printf("%ld+%d records out\n", (long) (outTotal / blockSize),
202 (outTotal % blockSize) != 0);