1
0
Files
src/Wip/dj/dj.c

327 lines
8.5 KiB
C
Raw Normal View History

2023-12-31 19:37:50 -07:00
#include <errno.h> /* errno */
#include <fcntl.h> /* open(2) */
#include <stdio.h> /* fprintf(3), stderr */
#include <stdlib.h> /* free(3), malloc(3), strtol(3), size_t */
2024-01-06 09:24:53 -07:00
#include <string.h> /* memcpy(3), memmove(3), memset(3) */
2023-12-31 19:37:50 -07:00
#include <sysexits.h> /* EX_OK, EX_USAGE */
2024-01-04 21:16:38 -07:00
#include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2),
optarg, optind, STDIN_FILENO, STDOUT_FILENO */
2023-12-31 19:37:50 -07:00
2024-01-04 21:16:38 -07:00
/* Macro to use the appropriate string for argv[0] in the case that
* argc == 0. */
2023-12-31 19:37:50 -07:00
#define ARGV0 (argv[0] == NULL ? "<no argv[0]>" : argv[0])
2024-01-04 21:16:38 -07:00
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
/* Macro to ensure fd is not a std* file, e.g. stdin. */
2024-01-02 22:31:21 -07:00
#define fdisnotstd(fd) \
((fd) != STDIN_FILENO \
&& (fd) != STDOUT_FILENO \
&& (fd) != STDERR_FILENO)
2024-01-04 21:16:38 -07:00
/* Macro to call the cleanup functions that operate on struct io on the
2024-01-06 09:24:53 -07:00
* particular io[2] used in main. Error conditions are not checked because this
* is only used when the program is about to terminate (hence its name). */
2024-01-04 21:16:38 -07:00
#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>";
2024-01-02 22:31:21 -07:00
/* information that needs to be kept about each file in the "pipe" */
struct Io{
int bs; /* buffer size */
size_t bufuse; /* buffer usage */
char *buf; /* buffer */
2024-01-03 19:05:31 -07:00
int fd; /* file descriptor */
int fl; /* file opening flags */
char *fn; /* file name (may be stdin_name or stdout_name) */
int prec; /* partial records processed */
int rec; /* records processed */
int seek; /* bytes to seek/skip (will be 0 after skippage) */
2024-01-02 22:31:21 -07:00
};
2024-01-04 21:16:38 -07:00
/* Allocates *io's buffer. Returns NULL if successful. */
2024-01-03 19:05:31 -07:00
static void *
Io_bufalloc(struct Io *io){
return (io->buf = malloc(io->bs * (sizeof io->buf)));
}
2024-01-04 21:16:38 -07:00
/* Frees *io's buffer. Returns io. */
static struct Io *
2024-01-03 19:05:31 -07:00
Io_buffree(struct Io *io){
free(io->buf);
2024-01-04 21:16:38 -07:00
return io;
2024-01-03 19:05:31 -07:00
}
2024-01-06 09:24:53 -07:00
/* Fills the unused portion of io's buffer with padding, updating io->bufuse.
* Returns io. */
static struct Io *
Io_bufrpad(struct Io *io, int padding){
memset(io->buf + io->bufuse, padding, io->bs - io->bufuse);
io->bufuse = io->bs;
return io;
}
/* Copies from the buffer in src to the buffer in dest no more than n units,
* removing the copied units from src and permuting the remaining units in the
* src buffer to the start of the buffer, modifying both the src and dest
* bufuse and returning dest. */
static struct Io*
Io_bufxfer(struct Io *dest, struct Io *src, int n){
memcpy(dest->buf, src->buf, (dest->bufuse = n));
memmove(src->buf, src->buf + n, (src->bufuse -= n));
return dest;
}
2024-01-04 21:16:38 -07:00
/* Closes io->fn and returns -1 on error, otherwise io->fd. */
2024-01-03 19:05:31 -07:00
static int
Io_fdclose(struct Io *io){
return fdisnotstd(io->fd)
? close(io->fd)
: 0;
}
2024-01-04 21:16:38 -07:00
/* Opens io->fn and saves the file descriptor into io->fd. Returns io->fd,
* which will be -1 if an error occured. */
2024-01-03 19:05:31 -07:00
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;
}
2024-01-04 21:16:38 -07:00
/* 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){
2024-01-03 19:05:31 -07:00
2024-01-04 21:16:38 -07:00
io->bufuse = read(io->fd, io->buf, io->bs);
return io;
}
2024-01-06 09:24:53 -07:00
/* 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
* change, errno will probably be set. Returns io. */
static struct Io *
Io_write(struct Io *io){
int t;
if((t = write(io->fd, io->buf, io->bufuse)) > 0)
memmove(io->buf, io->buf + t, (io->bufuse -= t));
return io;
}
2024-01-04 21:16:38 -07:00
/* 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. */
2024-01-03 19:05:31 -07:00
static int
Io_seek(struct Io *io){
if(fdisnotstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) == -1)
return -1;
2024-01-04 21:16:38 -07:00
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);
2024-01-03 19:05:31 -07:00
return -1;
}
2024-01-02 22:31:21 -07:00
2024-01-04 21:16:38 -07:00
/* 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){
2024-01-02 22:31:21 -07:00
2024-01-04 21:16:38 -07:00
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;
2024-01-02 22:31:21 -07:00
2024-01-04 21:16:38 -07:00
return io;
2024-01-02 22:31:21 -07:00
}
2023-12-31 19:37:50 -07:00
2024-01-04 21:16:38 -07:00
/* 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. */
2023-12-31 22:28:49 -07:00
static int
2023-12-31 19:37:50 -07:00
parse(char *s){
int r;
errno = 0;
2024-01-04 21:16:38 -07:00
r = (int)strtol(s, &s, 0);
return (*s == '\0' /* empty string */ || errno != 0)
2023-12-31 19:37:50 -07:00
? -1
: r;
}
2024-01-04 21:16:38 -07:00
/* Prints an error message suitable for the event of an operating system error,
* with the error itself to be described in the string s. */
2023-12-31 19:37:50 -07:00
static int
oserr(char *argv0, char *s){
2024-01-03 19:05:31 -07:00
2023-12-31 19:37:50 -07:00
fprintf(stderr, "%s: %s: %s\n", argv0, s, strerror(errno));
2024-01-03 19:05:31 -07:00
2023-12-31 19:37:50 -07:00
return EX_OSERR;
}
2024-01-04 21:16:38 -07:00
/* Prints statistics regarding the use of dj, particularly partially and
* completely read and written records. */
2023-12-31 22:28:49 -07:00
struct Io *
output(struct Io io[2]){
2024-01-06 09:24:53 -07:00
fprintf(stderr, "%d:%d > %d:%d\n", /* dd(1p) does %d+%d\n%d+%d\n */
2023-12-31 22:28:49 -07:00
io[0].rec, io[0].prec, io[1].rec, io[1].prec);;
2024-01-03 19:05:31 -07:00
2023-12-31 22:28:49 -07:00
return io;
}
2023-12-31 19:37:50 -07:00
static int
2024-01-03 19:05:31 -07:00
usage(char **argv){
2024-01-02 22:31:21 -07:00
2024-01-04 21:16:38 -07:00
fprintf(stderr, "Usage: %s (-fA) (-a [byte]) (-c [count]\n"
2024-01-01 21:59:37 -07:00
"\t(-i [input file]) (-b [input block size])"
2023-12-31 19:37:50 -07:00
" (-s [input offset])\n"
2024-01-01 21:59:37 -07:00
"\t(-o [output file]) (-B [output block size])"
2023-12-31 19:37:50 -07:00
" (-S [output offset])\n", ARGV0);
2024-01-03 19:05:31 -07:00
2023-12-31 19:37:50 -07:00
return EX_USAGE;
}
int main(int argc, char *argv[]){
2024-01-01 21:59:37 -07:00
char align;
2024-01-02 22:31:21 -07:00
char noerror;
2023-12-31 19:37:50 -07:00
int c;
int count;
2024-01-02 22:31:21 -07:00
int p; /* pipe side */
struct Io io[2]; /* { read, write } same as pipe(2) */
2023-12-31 19:37:50 -07:00
/* defaults */
2024-01-04 21:16:38 -07:00
align = -1;
2023-12-31 19:37:50 -07:00
count = 0;
noerror = 0;
2024-01-04 21:16:38 -07:00
io[0].fl = read_flags;
Io_setdefaults(&io[0]);
io[1].fl = write_flags;
Io_setdefaults(&io[1]);
2023-12-31 19:37:50 -07:00
if(argc > 0)
2024-01-04 21:16:38 -07:00
while((c = getopt(argc, argv, "aAc:i:b:fs:o:B:S:")) != -1)
2024-01-03 19:05:31 -07:00
if((c == 'i' || c == 'o')
&& Io_fdopen(&io[c == 'o'], optarg) != 0){ /* file args */
terminate(io);
return oserr(argv[0], optarg);
2024-01-04 21:16:38 -07:00
/* boolean/flag arguments */
}else if(c == 'f')
2024-01-01 21:59:37 -07:00
noerror = 1;
2024-01-04 21:16:38 -07:00
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 */
2024-01-01 21:59:37 -07:00
else if(!((c == 'c' && (count = parse(optarg)) < 0)
2023-12-31 19:37:50 -07:00
|| (c == 'b' && (io[0].bs = parse(optarg)) < 0)
|| (c == 's' && (io[0].seek = parse(optarg)) < 0)
|| (c == 'B' && (io[1].bs = parse(optarg)) < 0)
2024-01-01 21:59:37 -07:00
|| (c == 'S' && (io[1].seek = parse(optarg)) < 0))){
terminate(io);
2024-01-03 19:05:31 -07:00
return usage(argv);
2024-01-01 21:59:37 -07:00
}
2023-12-31 19:37:50 -07:00
if(argc > optind)
2024-01-03 19:05:31 -07:00
return usage(argv);
2023-12-31 19:37:50 -07:00
for(p = 0; p <= 1; ++p){
2024-01-03 19:05:31 -07:00
if(Io_bufalloc(&io[p]) == NULL){
2023-12-31 19:37:50 -07:00
fprintf(stderr, "%s: Failed to allocate %d bytes\n", ARGV0,
io[p].bs);
2024-01-01 21:59:37 -07:00
terminate(io);
2023-12-31 19:37:50 -07:00
return EX_OSERR;
2024-01-03 19:05:31 -07:00
}else if(io[p].seek > 0) /* seek */
switch(Io_seek(&io[p])){
2024-01-04 21:16:38 -07:00
case EX_OK: terminate(output(io)); return EX_OK;
2024-01-01 21:59:37 -07:00
}
2023-12-31 19:37:50 -07:00
}
2024-01-04 21:16:38 -07:00
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){
2024-01-06 09:24:53 -07:00
++io[0].prec;
2024-01-04 21:16:38 -07:00
if(noerror){
output(io);
}else
count = 1;
2024-01-06 09:24:53 -07:00
if(align >= 0)
Io_bufrpad(&io[0], align);
}else
++io[0].rec;
2023-12-31 19:37:50 -07:00
2024-01-02 22:31:21 -07:00
/* write */ while(io[0].bufuse > 0){
2024-01-06 09:24:53 -07:00
Io_bufxfer(&io[1], &io[0], MIN(io[0].bufuse, io[1].bs));
c = io[1].bufuse;
Io_write(&io[1]);
if(!noerror && io[1].bufuse == c)
Io_write(&io[1]); /* second chance */
if(io[1].bufuse == c) /* no more love */
count = 1;
else if(c > io[1].bufuse && io[1].bufuse > 0){
io[1].prec += 1;
if(noerror){
output(io);
}else
count = 1;
}else if(io[1].bufuse == 0)
++io[1].rec;
2023-12-31 19:37:50 -07:00
}
}while(count == 0 || --count > 0);
2024-01-04 21:16:38 -07:00
terminate(output(io));
return EX_OK;
2023-12-31 19:37:50 -07:00
}