Using seek=? used to cause part of the original file to be zero'ed, i

think this was caused by opening the file in truncate mode.
This commit is contained in:
Glenn L McGrath 2000-09-10 04:39:37 +00:00
parent 0ae8e5a645
commit 18310f1ce3
2 changed files with 8 additions and 10 deletions

View File

@ -57,6 +57,7 @@ extern int dd_main(int argc, char **argv)
uintmax_t totalSize; uintmax_t totalSize;
uintmax_t readSize; uintmax_t readSize;
unsigned char buf[BUFSIZ]; unsigned char buf[BUFSIZ];
off_t jumped;
argc--; argc--;
argv++; argv++;
@ -117,7 +118,7 @@ extern int dd_main(int argc, char **argv)
if (outFile == NULL) if (outFile == NULL)
outFd = fileno(stdout); outFd = fileno(stdout);
else else
outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666); outFd = open(outFile, O_WRONLY | O_CREAT, 0666);
if (outFd < 0) { if (outFd < 0) {
/* Note that we are not freeing buf or closing /* Note that we are not freeing buf or closing
@ -129,10 +130,9 @@ extern int dd_main(int argc, char **argv)
fatalError( outFile); fatalError( outFile);
} }
lseek(inFd, skipBlocks * blockSize, SEEK_SET); lseek(inFd, (off_t) (skipBlocks * blockSize), SEEK_SET);
lseek(outFd, seekBlocks * blockSize, SEEK_SET); jumped = lseek(outFd, (off_t) (seekBlocks * blockSize), SEEK_SET);
totalSize=count*blockSize; totalSize=count*blockSize;
printf("totalsize is %d\n",(int) totalSize);
while ((readSize = totalSize - inTotal) > 0) { while ((readSize = totalSize - inTotal) > 0) {
if (readSize > BUFSIZ) if (readSize > BUFSIZ)
readSize=BUFSIZ; readSize=BUFSIZ;
@ -148,7 +148,6 @@ extern int dd_main(int argc, char **argv)
#ifdef BB_FEATURE_CLEAN_UP #ifdef BB_FEATURE_CLEAN_UP
close(inFd); close(inFd);
close(outFd); close(outFd);
free(buf);
#endif #endif
printf("%ld+%d records in\n", (long) (inTotal / blockSize), printf("%ld+%d records in\n", (long) (inTotal / blockSize),

9
dd.c
View File

@ -57,6 +57,7 @@ extern int dd_main(int argc, char **argv)
uintmax_t totalSize; uintmax_t totalSize;
uintmax_t readSize; uintmax_t readSize;
unsigned char buf[BUFSIZ]; unsigned char buf[BUFSIZ];
off_t jumped;
argc--; argc--;
argv++; argv++;
@ -117,7 +118,7 @@ extern int dd_main(int argc, char **argv)
if (outFile == NULL) if (outFile == NULL)
outFd = fileno(stdout); outFd = fileno(stdout);
else else
outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666); outFd = open(outFile, O_WRONLY | O_CREAT, 0666);
if (outFd < 0) { if (outFd < 0) {
/* Note that we are not freeing buf or closing /* Note that we are not freeing buf or closing
@ -129,10 +130,9 @@ extern int dd_main(int argc, char **argv)
fatalError( outFile); fatalError( outFile);
} }
lseek(inFd, skipBlocks * blockSize, SEEK_SET); lseek(inFd, (off_t) (skipBlocks * blockSize), SEEK_SET);
lseek(outFd, seekBlocks * blockSize, SEEK_SET); jumped = lseek(outFd, (off_t) (seekBlocks * blockSize), SEEK_SET);
totalSize=count*blockSize; totalSize=count*blockSize;
printf("totalsize is %d\n",(int) totalSize);
while ((readSize = totalSize - inTotal) > 0) { while ((readSize = totalSize - inTotal) > 0) {
if (readSize > BUFSIZ) if (readSize > BUFSIZ)
readSize=BUFSIZ; readSize=BUFSIZ;
@ -148,7 +148,6 @@ extern int dd_main(int argc, char **argv)
#ifdef BB_FEATURE_CLEAN_UP #ifdef BB_FEATURE_CLEAN_UP
close(inFd); close(inFd);
close(outFd); close(outFd);
free(buf);
#endif #endif
printf("%ld+%d records in\n", (long) (inTotal / blockSize), printf("%ld+%d records in\n", (long) (inTotal / blockSize),