1
0

2024-01-04 work

This commit is contained in:
dtb 2024-01-04 21:16:38 -07:00
parent ce1a4851cd
commit 6cacfcdc86

View File

@ -2,17 +2,36 @@
#include <fcntl.h> /* open(2) */
#include <stdio.h> /* fprintf(3), stderr */
#include <stdlib.h> /* free(3), malloc(3), strtol(3), size_t */
#include <string.h> /* memset(3) */
#include <string.h> /* memcpy(3), memset(3) */
#include <sysexits.h> /* EX_OK, EX_USAGE */
#include <unistd.h> /* close(2), getopt(3), read(2), write(2), optarg, optind,
STDIN_FILENO, STDOUT_FILENO */
#include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2),
optarg, optind, STDIN_FILENO, STDOUT_FILENO */
/* Macro to use the appropriate string for argv[0] in the case that
* argc == 0. */
#define ARGV0 (argv[0] == NULL ? "<no argv[0]>" : argv[0])
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
/* Macro to ensure fd is not a std* file, e.g. stdin. */
#define fdisnotstd(fd) \
((fd) != STDIN_FILENO \
&& (fd) != STDOUT_FILENO \
&& (fd) != STDERR_FILENO)
/* Macro to call the cleanup functions that operate on struct io on the
* particular io[2] used in main. */
#define terminate(io) do{ \
Io_buffree(&(io)[0]); \
Io_buffree(&(io)[1]); \
Io_fdclose(&(io)[0]); \
Io_fdclose(&(io)[1]); }while(0)
static int read_flags = O_RDONLY;
static int write_flags = O_WRONLY | O_CREAT;
static char *stdin_name = "<stdin>";
static char *stdout_name = "<stdout>";
/* information that needs to be kept about each file in the "pipe" */
struct Io{
int bs; /* buffer size */
@ -21,29 +40,28 @@ struct Io{
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>";
/* Allocates *io's buffer. Returns NULL if successful. */
static void *
Io_bufalloc(struct Io *io){
return (io->buf = malloc(io->bs * (sizeof io->buf)));
}
static void
/* Frees *io's buffer. Returns io. */
static struct Io *
Io_buffree(struct Io *io){
free(io->buf);
return;
return io;
}
/* Closes io->fn and returns -1 on error, otherwise io->fd. */
static int
Io_fdclose(struct Io *io){
@ -52,6 +70,8 @@ Io_fdclose(struct Io *io){
: 0;
}
/* Opens io->fn and saves the file descriptor into io->fd. Returns io->fd,
* which will be -1 if an error occured. */
static int
Io_fdopen(struct Io *io, char *fn){
int fd;
@ -65,81 +85,79 @@ Io_fdopen(struct Io *io, char *fn){
return fd;
}
static int _read(int fd, void *b, size_t bs);
/* Reads io->bs bytes from *io's file descriptor into io->buf, storing the
* number of read bytes in io->bufuse. If io->bufuse is 0, errno will probably
* be set. Returns io. */
static struct Io *
Io_read(struct Io *io){
/* counter-intuitively, returns negative if successful and a sysexits.h exit
* code if an unrecoverable error occurred */
io->bufuse = read(io->fd, io->buf, io->bs);
return io;
}
/* Seeks io->seek bytes through *io's file descriptor, (counter-intuitively)
* returning -1 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);
}
memset(io->buf, '\0', io->bs);
do{ if( (io->fl == read_flags
&& (((io->bufuse = read(io->fd, io->buf,
MIN(io->bs, io->seek))) != 0)
/* second chance if 0 */
|| ((io->bufuse = read(io->fd, io->buf,
MIN(io->bs, io->seek))) != 0)))
/* cheat and use bufuse as the retval for write
* (the buffer is zeroed, it doesn't matter) */
|| (io->fl == write_flags
&& ((io->bufuse = write(io->fd, io->buf,
MIN(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]){
/* 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[0].bs = io[1].bs = 1024;
io[0].buf = io[1].buf = NULL;
io[0].fd = STDIN_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[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;
io->bs = 1024;
io->buf = NULL;
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;
return io;
}
/* read(2) but with a second chance if it returns zero */
/* Parses the string s to an integer, returning either the integer or in the
* case of an error a negative integer. This is used for argument parsing
* (e.g. -B [int]) in dj and no negative integer would be valid anyway. */
static int
_read(int fd, void *b, size_t bs){
int retval;
return ((retval = read(fd, buf, bs)) == 0)
? read(fd, buf, bs)
: retval;
}
static int /* on error returns -1, else returns non-negative */
parse(char *s){
int r;
errno = 0;
return (*s == '\0' /* empty string */
|| *(s = (int)strtol(s, &s, 0)) != '\0' /* not fully processed */
|| errno != 0)
r = (int)strtol(s, &s, 0);
return (*s == '\0' /* empty string */ || errno != 0)
? -1
: r;
}
/* s is the component (e.g. filename) that failed */
/* Prints an error message suitable for the event of an operating system error,
* with the error itself to be described in the string s. */
static int
oserr(char *argv0, char *s){
@ -148,7 +166,8 @@ oserr(char *argv0, char *s){
return EX_OSERR;
}
/* diagnostic output */
/* Prints statistics regarding the use of dj, particularly partially and
* completely read and written records. */
struct Io *
output(struct Io io[2]){
@ -158,21 +177,10 @@ output(struct Io io[2]){
return io;
}
static int
terminate(struct Io io[2]){
Io_buffree(&io[0]);
Io_buffree(&io[1]);
Io_fdclose(&io[0]);
Io_fdclose(&io[1]);
return EX_OK;
}
static int
usage(char **argv){
fprintf(stderr, "Usage: %s (-af) (-c [count]\n"
fprintf(stderr, "Usage: %s (-fA) (-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])"
@ -191,23 +199,29 @@ int main(int argc, char *argv[]){
struct Io io[2]; /* { read, write } same as pipe(2) */
/* defaults */
align = 0;
align = -1;
count = 0;
noerror = 0;
Io_setdefaults(io);
io[0].fl = read_flags;
Io_setdefaults(&io[0]);
io[1].fl = write_flags;
Io_setdefaults(&io[1]);
if(argc > 0)
while((c = getopt(argc, argv, "ac:i:b:fs:o:B:S:")) != -1)
while((c = getopt(argc, argv, "aAc:i:b:fs:o:B:S:")) != -1)
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;
else if(c == 'f')
/* boolean/flag arguments */
}else if(c == 'f')
noerror = 1;
/* numeric arguments */
else if(c == 'A')
align = '\0';
/* optarg */
else if(c == 'a' && optarg[0] != '\0' && optarg[1] == '\0')
align = optarg[0];
/* numeric args as a double negative to catch uncaught stuff */
else if(!((c == 'c' && (count = parse(optarg)) < 0)
|| (c == 'b' && (io[0].bs = parse(optarg)) < 0)
|| (c == 's' && (io[0].seek = parse(optarg)) < 0)
@ -228,34 +242,37 @@ int main(int argc, char *argv[]){
return EX_OSERR;
}else if(io[p].seek > 0) /* seek */
switch(Io_seek(&io[p])){
case EX_OK: return terminate(output(io));
case EX_OK: terminate(output(io)); return EX_OK;
}
}
do{ /* read */ if((io[0].bufuse
= read(io[0].fd, io[0].buf, io[0].bs))
< io[0].bs){
if(io[0].bufuse == 0 && (!noerror
|| ((io[0].bufuse = read(io[0].fd, io[0].buf, io[0].bs))
== 0)))
break; /* that's all she wrote */
else{
io[0].prec += 1;
if(noerror){
}else
count = 1;
if(align){
memset(io[0].buf + io[0].bufuse, '\0',
io[0].bs - io[0].bufuse);
io[0].bufuse = io[0].bs;
}
do{ /* read */
Io_read(&io[0]);
if(!noerror && io[0].bufuse == 0)
Io_read(&io[0]); /* second chance */
if(io[0].bufuse == 0) /* that's all she wrote */
break;
else if(io[0].bufuse < io[0].bs){
io[0].prec += 1;
if(noerror){
output(io);
}else
count = 1;
if(align >= 0){
memset(io[0].buf + io[0].bufuse, align,
io[0].bs - io[0].bufuse);
io[0].bufuse = io[0].bs;
}
}
/* write */ while(io[0].bufuse > 0){
memcpy(io[1].buf, io[0].buf, io[1].bs);
memmove(io[0].buf, io[0].buf + io[1].bs, io[0].bs - io[1].bs);
}
}while(count == 0 || --count > 0);
return terminate(output(io));
terminate(output(io));
return EX_OK;
}