Complicate truncate behavior.
This commit is contained in:
parent
fd4c58d7c5
commit
c9acf8c766
@ -41,7 +41,7 @@ static struct suffix_mult dd_suffixes[] = {
|
|||||||
|
|
||||||
int dd_main(int argc, char **argv)
|
int dd_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int i, ifd, ofd, sync = FALSE, trunc = TRUE;
|
int i, ifd, ofd, oflag, sync = FALSE, trunc = TRUE;
|
||||||
size_t in_full = 0, in_part = 0, out_full = 0, out_part = 0;
|
size_t in_full = 0, in_part = 0, out_full = 0, out_part = 0;
|
||||||
size_t bs = 512, count = -1;
|
size_t bs = 512, count = -1;
|
||||||
ssize_t n;
|
ssize_t n;
|
||||||
@ -94,8 +94,19 @@ int dd_main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (outfile != NULL) {
|
if (outfile != NULL) {
|
||||||
if ((ofd = open(outfile, O_WRONLY | O_CREAT, 0666)) < 0)
|
oflag = O_WRONLY | O_CREAT;
|
||||||
|
|
||||||
|
if (!seek && trunc)
|
||||||
|
oflag |= O_TRUNC;
|
||||||
|
|
||||||
|
if ((ofd = open(outfile, oflag, 0666)) < 0)
|
||||||
perror_msg_and_die("%s", outfile);
|
perror_msg_and_die("%s", outfile);
|
||||||
|
|
||||||
|
if (seek && trunc) {
|
||||||
|
if (ftruncate(ofd, seek * bs) < 0)
|
||||||
|
perror_msg_and_die("%s", outfile);
|
||||||
|
}
|
||||||
|
|
||||||
statusfp = stdout;
|
statusfp = stdout;
|
||||||
} else {
|
} else {
|
||||||
ofd = STDOUT_FILENO;
|
ofd = STDOUT_FILENO;
|
||||||
@ -113,11 +124,6 @@ int dd_main(int argc, char **argv)
|
|||||||
perror_msg_and_die("%s", outfile);
|
perror_msg_and_die("%s", outfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trunc) {
|
|
||||||
if (ftruncate(ofd, seek * bs) < 0)
|
|
||||||
perror_msg_and_die("%s", outfile);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (in_full + in_part != count) {
|
while (in_full + in_part != count) {
|
||||||
n = safe_read(ifd, buf, bs);
|
n = safe_read(ifd, buf, bs);
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
|
20
dd.c
20
dd.c
@ -41,7 +41,7 @@ static struct suffix_mult dd_suffixes[] = {
|
|||||||
|
|
||||||
int dd_main(int argc, char **argv)
|
int dd_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int i, ifd, ofd, sync = FALSE, trunc = TRUE;
|
int i, ifd, ofd, oflag, sync = FALSE, trunc = TRUE;
|
||||||
size_t in_full = 0, in_part = 0, out_full = 0, out_part = 0;
|
size_t in_full = 0, in_part = 0, out_full = 0, out_part = 0;
|
||||||
size_t bs = 512, count = -1;
|
size_t bs = 512, count = -1;
|
||||||
ssize_t n;
|
ssize_t n;
|
||||||
@ -94,8 +94,19 @@ int dd_main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (outfile != NULL) {
|
if (outfile != NULL) {
|
||||||
if ((ofd = open(outfile, O_WRONLY | O_CREAT, 0666)) < 0)
|
oflag = O_WRONLY | O_CREAT;
|
||||||
|
|
||||||
|
if (!seek && trunc)
|
||||||
|
oflag |= O_TRUNC;
|
||||||
|
|
||||||
|
if ((ofd = open(outfile, oflag, 0666)) < 0)
|
||||||
perror_msg_and_die("%s", outfile);
|
perror_msg_and_die("%s", outfile);
|
||||||
|
|
||||||
|
if (seek && trunc) {
|
||||||
|
if (ftruncate(ofd, seek * bs) < 0)
|
||||||
|
perror_msg_and_die("%s", outfile);
|
||||||
|
}
|
||||||
|
|
||||||
statusfp = stdout;
|
statusfp = stdout;
|
||||||
} else {
|
} else {
|
||||||
ofd = STDOUT_FILENO;
|
ofd = STDOUT_FILENO;
|
||||||
@ -113,11 +124,6 @@ int dd_main(int argc, char **argv)
|
|||||||
perror_msg_and_die("%s", outfile);
|
perror_msg_and_die("%s", outfile);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trunc) {
|
|
||||||
if (ftruncate(ofd, seek * bs) < 0)
|
|
||||||
perror_msg_and_die("%s", outfile);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (in_full + in_part != count) {
|
while (in_full + in_part != count) {
|
||||||
n = safe_read(ifd, buf, bs);
|
n = safe_read(ifd, buf, bs);
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user