2024-01-02 work
This commit is contained in:
parent
ba1a1ed210
commit
9f9f41da3e
106
Wip/dj/dj.c
106
Wip/dj/dj.c
@ -8,6 +8,42 @@
|
||||
STDIN_FILENO, STDOUT_FILENO */
|
||||
|
||||
#define ARGV0 (argv[0] == NULL ? "<no argv[0]>" : argv[0])
|
||||
#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;
|
||||
}
|
||||
|
||||
/* read(2) but with a second chance if it returns zero */
|
||||
static int
|
||||
@ -32,12 +68,15 @@ parse(char *s){
|
||||
: r;
|
||||
}
|
||||
|
||||
/* s is the component (e.g. filename) that failed */
|
||||
static int
|
||||
oserr(char *argv0, char *s){
|
||||
fprintf(stderr, "%s: %s: %s\n", argv0, s, strerror(errno));
|
||||
return EX_OSERR;
|
||||
}
|
||||
|
||||
/* diagnostic output
|
||||
* dd(1p) does %d+%d\n%d+%d\n */
|
||||
struct Io *
|
||||
output(struct Io io[2]){
|
||||
|
||||
@ -50,7 +89,7 @@ static int
|
||||
terminate(struct Io io[2]){
|
||||
|
||||
for(;; ++io){
|
||||
if(io->fd != STDIN_FILENO && io->fd != STDOUT_FILENO)
|
||||
if(fdisnotstd(io->fd))
|
||||
close(io->fd);
|
||||
free(io->buf);
|
||||
if(io->index == 1)
|
||||
@ -62,6 +101,7 @@ terminate(struct Io io[2]){
|
||||
|
||||
static int
|
||||
usage(char *argv0){
|
||||
|
||||
fprintf(stderr, "Usage: %s (-af) (-c [count]\n"
|
||||
"\t(-i [input file]) (-b [input block size])"
|
||||
" (-s [input offset])\n"
|
||||
@ -70,41 +110,20 @@ usage(char *argv0){
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
static char stdin_name = "<stdin>";
|
||||
static char stdout_name = "<stdout>";
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
struct Io{
|
||||
size_t index;
|
||||
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) */
|
||||
char align;
|
||||
char noerror;
|
||||
int c;
|
||||
int count;
|
||||
size_t i;
|
||||
size_t p;
|
||||
char noerror;
|
||||
int p; /* pipe side */
|
||||
struct Io io[2]; /* { read, write } same as pipe(2) */
|
||||
|
||||
/* defaults */
|
||||
align = 0;
|
||||
count = 0;
|
||||
noerror = 0;
|
||||
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;
|
||||
Io_setdefaults(io);
|
||||
|
||||
if(argc > 0)
|
||||
while((c = getopt(argc, argv, "ac:i:b:fs:o:B:S:")) != -1)
|
||||
@ -112,14 +131,13 @@ int main(int argc, char *argv[]){
|
||||
int t;
|
||||
|
||||
p = (c == 'o');
|
||||
if( (t = open(optarg,
|
||||
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){
|
||||
|| (fdisnotstd(io[p].fd)
|
||||
&& close(io[p].fd) != 0)){
|
||||
terminate(io);
|
||||
return oserr(argv[0], optarg);
|
||||
}else{
|
||||
@ -152,8 +170,7 @@ int main(int argc, char *argv[]){
|
||||
terminate(io);
|
||||
return EX_OSERR;
|
||||
}else if(io[p].seek > 0){ /* seek */
|
||||
if(io[p].fd == STDIN_FILENO
|
||||
|| io[p].fd == STDOUT_FILENO
|
||||
if(!fdisnotstd(io[p].fd)
|
||||
|| lseek(io[p].fd, io[p].seek, SEEK_SET) == -1){
|
||||
if(p == 0){
|
||||
do if((io[p].bufuse = _read(io[p].fd, io[p].buf,
|
||||
@ -185,16 +202,29 @@ int main(int argc, char *argv[]){
|
||||
|
||||
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)
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while(io[0].bufuse > 0){
|
||||
/* write */ while(io[0].bufuse > 0){
|
||||
}
|
||||
|
||||
}while(count == 0 || --count > 0);
|
||||
|
||||
return terminate(output(io));
|
||||
|
Loading…
Reference in New Issue
Block a user