some work on dj(1) for bonsai
This commit is contained in:
parent
f501a77764
commit
e5ebf44fbc
152
Wip/dj/dj.c
Normal file
152
Wip/dj/dj.c
Normal file
@ -0,0 +1,152 @@
|
||||
#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 */
|
||||
#include <sysexits.h> /* EX_OK, EX_USAGE */
|
||||
#include <unistd.h> /* close(2), getopt(3), optarg, optind, STDIN_FILENO,
|
||||
STDOUT_FILENO */
|
||||
|
||||
#define ARGV0 (argv[0] == NULL ? "<no argv[0]>" : argv[0])
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static int
|
||||
oserr(char *argv0, char *s){
|
||||
fprintf(stderr, "%s: %s: %s\n", argv0, s, strerror(errno));
|
||||
return EX_OSERR;
|
||||
}
|
||||
|
||||
static int
|
||||
usage(char *argv0){
|
||||
fprintf(stderr, "Usage: %s (-f) (-c [count]\n"
|
||||
"\t(-i [input file]) (-a) (-b [input block size])"
|
||||
" (-s [input offset])\n"
|
||||
"\t(-o [output file]) (-A) (-B [output block size])"
|
||||
" (-S [output offset])\n", ARGV0);
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
static char stdin_name = "<stdin>";
|
||||
static char stdout_name = "<stdout>";
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
struct Io{
|
||||
char align;
|
||||
char *fn;
|
||||
int rec;
|
||||
int prec;
|
||||
int fd;
|
||||
int seek;
|
||||
int bs;
|
||||
size_t bufuse;
|
||||
char *buf;
|
||||
} io[2]; /* { read, write } same as pipe(2) */
|
||||
int c;
|
||||
int count;
|
||||
size_t i;
|
||||
size_t p;
|
||||
char noerror;
|
||||
|
||||
/* defaults */
|
||||
count = 0;
|
||||
noerror = 0;
|
||||
io[0].align = io[1].align = 0;
|
||||
io[0].bs = io[1].bs = 1024;
|
||||
io[0].fd = STDIN_FILENO;
|
||||
io[0].fn = stdin_name;
|
||||
io[1].fd = STDOUT_FILENO;
|
||||
io[1].fn = stdout_name;
|
||||
io[0].seek = io[1].seek = 0;
|
||||
|
||||
if(argc > 0)
|
||||
while((c = getopt(argc, argv, "aAc: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)
|
||||
|| (io[p].fd != STDIN_FILENO
|
||||
&& io[p].fd != STDOUT_FILENO
|
||||
&& fclose(io[p].f) == EOF){
|
||||
return oserr(argv[0], optarg);
|
||||
}else{
|
||||
io[p].fd = t;
|
||||
io[p].fn = optarg;
|
||||
}
|
||||
/* numeric arguments */
|
||||
}else if(!((c == 'c' && (count = parse(optarg)) < 0)
|
||||
|| (c == 'b' && (io[0].bs = parse(optarg)) < 0)
|
||||
|| (c == 's' && (io[0].seek = parse(optarg)) < 0)
|
||||
|| (c == 'B' && (io[1].bs = parse(optarg)) < 0)
|
||||
|| (c == 'S' && (io[1].seek = parse(optarg)) < 0)))
|
||||
return usage(argv[0]);
|
||||
/* boolean arguments */
|
||||
else if(c == 'a' || c == 'A')
|
||||
io[c == 'A'].align = 1;
|
||||
else if(c == 'f')
|
||||
noerror = 1;
|
||||
|
||||
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);
|
||||
return EX_OSERR;
|
||||
}else if(io[p].seek > 0){ /* seek */
|
||||
if(io[p].fd == STDIN_FILENO){
|
||||
do{ io[p].seek -= (io[p].bufuse
|
||||
= read(io[p].fd, io[p].buf, io[p].ibs));
|
||||
if(io[p].bufuse == 0)
|
||||
while(io[p].seek > 0);
|
||||
|
||||
}
|
||||
for(i = 0; i < io[p].seek; ++i)
|
||||
(void)getc(io[p].f);
|
||||
else if(io[p].f == stdout)
|
||||
for(i = 0; i < io[p].seek; ++i)
|
||||
putc('\0', io[p].f;
|
||||
else if(fseek(io[p].f, io[p].seek, SEEK_SET) == -1)
|
||||
return oserr(argv[0], io[p].fn);
|
||||
}
|
||||
}
|
||||
|
||||
io[0].prec = io[0].rec = io[1].prec = io[1].rec = 0;
|
||||
|
||||
do{ if( (io[0].bufuse
|
||||
= fread(io[0].buf, sizeof io[0].buf, io[0].bs, io[0].f))
|
||||
< ibs){
|
||||
if(feof(io[0].f) != 0)
|
||||
count = 1;
|
||||
}
|
||||
|
||||
while(io[0].bufuse > 0){
|
||||
}
|
||||
|
||||
}while(count == 0 || --count > 0);
|
||||
|
||||
for(i = 0; i <= 1; ++i){
|
||||
free(io[i].buf);
|
||||
if(io[i].fd != STDIN_FILENO && io[i].fd != STDOUT_FILENO)
|
||||
close(io[i].fd);
|
||||
}
|
||||
|
||||
return EX_OK;
|
||||
}
|
Loading…
Reference in New Issue
Block a user