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

232 lines
5.4 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-01 21:59:37 -07:00
#include <string.h> /* memset(3) */
2023-12-31 19:37:50 -07:00
#include <sysexits.h> /* EX_OK, EX_USAGE */
2024-01-01 21:59:37 -07:00
#include <unistd.h> /* close(2), getopt(3), read(2), write(2), optarg, optind,
STDIN_FILENO, STDOUT_FILENO */
2023-12-31 19:37:50 -07:00
#define ARGV0 (argv[0] == NULL ? "<no argv[0]>" : argv[0])
2024-01-02 22:31:21 -07:00
#define fdisnotstd(fd) \
((fd) != STDIN_FILENO \
&& (fd) != STDOUT_FILENO \
&& (fd) != STDERR_FILENO)
/* 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 */
};
static char stdin_name = "<stdin>";
static char stdout_name = "<stdout>";
static void
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[1].fn = stdout_name;
io[0].index = 0;
io[1].index = 1;
io[0].seek = io[1].seek = 0;
return;
}
2023-12-31 19:37:50 -07:00
2023-12-31 22:28:49 -07:00
/* read(2) but with a second chance if it returns zero */
static int
_read(int fd, void *b, size_t bs){
int retval;
return ((retval = read(fd, buf, bs)) == 0)
? read(fd, buf, bs)
: retval;
}
2023-12-31 19:37:50 -07:00
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)
? -1
: r;
}
2024-01-02 22:31:21 -07:00
/* s is the component (e.g. filename) that failed */
2023-12-31 19:37:50 -07:00
static int
oserr(char *argv0, char *s){
fprintf(stderr, "%s: %s: %s\n", argv0, s, strerror(errno));
return EX_OSERR;
}
2024-01-02 22:31:21 -07:00
/* diagnostic output
* dd(1p) does %d+%d\n%d+%d\n */
2023-12-31 22:28:49 -07:00
struct Io *
output(struct Io io[2]){
fprintf(stderr, "%d:%d / %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){
2024-01-02 22:31:21 -07:00
if(fdisnotstd(io->fd))
2023-12-31 22:28:49 -07:00
close(io->fd);
free(io->buf);
if(io->index == 1)
break;
}
return EX_OK;
}
2023-12-31 19:37:50 -07:00
static int
usage(char *argv0){
2024-01-02 22:31:21 -07:00
2024-01-01 21:59:37 -07:00
fprintf(stderr, "Usage: %s (-af) (-c [count]\n"
"\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);
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-01 21:59:37 -07:00
align = 0;
2023-12-31 19:37:50 -07:00
count = 0;
noerror = 0;
2024-01-02 22:31:21 -07:00
Io_setdefaults(io);
2023-12-31 19:37:50 -07:00
if(argc > 0)
2024-01-01 21:59:37 -07:00
while((c = getopt(argc, argv, "ac:i:b:fs:o:B:S:")) != -1)
2023-12-31 19:37:50 -07:00
if(c == 'i' || c == 'o'){ /* file arguments */
int t;
p = (c == 'o');
2024-01-02 22:31:21 -07:00
if( ((t = open(optarg,
2023-12-31 19:37:50 -07:00
c == 'i'
? O_RDONLY
: O_WRONLY | O_CREAT))
== -1)
2024-01-02 22:31:21 -07:00
|| (fdisnotstd(io[p].fd)
&& close(io[p].fd) != 0)){
2024-01-01 21:59:37 -07:00
terminate(io);
2023-12-31 19:37:50 -07:00
return oserr(argv[0], optarg);
}else{
io[p].fd = t;
io[p].fn = optarg;
}
2024-01-01 21:59:37 -07:00
/* boolean arguments */
}else if(c == 'a')
align = 1;
else if(c == 'f')
noerror = 1;
2023-12-31 19:37:50 -07:00
/* numeric arguments */
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);
2023-12-31 19:37:50 -07:00
return usage(argv[0]);
2024-01-01 21:59:37 -07:00
}
2023-12-31 19:37:50 -07:00
if(argc > optind)
return usage(argv[0]);
for(p = 0; p <= 1; ++p){
/* buffer alloc */
if((io[p].buf = malloc(io[p].bs * (sizeof io[p].buf))) == NULL){
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;
}else if(io[p].seek > 0){ /* seek */
2024-01-02 22:31:21 -07:00
if(!fdisnotstd(io[p].fd)
|| lseek(io[p].fd, io[p].seek, SEEK_SET) == -1){
2024-01-01 21:59:37 -07:00
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);
}
}
2023-12-31 19:37:50 -07:00
}
}
io[0].prec = io[0].rec = io[1].prec = io[1].rec = 0;
2024-01-02 22:31:21 -07:00
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
2023-12-31 19:37:50 -07:00
count = 1;
2024-01-02 22:31:21 -07:00
if(align){
memset(io[0].buf + io[0].bufuse, '\0',
io[0].bs - io[0].bufuse);
io[0].bufuse = io[0].bs;
}
}
2023-12-31 19:37:50 -07:00
}
2024-01-02 22:31:21 -07:00
/* write */ while(io[0].bufuse > 0){
2023-12-31 19:37:50 -07:00
}
}while(count == 0 || --count > 0);
2024-01-01 21:59:37 -07:00
return terminate(output(io));
2023-12-31 19:37:50 -07:00
}