My previous attempt to make dd use fullRead, fullWrite was very broken,

this should actually work.
This commit is contained in:
Glenn L McGrath 2000-09-10 01:54:27 +00:00
parent bd7c67136a
commit 0ae8e5a645
2 changed files with 28 additions and 26 deletions

View File

@ -52,9 +52,11 @@ extern int dd_main(int argc, char **argv)
uintmax_t skipBlocks = 0; uintmax_t skipBlocks = 0;
uintmax_t seekBlocks = 0; uintmax_t seekBlocks = 0;
uintmax_t count = (uintmax_t) - 1; uintmax_t count = (uintmax_t) - 1;
uintmax_t intotal; uintmax_t inTotal = 0;
uintmax_t outTotal; uintmax_t outTotal = 0;
unsigned char *buf; uintmax_t totalSize;
uintmax_t readSize;
unsigned char buf[BUFSIZ];
argc--; argc--;
argv++; argv++;
@ -98,11 +100,6 @@ extern int dd_main(int argc, char **argv)
argv++; argv++;
} }
buf = xmalloc(blockSize);
intotal = 0;
outTotal = 0;
if (inFile == NULL) if (inFile == NULL)
inFd = fileno(stdin); inFd = fileno(stdin);
else else
@ -134,9 +131,13 @@ extern int dd_main(int argc, char **argv)
lseek(inFd, skipBlocks * blockSize, SEEK_SET); lseek(inFd, skipBlocks * blockSize, SEEK_SET);
lseek(outFd, seekBlocks * blockSize, SEEK_SET); lseek(outFd, seekBlocks * blockSize, SEEK_SET);
totalSize=count*blockSize;
while ((inCc = read(inFd, buf, sizeof(buf))) > 0) { printf("totalsize is %d\n",(int) totalSize);
intotal +=inCc; while ((readSize = totalSize - inTotal) > 0) {
if (readSize > BUFSIZ)
readSize=BUFSIZ;
inCc = read(inFd, buf, readSize);
inTotal += inCc;
if ((outCc = fullWrite(outFd, buf, inCc)) < 0) if ((outCc = fullWrite(outFd, buf, inCc)) < 0)
break; break;
outTotal += outCc; outTotal += outCc;
@ -150,8 +151,8 @@ extern int dd_main(int argc, char **argv)
free(buf); free(buf);
#endif #endif
printf("%ld+%d records in\n", (long) (intotal / blockSize), printf("%ld+%d records in\n", (long) (inTotal / blockSize),
(intotal % blockSize) != 0); (inTotal % blockSize) != 0);
printf("%ld+%d records out\n", (long) (outTotal / blockSize), printf("%ld+%d records out\n", (long) (outTotal / blockSize),
(outTotal % blockSize) != 0); (outTotal % blockSize) != 0);
exit(TRUE); exit(TRUE);

27
dd.c
View File

@ -52,9 +52,11 @@ extern int dd_main(int argc, char **argv)
uintmax_t skipBlocks = 0; uintmax_t skipBlocks = 0;
uintmax_t seekBlocks = 0; uintmax_t seekBlocks = 0;
uintmax_t count = (uintmax_t) - 1; uintmax_t count = (uintmax_t) - 1;
uintmax_t intotal; uintmax_t inTotal = 0;
uintmax_t outTotal; uintmax_t outTotal = 0;
unsigned char *buf; uintmax_t totalSize;
uintmax_t readSize;
unsigned char buf[BUFSIZ];
argc--; argc--;
argv++; argv++;
@ -98,11 +100,6 @@ extern int dd_main(int argc, char **argv)
argv++; argv++;
} }
buf = xmalloc(blockSize);
intotal = 0;
outTotal = 0;
if (inFile == NULL) if (inFile == NULL)
inFd = fileno(stdin); inFd = fileno(stdin);
else else
@ -134,9 +131,13 @@ extern int dd_main(int argc, char **argv)
lseek(inFd, skipBlocks * blockSize, SEEK_SET); lseek(inFd, skipBlocks * blockSize, SEEK_SET);
lseek(outFd, seekBlocks * blockSize, SEEK_SET); lseek(outFd, seekBlocks * blockSize, SEEK_SET);
totalSize=count*blockSize;
while ((inCc = read(inFd, buf, sizeof(buf))) > 0) { printf("totalsize is %d\n",(int) totalSize);
intotal +=inCc; while ((readSize = totalSize - inTotal) > 0) {
if (readSize > BUFSIZ)
readSize=BUFSIZ;
inCc = read(inFd, buf, readSize);
inTotal += inCc;
if ((outCc = fullWrite(outFd, buf, inCc)) < 0) if ((outCc = fullWrite(outFd, buf, inCc)) < 0)
break; break;
outTotal += outCc; outTotal += outCc;
@ -150,8 +151,8 @@ extern int dd_main(int argc, char **argv)
free(buf); free(buf);
#endif #endif
printf("%ld+%d records in\n", (long) (intotal / blockSize), printf("%ld+%d records in\n", (long) (inTotal / blockSize),
(intotal % blockSize) != 0); (inTotal % blockSize) != 0);
printf("%ld+%d records out\n", (long) (outTotal / blockSize), printf("%ld+%d records out\n", (long) (outTotal / blockSize),
(outTotal % blockSize) != 0); (outTotal % blockSize) != 0);
exit(TRUE); exit(TRUE);