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