1
0

2024-01-03 work

This commit is contained in:
dtb 2024-01-03 19:05:31 -07:00
parent 9f9f41da3e
commit ce1a4851cd

View File

@ -15,19 +15,86 @@
/* information that needs to be kept about each file in the "pipe" */ /* information that needs to be kept about each file in the "pipe" */
struct Io{ struct Io{
size_t index; /* index in io[] array */
char *fn; /* file name (may be stdin_name or stdout_name) */
int rec; /* records processed */
int prec; /* partial records processed */
int fd; /* file descriptor */
int seek; /* bytes to seek/skip (will be 0 after skippage) */
int bs; /* buffer size */ int bs; /* buffer size */
size_t bufuse; /* buffer usage */ size_t bufuse; /* buffer usage */
char *buf; /* buffer */ char *buf; /* buffer */
int fd; /* file descriptor */
int fl; /* file opening flags */
char *fn; /* file name (may be stdin_name or stdout_name) */
size_t index; /* index in io[] array */
int prec; /* partial records processed */
int rec; /* records processed */
int seek; /* bytes to seek/skip (will be 0 after skippage) */
}; };
static char stdin_name = "<stdin>"; static char *stdin_name = "<stdin>";
static char stdout_name = "<stdout>"; static char *stdout_name = "<stdout>";
static void *
Io_bufalloc(struct Io *io){
return (io->buf = malloc(io->bs * (sizeof io->buf)));
}
static void
Io_buffree(struct Io *io){
free(io->buf);
return;
}
static int
Io_fdclose(struct Io *io){
return fdisnotstd(io->fd)
? close(io->fd)
: 0;
}
static int
Io_fdopen(struct Io *io, char *fn){
int fd;
if((fd = open(fn, io->fl)) != -1
&& (fdisnotstd(io->fd) || close(io->fd) == 0)){
io->fd = fd;
io->fn = fn;
}
return fd;
}
static int _read(int fd, void *b, size_t bs);
/* counter-intuitively, returns negative if successful and a sysexits.h exit
* code if an unrecoverable error occurred */
static int
Io_seek(struct Io *io){
if(fdisnotstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) == -1)
return -1;
else if(io->fl == O_RDONLY){
do if((io->bufuse = _read(io->fd, io->buf,
(io->bs < io->seek) ? io->bs : io->seek)) != 0)
io->seek -= io->bufuse;
else /* not enough to skip */
return EX_OK;
while(io->seek > 0);
}else{
memset(io->buf, '\0', io->bs);
/* cheat and use bufuse as the retval for write
* (the buffer is zeroed, it doesn't matter) */
do if((io->bufuse = write(io->fd, io->buf,
(io->bs < io->seek) ? io->bs : io->seek)) != 0)
io->seek -= io->bufuse;
else
return EX_OK;
while(io->seek > 0);
}
return -1;
}
static void static void
Io_setdefaults(struct Io io[2]){ Io_setdefaults(struct Io io[2]){
@ -35,11 +102,15 @@ Io_setdefaults(struct Io io[2]){
io[0].bs = io[1].bs = 1024; io[0].bs = io[1].bs = 1024;
io[0].buf = io[1].buf = NULL; io[0].buf = io[1].buf = NULL;
io[0].fd = STDIN_FILENO; io[0].fd = STDIN_FILENO;
io[0].fn = stdin_name;
io[1].fd = STDOUT_FILENO; io[1].fd = STDOUT_FILENO;
io[0].fl = O_RDONLY;
io[1].fl = O_WRONLY | O_CREAT;
io[0].fn = stdin_name;
io[1].fn = stdout_name; io[1].fn = stdout_name;
io[0].index = 0; io[0].index = 0;
io[1].index = 1; io[1].index = 1;
io[0].prec = io[1].prec = 0;
io[0].rec = io[1].rec = 0;
io[0].seek = io[1].seek = 0; io[0].seek = io[1].seek = 0;
return; return;
@ -71,42 +142,42 @@ parse(char *s){
/* s is the component (e.g. filename) that failed */ /* s is the component (e.g. filename) that failed */
static int static int
oserr(char *argv0, char *s){ oserr(char *argv0, char *s){
fprintf(stderr, "%s: %s: %s\n", argv0, s, strerror(errno)); fprintf(stderr, "%s: %s: %s\n", argv0, s, strerror(errno));
return EX_OSERR; return EX_OSERR;
} }
/* diagnostic output /* diagnostic output */
* dd(1p) does %d+%d\n%d+%d\n */
struct Io * struct Io *
output(struct Io io[2]){ output(struct Io io[2]){
fprintf(stderr, "%d:%d / %d:%d\n", fprintf(stderr, "%d:%d / %d:%d\n", /* dd(1p) does %d+%d\n%d+%d\n */
io[0].rec, io[0].prec, io[1].rec, io[1].prec);; io[0].rec, io[0].prec, io[1].rec, io[1].prec);;
return io; return io;
} }
static int static int
terminate(struct Io io[2]){ terminate(struct Io io[2]){
for(;; ++io){ Io_buffree(&io[0]);
if(fdisnotstd(io->fd)) Io_buffree(&io[1]);
close(io->fd); Io_fdclose(&io[0]);
free(io->buf); Io_fdclose(&io[1]);
if(io->index == 1)
break;
}
return EX_OK; return EX_OK;
} }
static int static int
usage(char *argv0){ usage(char **argv){
fprintf(stderr, "Usage: %s (-af) (-c [count]\n" fprintf(stderr, "Usage: %s (-af) (-c [count]\n"
"\t(-i [input file]) (-b [input block size])" "\t(-i [input file]) (-b [input block size])"
" (-s [input offset])\n" " (-s [input offset])\n"
"\t(-o [output file]) (-B [output block size])" "\t(-o [output file]) (-B [output block size])"
" (-S [output offset])\n", ARGV0); " (-S [output offset])\n", ARGV0);
return EX_USAGE; return EX_USAGE;
} }
@ -127,23 +198,10 @@ int main(int argc, char *argv[]){
if(argc > 0) if(argc > 0)
while((c = getopt(argc, argv, "ac:i:b:fs:o:B:S:")) != -1) while((c = getopt(argc, argv, "ac:i:b:fs:o:B:S:")) != -1)
if(c == 'i' || c == 'o'){ /* file arguments */ if((c == 'i' || c == 'o')
int t; && Io_fdopen(&io[c == 'o'], optarg) != 0){ /* file args */
p = (c == 'o');
if( ((t = open(optarg,
c == 'i'
? O_RDONLY
: O_WRONLY | O_CREAT))
== -1)
|| (fdisnotstd(io[p].fd)
&& close(io[p].fd) != 0)){
terminate(io); terminate(io);
return oserr(argv[0], optarg); return oserr(argv[0], optarg);
}else{
io[p].fd = t;
io[p].fn = optarg;
}
/* boolean arguments */ /* boolean arguments */
}else if(c == 'a') }else if(c == 'a')
align = 1; align = 1;
@ -156,51 +214,23 @@ int main(int argc, char *argv[]){
|| (c == 'B' && (io[1].bs = parse(optarg)) < 0) || (c == 'B' && (io[1].bs = parse(optarg)) < 0)
|| (c == 'S' && (io[1].seek = parse(optarg)) < 0))){ || (c == 'S' && (io[1].seek = parse(optarg)) < 0))){
terminate(io); terminate(io);
return usage(argv[0]); return usage(argv);
} }
if(argc > optind) if(argc > optind)
return usage(argv[0]); return usage(argv);
for(p = 0; p <= 1; ++p){ for(p = 0; p <= 1; ++p){
/* buffer alloc */ if(Io_bufalloc(&io[p]) == NULL){
if((io[p].buf = malloc(io[p].bs * (sizeof io[p].buf))) == NULL){
fprintf(stderr, "%s: Failed to allocate %d bytes\n", ARGV0, fprintf(stderr, "%s: Failed to allocate %d bytes\n", ARGV0,
io[p].bs); io[p].bs);
terminate(io); terminate(io);
return EX_OSERR; return EX_OSERR;
}else if(io[p].seek > 0){ /* seek */ }else if(io[p].seek > 0) /* seek */
if(!fdisnotstd(io[p].fd) switch(Io_seek(&io[p])){
|| lseek(io[p].fd, io[p].seek, SEEK_SET) == -1){ case EX_OK: return terminate(output(io));
if(p == 0){
do if((io[p].bufuse = _read(io[p].fd, io[p].buf,
io[p].bs < io[p].seek
? io[p].bs
: io[p].seek))
!= 0)
io[p].seek -= io[p].bufuse;
else /* not enough to skip */
return terminate(output(io));
while(io[p].seek > 0);
}else{
memset(io[p].buf, '\0', io[p].bs);
/* cheat and use bufuse as the retval for write
* (the buffer is zeroed, it doesn't matter) */
do if((io[p].bufuse = write(io[p].fd, io[p].buf,
io[p].bs < io[p].seek
? io[p].bs
: io[p].seek))
!= 0)
io[p].seek -= io[p].bufuse;
else
return terminate(output(io));
while(io[p].seek > 0);
} }
} }
}
}
io[0].prec = io[0].rec = io[1].prec = io[1].rec = 0;
do{ /* read */ if((io[0].bufuse do{ /* read */ if((io[0].bufuse
= read(io[0].fd, io[0].buf, io[0].bs)) = read(io[0].fd, io[0].buf, io[0].bs))