From 66f54982320eb6ca2f47da26c8dac78aaf4aaecc Mon Sep 17 00:00:00 2001 From: DTB Date: Wed, 26 Jun 2024 12:40:36 -0600 Subject: [PATCH] dj(1): refactor Io_fdseek --- src/dj.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/dj.c b/src/dj.c index e41f350..7c0b7c3 100644 --- a/src/dj.c +++ b/src/dj.c @@ -156,18 +156,15 @@ Io_fdopen(struct Io *io, char *fn){ return fd; } -/* Seeks io->seek bytes through *io's file descriptor, (counter-intuitively) - * returning -1 if successful and a sysexits.h exit code if an unrecoverable - * error occurred. io->buf will be cleared of useful bytes and io->seek will - * be set to zero to indicate the seek occurred. */ -static int +/* Seeks io->seek bytes through *io's file descriptor, subtracting the number + * of sought bytes from io->seek. This procedure leaves garbage in io->buf. */ +static void Io_fdseek(struct Io *io){ - int (*op)(int, void *, size_t); - if(!fdisstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) != -1) - return -1; + if(io->seek != 0 + || (!fdisstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) != -1)) + return; - /* repeated code to get the condition out of the loop */ if(io->fl == write_flags){ memset(io->buf, '\0', io->bs); /* We're going to cheat and use bufuse as the retval for write(2), @@ -186,12 +183,11 @@ Io_fdseek(struct Io *io){ /* second chance */ io->bufuse = read(io->fd, io->buf, MIN(io->bs, io->seek)); }while((io->seek -= io->bufuse) > 0 && io->bufuse != 0); - }else - return EX_SOFTWARE; + } io->bufuse = 0; - return -1; + return; } /* Reads io->bs bytes from *io's file descriptor into io->buf, storing the @@ -352,11 +348,10 @@ int main(int argc, char *argv[]){ terminate(ep); return EX_OSERR; }else if(ep[i].seek > 0) - switch(Io_fdseek(&ep[i])){ - case EX_OK: - output(ep, fmt_output); + Io_fdseek(&ep[i]); + if(ep[i].seek > 0){ terminate(ep); - return EX_OK; + return oserr(ep[i].fn); } }