From 5b1d4fef8828a6057183b95d3828564b4f1811c8 Mon Sep 17 00:00:00 2001 From: DTB Date: Wed, 3 Jul 2024 19:22:34 -0600 Subject: [PATCH] dj(1): remove Io_fdopen --- src/dj.c | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/src/dj.c b/src/dj.c index 6815a82..974bcf0 100644 --- a/src/dj.c +++ b/src/dj.c @@ -62,26 +62,6 @@ static int write_flags = O_WRONLY | O_CREAT; /* Macro to check if fd is stdin or stdout */ #define fdisstd(fd) ((fd) == STDIN_FILENO || (fd) == STDOUT_FILENO) -/* Opens io->fn and saves the file descriptor into io->fd. Returns io->fd, - * which will be -1 if an error occured. */ -static int -Io_fdopen(struct Io *io, char *fn){ - int fd; - - assert(io != NULL); - - if((fd = open(fn, io->fl, - /* these are the flags used by touch(1p) */ - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) - != -1 - && (fdisstd(io->fd) || close(io->fd) == 0)){ - io->fd = fd; - io->fn = fn; - } - - return fd; -} - /* 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. */ @@ -155,12 +135,12 @@ usage(char *s){ } int main(int argc, char *argv[]){ - int align; /* low 8b used, negative if no alignment is being done */ - int count; /* 0 if dj(1) runs until no more reads are possible */ - char *fmt; /* == fmt_asv (default) or fmt_human (-H) */ - size_t i; /* side of io being modified */ - struct Io io[2]; + int align; /* low 8b used, negative if no alignment is being done */ + int count; /* 0 if dj(1) runs until no more reads are possible */ + 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]; /* Set defaults. */ align = -1; @@ -190,10 +170,21 @@ int main(int argc, char *argv[]){ io[i].fd = i == 0 ? STDIN_FILENO : STDOUT_FILENO; io[i].fn = i == 0 ? stdin_name : stdout_name; break; - }else if(Io_fdopen(&io[i], optarg) != -1) - break; + }else{ + int fd; + + if((fd = open(optarg, io[i].fl, /* touch(1p) flags */ + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP + | S_IROTH | S_IWOTH)) + != -1 + && (fdisstd(io[i].fd) || close(io[i].fd) == 0)){ + io[i].fd = fd; + io[i].fn = optarg; + break; + } + } return oserr(optarg); - case 'n': noerror = 1; break; + case 'n': noerror = 1; break; case 'H': fmt = fmt_human; break; case 'a': if(optarg[0] == '\0' || optarg[1] == '\0'){