Compare commits
5 Commits
cc64561388
...
8c33f0116c
Author | SHA1 | Date | |
---|---|---|---|
8c33f0116c | |||
f8c0e0570c | |||
fc0d9e374b | |||
f49a2d2eb8 | |||
4004a4a006 |
62
src/dj.c
62
src/dj.c
@ -56,6 +56,9 @@ static char *fmt_human = "%d+%d > %d+%d; %d > %d\n";
|
|||||||
static char *stdin_name = "<stdin>";
|
static char *stdin_name = "<stdin>";
|
||||||
static char *stdout_name = "<stdout>";
|
static char *stdout_name = "<stdout>";
|
||||||
|
|
||||||
|
|
||||||
|
static int creat_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
|
||||||
|
| S_IWOTH; /* Consistent with touch(1p). */
|
||||||
static int read_flags = O_RDONLY; /* Consistent with Busybox dd(1). */
|
static int read_flags = O_RDONLY; /* Consistent with Busybox dd(1). */
|
||||||
static int write_flags = O_WRONLY | O_CREAT;
|
static int write_flags = O_WRONLY | O_CREAT;
|
||||||
|
|
||||||
@ -64,27 +67,34 @@ static int write_flags = O_WRONLY | O_CREAT;
|
|||||||
/* Macro to check if fd is stdin or stdout */
|
/* Macro to check if fd is stdin or stdout */
|
||||||
#define fdisstd(fd) ((fd) == STDIN_FILENO || (fd) == STDOUT_FILENO)
|
#define fdisstd(fd) ((fd) == STDIN_FILENO || (fd) == STDOUT_FILENO)
|
||||||
|
|
||||||
/* Reads io->bs bytes from *io's file descriptor into io->buf, storing the
|
|
||||||
* number of read bytes in io->bufuse and updating io->bytes. If io->bufuse is
|
|
||||||
* 0, errno will probably be set. Returns io. */
|
|
||||||
static struct Io *
|
static struct Io *
|
||||||
Io_read(struct Io *io){
|
Io_read(struct Io *io){
|
||||||
|
|
||||||
|
assert(io->bs > 0);
|
||||||
|
|
||||||
io->bytes += (io->bufuse = read(io->fd, io->buf, io->bs));
|
io->bytes += (io->bufuse = read(io->fd, io->buf, io->bs));
|
||||||
|
|
||||||
|
assert(io->bufuse <= io->bs);
|
||||||
|
|
||||||
|
io->prec += (0 < io->bufuse && io->bufuse < io->bs);
|
||||||
|
io->rec += (io->bufuse == io->bs);
|
||||||
|
|
||||||
return io;
|
return io;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Writes io->bufuse 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 *
|
static struct Io *
|
||||||
Io_write(struct Io *io){
|
Io_write(struct Io *io){
|
||||||
int t;
|
int t;
|
||||||
|
|
||||||
|
assert(io->bufuse > 0);
|
||||||
|
assert(io->bufuse <= io->bs);
|
||||||
|
|
||||||
if((t = write(io->fd, io->buf, io->bufuse)) > 0)
|
if((t = write(io->fd, io->buf, io->bufuse)) > 0)
|
||||||
memmove(io->buf, io->buf + t, (io->bufuse -= t));
|
memmove(io->buf, io->buf + t, (io->bufuse -= t));
|
||||||
|
|
||||||
io->bytes += t;
|
io->bytes += t;
|
||||||
|
io->prec += (t > 0 && io->bufuse > 0);
|
||||||
|
io->rec += (t > 0 && io->bufuse == 0);
|
||||||
|
|
||||||
return io;
|
return io;
|
||||||
}
|
}
|
||||||
@ -102,9 +112,9 @@ oserr(char *s){
|
|||||||
/* Prints statistics regarding the use of dj, particularly partially and
|
/* Prints statistics regarding the use of dj, particularly partially and
|
||||||
* completely read and written records. */
|
* completely read and written records. */
|
||||||
static void
|
static void
|
||||||
printio(char *fmt, struct Io io[2]){
|
fprintio(FILE *stream, char *fmt, struct Io io[2]){
|
||||||
|
|
||||||
fprintf(stderr, fmt,
|
fprintf(stream, fmt,
|
||||||
io[0].rec, io[0].prec, io[1].rec, io[1].prec,
|
io[0].rec, io[0].prec, io[1].rec, io[1].prec,
|
||||||
io[0].bytes, io[1].bytes);
|
io[0].bytes, io[1].bytes);
|
||||||
|
|
||||||
@ -175,10 +185,7 @@ int main(int argc, char *argv[]){
|
|||||||
}else{
|
}else{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if((fd = open(optarg, io[i].fl, /* touch(1p) flags */
|
if((fd = open(optarg, io[i].fl, creat_mode)) != -1
|
||||||
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
|
|
||||||
| S_IROTH | S_IWOTH))
|
|
||||||
!= -1
|
|
||||||
&& (fdisstd(io[i].fd) || close(io[i].fd) == 0)){
|
&& (fdisstd(io[i].fd) || close(io[i].fd) == 0)){
|
||||||
io[i].fd = fd;
|
io[i].fd = fd;
|
||||||
io[i].fn = optarg;
|
io[i].fn = optarg;
|
||||||
@ -263,15 +270,14 @@ int main(int argc, char *argv[]){
|
|||||||
return oserr(io[i].fn);
|
return oserr(io[i].fn);
|
||||||
|
|
||||||
do{ /* read */
|
do{ /* read */
|
||||||
Io_read(&io[0]);
|
if(Io_read(&io[0])->bufuse == 0 && !noerror)
|
||||||
if(!noerror && io[0].bufuse == 0)
|
|
||||||
Io_read(&io[0]); /* second chance */
|
Io_read(&io[0]); /* second chance */
|
||||||
if(io[0].bufuse == 0) /* that's all she wrote */
|
if(io[0].bufuse == 0) /* that's all she wrote */
|
||||||
break;
|
break;
|
||||||
else if(io[0].bufuse < io[0].bs){
|
|
||||||
++io[0].prec;
|
if(io[0].bufuse < io[0].bs){
|
||||||
fprintf(stderr, "%s: Partial read:\n\t", program_name);
|
fprintf(stderr, "%s: Partial read:\n\t", program_name);
|
||||||
printio(fmt, io);
|
fprintio(stderr, fmt, io);
|
||||||
if(!noerror)
|
if(!noerror)
|
||||||
count = 1;
|
count = 1;
|
||||||
if(align >= 0){
|
if(align >= 0){
|
||||||
@ -280,8 +286,7 @@ int main(int argc, char *argv[]){
|
|||||||
io[0].bs - io[0].bufuse);
|
io[0].bs - io[0].bufuse);
|
||||||
io->bufuse = io->bs;
|
io->bufuse = io->bs;
|
||||||
}
|
}
|
||||||
}else
|
}
|
||||||
++io[0].rec;
|
|
||||||
|
|
||||||
/* write */
|
/* write */
|
||||||
do{
|
do{
|
||||||
@ -311,26 +316,23 @@ int main(int argc, char *argv[]){
|
|||||||
}
|
}
|
||||||
|
|
||||||
t = io[1].bufuse;
|
t = io[1].bufuse;
|
||||||
Io_write(&io[1]);
|
if(Io_write(&io[1])->bufuse == t && !noerror)
|
||||||
if(!noerror && io[1].bufuse == t)
|
|
||||||
Io_write(&io[1]); /* second chance */
|
Io_write(&io[1]); /* second chance */
|
||||||
if(t == io[1].bufuse){ /* no more love */
|
if(io[1].bufuse == t){ /* no more love */
|
||||||
count = 1;
|
count = 1;
|
||||||
break;
|
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);
|
fprintf(stderr, "%s: Partial write:\n\t", program_name);
|
||||||
printio(fmt, io);
|
fprintio(stderr, fmt, io);
|
||||||
if(!noerror)
|
if(!noerror)
|
||||||
count = 1;
|
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(io[0].bufuse > 0);
|
||||||
}while(count == 0 || --count > 0);
|
}while(count == 0 || --count > 0);
|
||||||
|
|
||||||
printio(fmt, io);
|
fprintio(stderr, fmt, io);
|
||||||
|
|
||||||
return EX_OK;
|
return EX_OK;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user