dj(1): error reporting more of the time
This commit is contained in:
parent
bab3cdd90e
commit
691e94c0c1
47
src/dj.c
47
src/dj.c
@ -37,16 +37,17 @@ char *program_name = "dj";
|
||||
* writing ends of its jockeyed "pipe". User-configurable members are noted
|
||||
* with their relevant options. */
|
||||
struct Io{
|
||||
int bs; /* buffer size (-bB) */
|
||||
char *buf; /* buffer */
|
||||
char *fn; /* file name (-io) */
|
||||
size_t bs; /* buffer size (-bB) */
|
||||
size_t bufuse; /* buffer usage */
|
||||
char *buf; /* buffer */
|
||||
size_t bytes; /* bytes processed */
|
||||
int fd; /* file descriptor */
|
||||
int fl; /* file opening flags */
|
||||
char *fn; /* file name (may be stdin_name or stdout_name) (-io) */
|
||||
size_t prec; /* partial records processed */
|
||||
size_t rec; /* records processed */
|
||||
long seek; /* bytes to seek/skip (will be 0 after skippage) (-sS) */
|
||||
size_t bytes; /* bytes processed */
|
||||
size_t prec; /* partial records processed */
|
||||
size_t rec; /* records processed */
|
||||
long seek; /* remaining bytes to seek/skip (-sS) */
|
||||
int fd; /* file descriptor */
|
||||
int fl; /* file opening flags */
|
||||
char error: 1; /* (bool) error status */
|
||||
};
|
||||
|
||||
/* To be assigned to main:fmt and used with printio(). */
|
||||
@ -73,8 +74,10 @@ Io_read(struct Io *io){
|
||||
assert(io->bs > 0);
|
||||
assert(io->bufuse < io->bs);
|
||||
|
||||
if((t = read(io->fd, &(io->buf)[io->bufuse], io->bs - io->bufuse)) < 0)
|
||||
if((t = read(io->fd, &(io->buf)[io->bufuse], io->bs - io->bufuse)) < 0){
|
||||
io->error = 1;
|
||||
t = 0;
|
||||
}
|
||||
|
||||
io->bufuse += t;
|
||||
io->bytes += t;
|
||||
@ -93,9 +96,10 @@ Io_write(struct Io *io){
|
||||
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){
|
||||
io->error = 1;
|
||||
t = 0;
|
||||
else if(t > 0)
|
||||
}else if(t > 0)
|
||||
memmove(io->buf, &(io->buf)[t], (io->bufuse -= t));
|
||||
|
||||
io->bytes += t;
|
||||
@ -158,20 +162,21 @@ int main(int argc, char *argv[]){
|
||||
char *fmt; /* == fmt_asv (default) or fmt_human (-H) */
|
||||
size_t i; /* side of io being modified */
|
||||
char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
|
||||
struct Io io[2];
|
||||
struct Io io[2 /* { in, out } */];
|
||||
|
||||
/* Set defaults. */
|
||||
align = -1;
|
||||
count = 0;
|
||||
fmt = fmt_asv;
|
||||
noerror = 0;
|
||||
for(i = 0; i < 2; ++i){
|
||||
for(i = 0; i < (sizeof io) / (sizeof *io); ++i){
|
||||
io[i].bs = 1024 /* 1 KiB */; /* GNU dd(1) default; POSIX says 512B */
|
||||
io[i].bufuse = 0;
|
||||
io[i].bytes = 0;
|
||||
io[i].fd = i == 0 ? STDIN_FILENO : STDOUT_FILENO;
|
||||
io[i].fn = i == 0 ? stdin_name : stdout_name;
|
||||
io[i].fl = i == 0 ? read_flags : write_flags;
|
||||
io[i].error = 0;
|
||||
io[i].prec = 0;
|
||||
io[i].rec = 0;
|
||||
io[i].seek = 0;
|
||||
@ -227,10 +232,10 @@ int main(int argc, char *argv[]){
|
||||
if(argc > optind)
|
||||
return usage(program_name);
|
||||
|
||||
for(i = 0; i < 2; ++i){
|
||||
for(i = 0; i < (sizeof io) / (sizeof *io); ++i){
|
||||
/* buffer allocation */
|
||||
if((io[i].buf = malloc(io[i].bs * (sizeof *(io[i].buf)))) == NULL){
|
||||
fprintf(stderr, "%s: Failed to allocate %d bytes\n",
|
||||
fprintf(stderr, "%s: Failed to allocate %zd bytes\n",
|
||||
program_name, io[i].bs);
|
||||
return EX_OSERR;
|
||||
}
|
||||
@ -245,7 +250,7 @@ int main(int argc, char *argv[]){
|
||||
do{
|
||||
memset(io[1].buf, '\0',
|
||||
(t = io[1].bufuse = MIN(io[1].bs, io[1].seek)));
|
||||
if(Io_write(&io[1])->bufuse == t && !noerror)
|
||||
if(Io_write(&io[1])->bufuse == t && !noerror && !io[1].error)
|
||||
Io_write(&io[1]); /* second chance */
|
||||
}while((io[1].seek -= (t - io[1].bufuse)) > 0 && io[1].bufuse != t);
|
||||
io[1].bufuse = 0;
|
||||
@ -268,7 +273,7 @@ int main(int argc, char *argv[]){
|
||||
io[0].bufuse = io[0].bs - io[0].seek;
|
||||
|
||||
t = io[0].bufuse;
|
||||
if(Io_read(&io[0])->bufuse == t && !noerror)
|
||||
if(Io_read(&io[0])->bufuse == t && !noerror && io[0].error)
|
||||
Io_read(&io[0]); /* second chance */
|
||||
assert(io[0].bufuse >= t);
|
||||
if(io[0].bufuse == t) /* that's all she wrote */
|
||||
@ -322,7 +327,7 @@ int main(int argc, char *argv[]){
|
||||
}
|
||||
|
||||
t = io[1].bufuse;
|
||||
if(Io_write(&io[1])->bufuse == t && !noerror)
|
||||
if(Io_write(&io[1])->bufuse == t && !noerror && !io[1].error)
|
||||
Io_write(&io[1]); /* second chance */
|
||||
assert(io[1].bufuse <= t);
|
||||
if(io[1].bufuse == t){ /* no more love */
|
||||
@ -341,5 +346,9 @@ int main(int argc, char *argv[]){
|
||||
|
||||
fprintio(stderr, fmt, io);
|
||||
|
||||
for(i = 0; i < (sizeof io) / (sizeof *io); ++i)
|
||||
if(io[i].error)
|
||||
return oserr(io[i].fn);
|
||||
|
||||
return EX_OK;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user