Compare commits

..

No commits in common. "2b593559afc64ad2a7f80bed0511e19250ade41d" and "3e1735f77887ef26c194acc25d0fcc2c5e219a4f" have entirely different histories.

View File

@ -62,6 +62,34 @@ 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
@ -281,12 +309,8 @@ int main(int argc, char *argv[]){
printio(fmt, io);
if(!noerror)
count = 1;
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;
}
if(align >= 0)
Io_bufrpad(&io[0], align);
}else
++io[0].rec;
@ -294,18 +318,10 @@ int main(int argc, char *argv[]){
do{
int t;
if(io[0].bs <= io[1].bs){
if(io[1].bs > io[0].bs){
int n;
/* 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 */
/* copy from ibuf as much as possible to the obuf */
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;
@ -314,8 +330,9 @@ int main(int argc, char *argv[]){
io[0].bufuse -= n;
if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1)
continue; /* obuf not saturated - we could write more */
}
continue; /* we could write more */
}else
Io_bufxfer(&io[1], &io[0], MIN(io[0].bufuse, io[1].bs));
t = io[1].bufuse;
Io_write(&io[1]);