dj(1): make Io_write handle prec and rec

This commit is contained in:
dtb 2024-07-04 19:36:32 -06:00
parent fc0d9e374b
commit f8c0e0570c
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -72,30 +72,34 @@ static int write_flags = O_WRONLY | O_CREAT;
static struct Io *
Io_read(struct Io *io){
assert(io->bs > 0);
io->bytes += (io->bufuse = read(io->fd, io->buf, io->bs));
assert(io->bufuse <= io->bs);
if(io->bufuse != 0){
if(io->bufuse < io->bs)
++io->prec;
else /* if(io->bufuse == io->bs) */
++io->rec;
}
io->prec += (0 < io->bufuse && io->bufuse < io->bs);
io->rec += (io->bufuse == io->bs);
return io;
}
/* Writes io->bufuse units from io->buf to io->fd, permuting any unwritten
/* Writes io->bufuse (>0) units from io->buf to io->fd, permuting any unwritten
* bytes to the start of io->buf and updating io->bufuse. If io->bufuse doesn't
* change, errno will probably be set. Returns io. */
static struct Io *
Io_write(struct Io *io){
int t;
assert(io->bufuse > 0);
assert(io->bufuse <= io->bs);
if((t = write(io->fd, io->buf, io->bufuse)) > 0)
memmove(io->buf, io->buf + t, (io->bufuse -= t));
io->bytes += t;
io->prec += (t > 0 && io->bufuse > 0);
io->rec += (t > 0 && io->bufuse == 0);
return io;
}
@ -322,19 +326,17 @@ int main(int argc, char *argv[]){
t = io[1].bufuse;
if(Io_write(&io[1])->bufuse == t && !noerror)
Io_write(&io[1]); /* second chance */
if(t == io[1].bufuse){ /* no more love */
if(io[1].bufuse == t){ /* no more love */
count = 1;
break;
}else if(t > io[1].bufuse && io[1].bufuse > 0){
io[1].prec += 1;
}
if(0 < io[1].bufuse && io[1].bufuse < t){
fprintf(stderr, "%s: Partial write:\n\t", program_name);
fprintio(stderr, fmt, io);
if(!noerror)
count = 1;
}else if(io[1].bufuse == 0 && t < io[1].bs)
++io[1].prec;
else
++io[1].rec;
}
}while(io[0].bufuse > 0);
}while(count == 0 || --count > 0);