From eec81218d9ed9522e4d7dd4bcfefbeb88b7a6c57 Mon Sep 17 00:00:00 2001 From: DTB Date: Sun, 31 Dec 2023 22:28:49 -0700 Subject: [PATCH] 2023 work --- Wip/dj/dj.c | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/Wip/dj/dj.c b/Wip/dj/dj.c index 43033a6..1738fa4 100644 --- a/Wip/dj/dj.c +++ b/Wip/dj/dj.c @@ -8,6 +8,16 @@ #define ARGV0 (argv[0] == NULL ? "" : argv[0]) +/* read(2) but with a second chance if it returns zero */ +static int +_read(int fd, void *b, size_t bs){ + int retval; + + return ((retval = read(fd, buf, bs)) == 0) + ? read(fd, buf, bs) + : retval; +} + static int /* on error returns -1, else returns non-negative */ parse(char *s){ int r; @@ -27,6 +37,28 @@ oserr(char *argv0, char *s){ return EX_OSERR; } +struct Io * +output(struct Io io[2]){ + + fprintf(stderr, "%d:%d / %d:%d\n", + io[0].rec, io[0].prec, io[1].rec, io[1].prec);; + return io; +} + +static int +terminate(struct Io io[2]){ + + for(;; ++io){ + if(io->fd != STDIN_FILENO && io->fd != STDOUT_FILENO) + close(io->fd); + free(io->buf); + if(io->index == 1) + break; + } + + return EX_OK; +} + static int usage(char *argv0){ fprintf(stderr, "Usage: %s (-f) (-c [count]\n" @@ -42,6 +74,7 @@ static char stdout_name = ""; int main(int argc, char *argv[]){ struct Io{ + size_t index; char align; char *fn; int rec; @@ -67,6 +100,8 @@ int main(int argc, char *argv[]){ io[0].fn = stdin_name; io[1].fd = STDOUT_FILENO; io[1].fn = stdout_name; + io[0].index = 0; + io[1].index = 1; io[0].seek = io[1].seek = 0; if(argc > 0) @@ -113,13 +148,11 @@ int main(int argc, char *argv[]){ }else if(io[p].seek > 0){ /* seek */ if(io[p].fd == STDIN_FILENO){ do{ io[p].seek -= (io[p].bufuse - = read(io[p].fd, io[p].buf, io[p].ibs)); + = _read(io[p].fd, io[p].buf, + io[p].bs < io[p].seek ? io[p].bs : io[p].seek)); if(io[p].bufuse == 0) - while(io[p].seek > 0); - - } - for(i = 0; i < io[p].seek; ++i) - (void)getc(io[p].f); + return terminate(output(io)); + }while(io[p].seek > 0); else if(io[p].f == stdout) for(i = 0; i < io[p].seek; ++i) putc('\0', io[p].f;