dj(1): fix retvals

This commit is contained in:
dtb 2024-07-07 21:13:44 -06:00
parent 691e94c0c1
commit 5d5a6d2172
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -45,9 +45,9 @@ struct Io{
size_t prec; /* partial records processed */
size_t rec; /* records processed */
long seek; /* remaining bytes to seek/skip (-sS) */
int error; /* errno */
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(). */
@ -75,7 +75,7 @@ Io_read(struct Io *io){
assert(io->bufuse < io->bs);
if((t = read(io->fd, &(io->buf)[io->bufuse], io->bs - io->bufuse)) < 0){
io->error = 1;
io->error = errno;
t = 0;
}
@ -97,7 +97,7 @@ Io_write(struct Io *io){
assert(io->bufuse <= io->bs);
if((t = write(io->fd, io->buf, io->bufuse)) < 0){
io->error = 1;
io->error = errno;
t = 0;
}else if(t > 0)
memmove(io->buf, &(io->buf)[t], (io->bufuse -= t));
@ -109,13 +109,9 @@ Io_write(struct Io *io){
return io;
}
/* Prints an error message suitable for the event of an operating system error,
* with the error itself to be described in the string s. */
static int
oserr(char *s){
fprintf(stderr, "%s: %s: %s\n", program_name, s, strerror(errno));
oserr(char *e, int n){
fprintf(stderr, "%s: %s: %s\n", program_name, e, strerror(n));
return EX_OSERR;
}
@ -203,7 +199,7 @@ int main(int argc, char *argv[]){
break;
}
}
return oserr(optarg);
return oserr(optarg, errno);
case 'n': noerror = 1; break;
case 'H': fmt = fmt_human; break;
case 'a':
@ -250,15 +246,17 @@ 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 && !io[1].error)
if(Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0)
Io_write(&io[1]); /* second chance */
if(io[1].error != 0)
return oserr(io[1].fn, io[1].error);
}while((io[1].seek -= (t - io[1].bufuse)) > 0 && io[1].bufuse != t);
io[1].bufuse = 0;
}
if(io[1].seek > 0){
fprintio(stderr, fmt, io);
return oserr(io[1].fn);
return oserr(io[1].fn, errno);
}
do{
@ -273,7 +271,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 && io[0].error)
if(Io_read(&io[0])->bufuse == t && !noerror && io[0].error == 0)
Io_read(&io[0]); /* second chance */
assert(io[0].bufuse >= t);
if(io[0].bufuse == t) /* that's all she wrote */
@ -327,7 +325,7 @@ int main(int argc, char *argv[]){
}
t = io[1].bufuse;
if(Io_write(&io[1])->bufuse == t && !noerror && !io[1].error)
if(Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0)
Io_write(&io[1]); /* second chance */
assert(io[1].bufuse <= t);
if(io[1].bufuse == t){ /* no more love */
@ -348,7 +346,7 @@ int main(int argc, char *argv[]){
for(i = 0; i < (sizeof io) / (sizeof *io); ++i)
if(io[i].error)
return oserr(io[i].fn);
return oserr(io[i].fn, io[i].error);
return EX_OK;
}