dj(1): replace do/while in write loop

This commit is contained in:
dtb 2024-07-19 18:40:24 -06:00
parent a01cea572d
commit 71f4a411b6
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -157,7 +157,7 @@ usage(char *s) {
}
int main(int argc, char *argv[]) {
int align; /* low 8b used, negative if no alignment is being done */
int align; /* low 8b used, negative if no alignment is being done */
int count; /* 0 if dj(1) runs until no more reads are possible */
char *fmt; /* == fmt_asv (default) or fmt_human (-H) */
size_t i; /* side of io being modified */
@ -331,21 +331,20 @@ int main(int argc, char *argv[]) {
}
}
/* write */
do { /* while(io[0].bufuse > 0); */
int t;
assert(io[0].bufuse > 0);
while (io[0].bufuse > 0) { /* write */
if (io[0].bs <= io[1].bs) {
int n;
/* saturate obuf */
memcpy(
memcpy( /* saturate obuf */
io[1].buf, io[0].buf,
(io[1].bufuse = (n = MIN(io[0].bufuse, io[1].bs)))
);
/* permute the copied units out of ibuf */
memmove(io[0].buf, &(io[0].buf)[n], (io[0].bufuse -= n));
} else /* if(io[0].bs < io[1].bs) */ {
} else /* if(io[0].bs > io[1].bs) */ {
int n;
/* drain what we can from ibuf */
@ -353,12 +352,10 @@ int main(int argc, char *argv[]) {
&(io[1].buf)[io[1].bufuse], io[0].buf,
(n = MIN(io[0].bufuse, io[1].bs - io[1].bufuse))
);
io[1].bufuse += n;
/* permute out the copied units */
memmove(io[0].buf, &(io[0].buf)[n], io[0].bs - n);
io[0].bufuse -= n;
if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1) {
@ -366,16 +363,27 @@ int main(int argc, char *argv[]) {
}
}
t = io[1].bufuse;
if (Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0) {
Io_write(&io[1]); /* second chance */
}
{ /* writes actually happen, or die */
size_t t;
assert(io[1].bufuse <= t);
t = io[1].bufuse;
if (Io_write(&io[1])->bufuse == t
&& !noerror
&& io[1].error == 0) {
Io_write(&io[1]); /* second chance */
}
if (io[1].bufuse == t) { /* no more love */
count = 1;
break;
assert(io[1].error == 0 || io[1].bufuse == t);
/* if the Io_writes errored, bufuse wouldn't have changed, and
* the error will be reported at the end of the read/write
* loop */
assert(io[1].bufuse <= t);
if (io[1].bufuse == t) { /* no more love */
count = 1;
break;
}
}
if (0 < io[1].bufuse /* && io[1].bufuse < t */) {
@ -384,7 +392,7 @@ int main(int argc, char *argv[]) {
if(!noerror) { count = 1; }
}
} while(io[0].bufuse > 0);
}
} while(count == 0 || --count > 0);
fprintio(stderr, fmt, io);