Compare commits
2 Commits
3e1735f778
...
2b593559af
Author | SHA1 | Date | |
---|---|---|---|
2b593559af | |||
b74160fa4e |
53
src/dj.c
53
src/dj.c
@ -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]);
|
||||
|
Loading…
Reference in New Issue
Block a user