From 2cfae0e8d7595964a492bf03f601512f0c7d15be Mon Sep 17 00:00:00 2001 From: DTB Date: Wed, 26 Jun 2024 15:15:37 -0600 Subject: [PATCH] dj(1): refactor (remove Io_setdefaults and other stuff) --- src/dj.c | 66 ++++++++++++++++++++++++-------------------------------- 1 file changed, 28 insertions(+), 38 deletions(-) diff --git a/src/dj.c b/src/dj.c index b45513e..5632645 100644 --- a/src/dj.c +++ b/src/dj.c @@ -16,7 +16,6 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ -#include /* isupper(3), tolower(3) */ #include /* errno */ #include /* open(2) */ #include /* fprintf(3), stderr */ @@ -29,6 +28,8 @@ S_IWUSR */ extern int errno; +char *program_name = "dj"; + /* dj uses two structures that respectively correspond to the reading and * writing ends of its jockeyed "pipe". User-configurable members are noted * with their relevant options. */ @@ -49,9 +50,9 @@ struct Io{ static char *fmt_asv = "%d\037%d\036%d\037%d\035%d\036%d\034"; static char *fmt_human = "%d+%d > %d+%d; %d > %d\n"; -static char *program_name = ""; static char *stdin_name = ""; static char *stdout_name = ""; + static int read_flags = O_RDONLY; /* Consistent with Busybox dd(1). */ static int write_flags = O_WRONLY | O_CREAT; @@ -195,23 +196,6 @@ Io_read(struct Io *io){ return io; } -/* Sets the variables in a struct *io to the defaults. Identifies the read/ - * write ends of the "pipe" by checking io->fl. Returns io. */ -static struct Io * -Io_setdefaults(struct Io *io){ - - io->bs = 1024 /* bytes; 1 KiB */; /* GNU dd(1) default; POSIX says 512B */ - io->buf = NULL; - io->bytes = 0; - io->fd = (io->fl == read_flags) ? STDIN_FILENO : STDOUT_FILENO; - io->fn = (io->fl == read_flags) ? stdin_name : stdout_name; - io->prec = 0; - io->rec = 0; - io->seek = 0; - - return io; -} - /* Writes io->bufuse units from io->buf to io->fd, permuting any unwritten * bytes to the start of io->buf and updating io->bufuse. If io->bufuse doesn't * change, errno will probably be set. Returns io. */ @@ -263,12 +247,12 @@ parse(char *s){ } static int -usage(void){ +usage(char *s){ fprintf(stderr, "Usage: %s (-Hn) (-a [byte]) (-c [count])\n" "\t(-i [input file]) (-b [input block size]) (-s [input offset])\n" "\t(-o [output file]) (-B [output block size]) (-S [output offset])\n", - program_name); + s); return EX_USAGE; } @@ -276,31 +260,37 @@ usage(void){ 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_output; /* == fmt_asv (default) or fmt_human (-H) */ + size_t i; /* side of io being modified */ struct Io io[2]; - char *fmt_output; /* fmt_asv (default) or fmt_human (-H) */ char noerror; /* 0=exits (default) 1=retries on partial reads or writes */ - int c; - int i; /* Set defaults. */ align = -1; count = 0; fmt_output = fmt_asv; noerror = 0; - io[0].fl = read_flags; - Io_setdefaults(&io[0]); - io[1].fl = write_flags; - Io_setdefaults(&io[1]); + for(i = 0; i < 2; ++i){ + io[i].bs = 1024 /* 1 KiB */; /* GNU dd(1) default; POSIX says 512B */ + io[i].bytes = 0; + io[i].fd = i ? STDIN_FILENO : STDOUT_FILENO; + io[i].fn = i ? stdin_name : stdout_name; + io[i].fl = i ? read_flags : write_flags; + io[i].prec = 0; + io[i].rec = 0; + io[i].seek = 0; + } if(argc > 0){ + int c; + program_name = argv[0]; while((c = getopt(argc, argv, "a:b:B:c:i:hHns:S:o:")) != -1) switch(c){ - case 'i': case 'o': - i = (c == 'o'); + case 'i': case 'o': i = (c == 'o') if(optarg[0] == '-' && optarg[1] == '\0'){ /* optarg == "-" */ - io[i].fd = (i == 0) ? STDIN_FILENO : STDOUT_FILENO; - io[i].fn = (i == 0) ? stdin_name : stdout_name; + io[i].fd = i ? STDIN_FILENO : STDOUT_FILENO; + io[i].fn = i ? stdin_name : stdout_name; break; }else if(Io_fdopen(&io[i], optarg) != -1) break; @@ -314,24 +304,24 @@ int main(int argc, char *argv[]){ break; } /* FALLTHROUGH */ - case 'c': case 'b': case 's': case 'B': case 'S': + case 'c': case 'b': case 's': case 'B': case 'S': /* numbers */ if(c == 'c' && (count = parse(optarg)) >= 0) break; - i = isupper(c); - c = tolower(c); + i = (c >= 'A' && c <= 'Z'); /* uppercase changes output */ + c &= 0x20 /* 0b 0010 0000 */; /* (ASCII) make lowercase */ if((c == 'b' && (io[i].bs = parse(optarg)) > 0) || (c == 's' && (io[i].seek = parse(optarg)) >= 0)) break; /* FALLTHROUGH */ default: terminate(io); - return usage(); + return usage(program_name); } } if(argc > optind){ terminate(io); - return usage(); + return usage(program_name); } for(i = 0; i < 2; ++i){ @@ -366,7 +356,7 @@ int main(int argc, char *argv[]){ ++io[0].rec; /* write */ - do{ if(io[1].bs > io[0].bs){ /* io[1].bs > io[0].bs */ + do{ if(io[1].bs > io[0].bs){ Io_bufxapp(&io[1], &io[0]); if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1) continue; /* we could write more */