2 * Copyright (c) 1999 by David I. Bell
3 * Permission is granted to use, distribute, or modify this source,
4 * provided that this copyright notice remains intact.
6 * The "dd" command, originally taken from sash.
8 * Permission to distribute this code under the GPL has been granted.
9 * Majorly modified, and bugs fixed for busybox by Erik Andersen <andersee@debian.org> <andersen@lineo.com>
15 const char dd_usage[] =
16 "Copy a file, converting and formatting according to options\n\
18 usage: [if=name] [of=name] [bs=n] [count=n]\n\
19 \tif=FILE\tread from FILE instead of stdin\n\
20 \tof=FILE\twrite to FILE instead of stout\n\
21 \tbs=n\tread and write N bytes at a time\n\
22 \tcount=n\tcopy only n input blocks\n\
24 BYTES may be suffixed: by k for x1024, b for x512, and w for x2.\n";
49 static const PARAM params[] =
59 static long getNum(const char * cp);
62 dd_main (int argc, char **argv)
78 unsigned char localBuf[BUF_SIZE];
89 cp = strchr(str, '=');
93 fprintf(stderr, "Bad dd argument\n");
99 for (par = params; par->name; par++)
101 if (strcmp(str, par->name) == 0)
110 fprintf(stderr, "Multiple input files illegal\n");
114 //fprintf(stderr, "if=%s\n", cp);
121 fprintf(stderr, "Multiple output files illegal\n");
125 //fprintf(stderr, "of=%s\n", cp);
130 blockSize = getNum(cp);
131 //fprintf(stderr, "bs=%d\n", blockSize);
135 fprintf(stderr, "Bad block size value\n");
143 //fprintf(stderr, "count=%ld\n", count);
147 fprintf(stderr, "Bad count value\n");
160 if (blockSize > sizeof(localBuf))
162 buf = malloc(blockSize);
166 fprintf(stderr, "Cannot allocate buffer\n");
177 inFd = open(inFile, 0);
192 outFd = creat(outFile, 0666);
205 while ( outTotal < count*blockSize )
207 inCc = read(inFd, buf, blockSize);
212 //fprintf(stderr, "read in =%d\n", inCc);
217 while ( intotal > outTotal )
219 if (outTotal+inCc > count*blockSize)
220 inCc=count*blockSize-outTotal;
221 outCc = write(outFd, cp, inCc);
227 //fprintf(stderr, "wrote out =%d\n", outCc);
232 //fprintf(stderr, "outTotal=%ld\n", outTotal);
242 if (close(outFd) < 0)
248 printf("%ld+%d records in\n", intotal / blockSize,
249 (intotal % blockSize) != 0);
251 printf("%ld+%d records out\n", outTotal / blockSize,
252 (outTotal % blockSize) != 0);
256 fprintf(stderr, "%s", dd_usage);
262 * Read a number with a possible multiplier.
263 * Returns -1 if the number format is illegal.
266 getNum(const char * cp)
275 while (isDecimal(*cp))
276 value = value * 10 + *cp++ - '0';