Compare commits

...

2 Commits

Author SHA1 Message Date
DTB
2b593559af
dj(1): remove Io_bufrpad 2024-07-03 19:06:59 -06:00
DTB
b74160fa4e
dj(1): remove Io_bufxfer 2024-07-03 19:04:01 -06:00

View File

@ -62,34 +62,6 @@ static int write_flags = O_WRONLY | O_CREAT;
/* Macro to check if fd is stdin or stdout */
#define fdisstd(fd) ((fd) == STDIN_FILENO || (fd) == STDOUT_FILENO)
/* Fills the unused portion of io's buffer with padding, updating io->bufuse.
* Returns io. */
static struct Io *
Io_bufrpad(struct Io *io, int padding){
assert(io != NULL);
memset(io->buf + io->bufuse, padding, io->bs - io->bufuse);
io->bufuse = io->bs;
return io;
}
/* Copies from the buffer in src to the buffer in dest no more than n units,
* removing the copied units from src and permuting the remaining units in the
* src buffer to the start of the buffer, modifying both the src and dest
* bufuse and returning dest. */
static struct Io*
Io_bufxfer(struct Io *dest, struct Io *src, int n){
assert(dest != NULL && src != NULL);
memcpy(dest->buf, src->buf, (dest->bufuse = n));
memmove(src->buf, src->buf + n, (src->bufuse -= n));
return dest;
}
/* Opens io->fn and saves the file descriptor into io->fd. Returns io->fd,
* which will be -1 if an error occured. */
static int
@ -309,8 +281,12 @@ int main(int argc, char *argv[]){
printio(fmt, io);
if(!noerror)
count = 1;
if(align >= 0)
Io_bufrpad(&io[0], align);
if(align >= 0){
/* fill the rest of the ibuf with padding */
memset(io[0].buf + io[0].bufuse, align,
io[0].bs - io[0].bufuse);
io->bufuse = io->bs;
}
}else
++io[0].rec;
@ -318,10 +294,18 @@ int main(int argc, char *argv[]){
do{
int t;
if(io[1].bs > io[0].bs){
if(io[0].bs <= io[1].bs){
int n;
/* copy from ibuf as much as possible to the obuf */
/* saturate obuf */
memcpy(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) */ {
int n;
/* drain what we can from ibuf */
memcpy(io[1].buf + io[1].bufuse, io[0].buf,
(n = MIN(io[0].bufuse, io[1].bs - io[1].bufuse)));
io[1].bufuse += n;
@ -330,9 +314,8 @@ int main(int argc, char *argv[]){
io[0].bufuse -= n;
if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1)
continue; /* we could write more */
}else
Io_bufxfer(&io[1], &io[0], MIN(io[0].bufuse, io[1].bs));
continue; /* obuf not saturated - we could write more */
}
t = io[1].bufuse;
Io_write(&io[1]);