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]){