1
0
Fork 0

2024-01-03 work

This commit is contained in:
dtb 2024-01-03 19:05:31 -07:00
parent 9f9f41da3e
commit ce1a4851cd
1 changed files with 101 additions and 71 deletions

View File

@ -15,19 +15,86 @@
/* information that needs to be kept about each file in the "pipe" */
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 */
size_t bufuse; /* buffer usage */
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 stdout_name = "<stdout>";
static char *stdin_name = "<stdin>";
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
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].buf = io[1].buf = NULL;
io[0].fd = STDIN_FILENO;
io[0].fn = stdin_name;
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[0].index = 0;
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;
return;
@ -71,42 +142,42 @@ parse(char *s){
/* s is the component (e.g. filename) that failed */
static int
oserr(char *argv0, char *s){
fprintf(stderr, "%s: %s: %s\n", argv0, s, strerror(errno));
return EX_OSERR;
}
/* diagnostic output
* dd(1p) does %d+%d\n%d+%d\n */
/* diagnostic output */
struct Io *
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);;
return io;
}
static int
terminate(struct Io io[2]){
for(;; ++io){
if(fdisnotstd(io->fd))
close(io->fd);
free(io->buf);
if(io->index == 1)
break;
}
Io_buffree(&io[0]);
Io_buffree(&io[1]);
Io_fdclose(&io[0]);
Io_fdclose(&io[1]);
return EX_OK;
}
static int
usage(char *argv0){
usage(char **argv){
fprintf(stderr, "Usage: %s (-af) (-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", ARGV0);
return EX_USAGE;
}
@ -127,23 +198,10 @@ int main(int argc, char *argv[]){
if(argc > 0)
while((c = getopt(argc, argv, "ac:i:b:fs:o:B:S:")) != -1)
if(c == 'i' || c == 'o'){ /* file arguments */
int t;
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);
return oserr(argv[0], optarg);
}else{
io[p].fd = t;
io[p].fn = optarg;
}
if((c == 'i' || c == 'o')
&& Io_fdopen(&io[c == 'o'], optarg) != 0){ /* file args */
terminate(io);
return oserr(argv[0], optarg);
/* boolean arguments */
}else if(c == 'a')
align = 1;
@ -156,52 +214,24 @@ int main(int argc, char *argv[]){
|| (c == 'B' && (io[1].bs = parse(optarg)) < 0)
|| (c == 'S' && (io[1].seek = parse(optarg)) < 0))){
terminate(io);
return usage(argv[0]);
return usage(argv);
}
if(argc > optind)
return usage(argv[0]);
return usage(argv);
for(p = 0; p <= 1; ++p){
/* buffer alloc */
if((io[p].buf = malloc(io[p].bs * (sizeof io[p].buf))) == NULL){
if(Io_bufalloc(&io[p]) == NULL){
fprintf(stderr, "%s: Failed to allocate %d bytes\n", ARGV0,
io[p].bs);
terminate(io);
return EX_OSERR;
}else if(io[p].seek > 0){ /* seek */
if(!fdisnotstd(io[p].fd)
|| lseek(io[p].fd, io[p].seek, SEEK_SET) == -1){
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);
}
}else if(io[p].seek > 0) /* seek */
switch(Io_seek(&io[p])){
case EX_OK: return terminate(output(io));
}
}
}
io[0].prec = io[0].rec = io[1].prec = io[1].rec = 0;
do{ /* read */ if((io[0].bufuse
= read(io[0].fd, io[0].buf, io[0].bs))
< io[0].bs){