1
0
Fork 0

2024-01-01 work

This commit is contained in:
dtb 2024-01-01 21:59:37 -07:00
parent eec81218d9
commit c2496ab0e8
1 changed files with 50 additions and 34 deletions

View File

@ -2,9 +2,10 @@
#include <fcntl.h> /* open(2) */
#include <stdio.h> /* fprintf(3), stderr */
#include <stdlib.h> /* free(3), malloc(3), strtol(3), size_t */
#include <string.h> /* memset(3) */
#include <sysexits.h> /* EX_OK, EX_USAGE */
#include <unistd.h> /* close(2), getopt(3), optarg, optind, STDIN_FILENO,
STDOUT_FILENO */
#include <unistd.h> /* close(2), getopt(3), read(2), write(2), optarg, optind,
STDIN_FILENO, STDOUT_FILENO */
#define ARGV0 (argv[0] == NULL ? "<no argv[0]>" : argv[0])
@ -61,10 +62,10 @@ terminate(struct Io io[2]){
static int
usage(char *argv0){
fprintf(stderr, "Usage: %s (-f) (-c [count]\n"
"\t(-i [input file]) (-a) (-b [input block size])"
fprintf(stderr, "Usage: %s (-af) (-c [count]\n"
"\t(-i [input file]) (-b [input block size])"
" (-s [input offset])\n"
"\t(-o [output file]) (-A) (-B [output block size])"
"\t(-o [output file]) (-B [output block size])"
" (-S [output offset])\n", ARGV0);
return EX_USAGE;
}
@ -75,7 +76,6 @@ static char stdout_name = "<stdout>";
int main(int argc, char *argv[]){
struct Io{
size_t index;
char align;
char *fn;
int rec;
int prec;
@ -85,6 +85,7 @@ int main(int argc, char *argv[]){
size_t bufuse;
char *buf;
} io[2]; /* { read, write } same as pipe(2) */
char align;
int c;
int count;
size_t i;
@ -92,10 +93,11 @@ int main(int argc, char *argv[]){
char noerror;
/* defaults */
align = 0;
count = 0;
noerror = 0;
io[0].align = io[1].align = 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;
@ -105,7 +107,7 @@ int main(int argc, char *argv[]){
io[0].seek = io[1].seek = 0;
if(argc > 0)
while((c = getopt(argc, argv, "aAc:i:b:fs:o:B:S:")) != -1)
while((c = getopt(argc, argv, "ac:i:b:fs:o:B:S:")) != -1)
if(c == 'i' || c == 'o'){ /* file arguments */
int t;
@ -118,23 +120,26 @@ int main(int argc, char *argv[]){
|| (io[p].fd != STDIN_FILENO
&& io[p].fd != STDOUT_FILENO
&& fclose(io[p].f) == EOF){
terminate(io);
return oserr(argv[0], optarg);
}else{
io[p].fd = t;
io[p].fn = optarg;
}
/* boolean arguments */
}else if(c == 'a')
align = 1;
else if(c == 'f')
noerror = 1;
/* numeric arguments */
}else if(!((c == 'c' && (count = parse(optarg)) < 0)
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)))
|| (c == 'S' && (io[1].seek = parse(optarg)) < 0))){
terminate(io);
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]);
@ -144,20 +149,37 @@ int main(int argc, char *argv[]){
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);
terminate(io);
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].bs < io[p].seek ? io[p].bs : io[p].seek));
if(io[p].bufuse == 0)
return terminate(output(io));
}while(io[p].seek > 0);
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);
if(io[p].fd == STDIN_FILENO
|| io[p].fd == STDOUT_FILENO
|| 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,
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);
}
}
}
}
@ -175,11 +197,5 @@ int main(int argc, char *argv[]){
}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;
return terminate(output(io));
}