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 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 */ int count; /* 0 if dj(1) runs until no more reads are possible */
char *fmt; /* == fmt_asv (default) or fmt_human (-H) */ char *fmt; /* == fmt_asv (default) or fmt_human (-H) */
size_t i; /* side of io being modified */ size_t i; /* side of io being modified */
@ -331,21 +331,20 @@ int main(int argc, char *argv[]) {
} }
} }
/* write */ assert(io[0].bufuse > 0);
do { /* while(io[0].bufuse > 0); */
int t;
while (io[0].bufuse > 0) { /* write */
if (io[0].bs <= io[1].bs) { if (io[0].bs <= io[1].bs) {
int n; int n;
/* saturate obuf */ memcpy( /* saturate obuf */
memcpy(
io[1].buf, io[0].buf, io[1].buf, io[0].buf,
(io[1].bufuse = (n = MIN(io[0].bufuse, io[1].bs))) (io[1].bufuse = (n = MIN(io[0].bufuse, io[1].bs)))
); );
/* permute the copied units out of ibuf */ /* permute the copied units out of ibuf */
memmove(io[0].buf, &(io[0].buf)[n], (io[0].bufuse -= n)); 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; int n;
/* drain what we can from ibuf */ /* 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, &(io[1].buf)[io[1].bufuse], io[0].buf,
(n = MIN(io[0].bufuse, io[1].bs - io[1].bufuse)) (n = MIN(io[0].bufuse, io[1].bs - io[1].bufuse))
); );
io[1].bufuse += n; io[1].bufuse += n;
/* permute out the copied units */ /* permute out the copied units */
memmove(io[0].buf, &(io[0].buf)[n], io[0].bs - n); memmove(io[0].buf, &(io[0].buf)[n], io[0].bs - n);
io[0].bufuse -= n; io[0].bufuse -= n;
if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1) { 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; { /* writes actually happen, or die */
if (Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0) { size_t t;
Io_write(&io[1]); /* second chance */
}
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 */ assert(io[1].error == 0 || io[1].bufuse == t);
count = 1; /* if the Io_writes errored, bufuse wouldn't have changed, and
break; * 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 */) { if (0 < io[1].bufuse /* && io[1].bufuse < t */) {
@ -384,7 +392,7 @@ int main(int argc, char *argv[]) {
if(!noerror) { count = 1; } if(!noerror) { count = 1; }
} }
} while(io[0].bufuse > 0); }
} while(count == 0 || --count > 0); } while(count == 0 || --count > 0);
fprintio(stderr, fmt, io); fprintio(stderr, fmt, io);