forked from bonsai/harakit
dj(1): refactor (remove Io_setdefaults and other stuff)
This commit is contained in:
parent
fb74e7bef0
commit
2cfae0e8d7
66
src/dj.c
66
src/dj.c
@ -16,7 +16,6 @@
|
|||||||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <ctype.h> /* isupper(3), tolower(3) */
|
|
||||||
#include <errno.h> /* errno */
|
#include <errno.h> /* errno */
|
||||||
#include <fcntl.h> /* open(2) */
|
#include <fcntl.h> /* open(2) */
|
||||||
#include <stdio.h> /* fprintf(3), stderr */
|
#include <stdio.h> /* fprintf(3), stderr */
|
||||||
@ -29,6 +28,8 @@
|
|||||||
S_IWUSR */
|
S_IWUSR */
|
||||||
extern int errno;
|
extern int errno;
|
||||||
|
|
||||||
|
char *program_name = "dj";
|
||||||
|
|
||||||
/* dj uses two structures that respectively correspond to the reading and
|
/* dj uses two structures that respectively correspond to the reading and
|
||||||
* writing ends of its jockeyed "pipe". User-configurable members are noted
|
* writing ends of its jockeyed "pipe". User-configurable members are noted
|
||||||
* with their relevant options. */
|
* 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_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 *fmt_human = "%d+%d > %d+%d; %d > %d\n";
|
||||||
|
|
||||||
static char *program_name = "<no argv[0]>";
|
|
||||||
static char *stdin_name = "<stdin>";
|
static char *stdin_name = "<stdin>";
|
||||||
static char *stdout_name = "<stdout>";
|
static char *stdout_name = "<stdout>";
|
||||||
|
|
||||||
static int read_flags = O_RDONLY; /* Consistent with Busybox dd(1). */
|
static int read_flags = O_RDONLY; /* Consistent with Busybox dd(1). */
|
||||||
static int write_flags = O_WRONLY | O_CREAT;
|
static int write_flags = O_WRONLY | O_CREAT;
|
||||||
|
|
||||||
@ -195,23 +196,6 @@ Io_read(struct Io *io){
|
|||||||
return 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
|
/* 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
|
* bytes to the start of io->buf and updating io->bufuse. If io->bufuse doesn't
|
||||||
* change, errno will probably be set. Returns io. */
|
* change, errno will probably be set. Returns io. */
|
||||||
@ -263,12 +247,12 @@ parse(char *s){
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
usage(void){
|
usage(char *s){
|
||||||
|
|
||||||
fprintf(stderr, "Usage: %s (-Hn) (-a [byte]) (-c [count])\n"
|
fprintf(stderr, "Usage: %s (-Hn) (-a [byte]) (-c [count])\n"
|
||||||
"\t(-i [input file]) (-b [input block size]) (-s [input offset])\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",
|
"\t(-o [output file]) (-B [output block size]) (-S [output offset])\n",
|
||||||
program_name);
|
s);
|
||||||
|
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
}
|
}
|
||||||
@ -276,31 +260,37 @@ usage(void){
|
|||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
int align; /* low 8b used, negative if no alignment is being done */
|
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 */
|
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];
|
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 */
|
char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
|
||||||
int c;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
/* Set defaults. */
|
/* Set defaults. */
|
||||||
align = -1;
|
align = -1;
|
||||||
count = 0;
|
count = 0;
|
||||||
fmt_output = fmt_asv;
|
fmt_output = fmt_asv;
|
||||||
noerror = 0;
|
noerror = 0;
|
||||||
io[0].fl = read_flags;
|
for(i = 0; i < 2; ++i){
|
||||||
Io_setdefaults(&io[0]);
|
io[i].bs = 1024 /* 1 KiB */; /* GNU dd(1) default; POSIX says 512B */
|
||||||
io[1].fl = write_flags;
|
io[i].bytes = 0;
|
||||||
Io_setdefaults(&io[1]);
|
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){
|
if(argc > 0){
|
||||||
|
int c;
|
||||||
|
|
||||||
program_name = argv[0];
|
program_name = argv[0];
|
||||||
while((c = getopt(argc, argv, "a:b:B:c:i:hHns:S:o:")) != -1)
|
while((c = getopt(argc, argv, "a:b:B:c:i:hHns:S:o:")) != -1)
|
||||||
switch(c){
|
switch(c){
|
||||||
case 'i': case 'o':
|
case 'i': case 'o': i = (c == 'o')
|
||||||
i = (c == 'o');
|
|
||||||
if(optarg[0] == '-' && optarg[1] == '\0'){ /* optarg == "-" */
|
if(optarg[0] == '-' && optarg[1] == '\0'){ /* optarg == "-" */
|
||||||
io[i].fd = (i == 0) ? STDIN_FILENO : STDOUT_FILENO;
|
io[i].fd = i ? STDIN_FILENO : STDOUT_FILENO;
|
||||||
io[i].fn = (i == 0) ? stdin_name : stdout_name;
|
io[i].fn = i ? stdin_name : stdout_name;
|
||||||
break;
|
break;
|
||||||
}else if(Io_fdopen(&io[i], optarg) != -1)
|
}else if(Io_fdopen(&io[i], optarg) != -1)
|
||||||
break;
|
break;
|
||||||
@ -314,24 +304,24 @@ int main(int argc, char *argv[]){
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* FALLTHROUGH */
|
/* 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)
|
if(c == 'c' && (count = parse(optarg)) >= 0)
|
||||||
break;
|
break;
|
||||||
i = isupper(c);
|
i = (c >= 'A' && c <= 'Z'); /* uppercase changes output */
|
||||||
c = tolower(c);
|
c &= 0x20 /* 0b 0010 0000 */; /* (ASCII) make lowercase */
|
||||||
if((c == 'b' && (io[i].bs = parse(optarg)) > 0)
|
if((c == 'b' && (io[i].bs = parse(optarg)) > 0)
|
||||||
|| (c == 's' && (io[i].seek = parse(optarg)) >= 0))
|
|| (c == 's' && (io[i].seek = parse(optarg)) >= 0))
|
||||||
break;
|
break;
|
||||||
/* FALLTHROUGH */
|
/* FALLTHROUGH */
|
||||||
default:
|
default:
|
||||||
terminate(io);
|
terminate(io);
|
||||||
return usage();
|
return usage(program_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(argc > optind){
|
if(argc > optind){
|
||||||
terminate(io);
|
terminate(io);
|
||||||
return usage();
|
return usage(program_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i = 0; i < 2; ++i){
|
for(i = 0; i < 2; ++i){
|
||||||
@ -366,7 +356,7 @@ int main(int argc, char *argv[]){
|
|||||||
++io[0].rec;
|
++io[0].rec;
|
||||||
|
|
||||||
/* write */
|
/* 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]);
|
Io_bufxapp(&io[1], &io[0]);
|
||||||
if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1)
|
if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1)
|
||||||
continue; /* we could write more */
|
continue; /* we could write more */
|
||||||
|
Loading…
Reference in New Issue
Block a user