1
0

2024-01-01 work

This commit is contained in:
dtb 2024-01-01 21:59:37 -07:00
parent eec81218d9
commit c2496ab0e8

View File

@ -2,9 +2,10 @@
#include <fcntl.h> /* open(2) */ #include <fcntl.h> /* open(2) */
#include <stdio.h> /* fprintf(3), stderr */ #include <stdio.h> /* fprintf(3), stderr */
#include <stdlib.h> /* free(3), malloc(3), strtol(3), size_t */ #include <stdlib.h> /* free(3), malloc(3), strtol(3), size_t */
#include <string.h> /* memset(3) */
#include <sysexits.h> /* EX_OK, EX_USAGE */ #include <sysexits.h> /* EX_OK, EX_USAGE */
#include <unistd.h> /* close(2), getopt(3), optarg, optind, STDIN_FILENO, #include <unistd.h> /* close(2), getopt(3), read(2), write(2), optarg, optind,
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])
@ -61,10 +62,10 @@ terminate(struct Io io[2]){
static int static int
usage(char *argv0){ usage(char *argv0){
fprintf(stderr, "Usage: %s (-f) (-c [count]\n" fprintf(stderr, "Usage: %s (-af) (-c [count]\n"
"\t(-i [input file]) (-a) (-b [input block size])" "\t(-i [input file]) (-b [input block size])"
" (-s [input offset])\n" " (-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); " (-S [output offset])\n", ARGV0);
return EX_USAGE; return EX_USAGE;
} }
@ -75,7 +76,6 @@ static char stdout_name = "<stdout>";
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
struct Io{ struct Io{
size_t index; size_t index;
char align;
char *fn; char *fn;
int rec; int rec;
int prec; int prec;
@ -85,6 +85,7 @@ int main(int argc, char *argv[]){
size_t bufuse; size_t bufuse;
char *buf; char *buf;
} io[2]; /* { read, write } same as pipe(2) */ } io[2]; /* { read, write } same as pipe(2) */
char align;
int c; int c;
int count; int count;
size_t i; size_t i;
@ -92,10 +93,11 @@ int main(int argc, char *argv[]){
char noerror; char noerror;
/* defaults */ /* defaults */
align = 0;
count = 0; count = 0;
noerror = 0; noerror = 0;
io[0].align = io[1].align = 0;
io[0].bs = io[1].bs = 1024; io[0].bs = io[1].bs = 1024;
io[0].buf = io[1].buf = NULL;
io[0].fd = STDIN_FILENO; io[0].fd = STDIN_FILENO;
io[0].fn = stdin_name; io[0].fn = stdin_name;
io[1].fd = STDOUT_FILENO; io[1].fd = STDOUT_FILENO;
@ -105,7 +107,7 @@ int main(int argc, char *argv[]){
io[0].seek = io[1].seek = 0; io[0].seek = io[1].seek = 0;
if(argc > 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 */ if(c == 'i' || c == 'o'){ /* file arguments */
int t; int t;
@ -118,23 +120,26 @@ int main(int argc, char *argv[]){
|| (io[p].fd != STDIN_FILENO || (io[p].fd != STDIN_FILENO
&& io[p].fd != STDOUT_FILENO && io[p].fd != STDOUT_FILENO
&& fclose(io[p].f) == EOF){ && fclose(io[p].f) == EOF){
terminate(io);
return oserr(argv[0], optarg); return oserr(argv[0], optarg);
}else{ }else{
io[p].fd = t; io[p].fd = t;
io[p].fn = optarg; io[p].fn = optarg;
} }
/* boolean arguments */
}else if(c == 'a')
align = 1;
else if(c == 'f')
noerror = 1;
/* numeric arguments */ /* 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 == 'b' && (io[0].bs = parse(optarg)) < 0)
|| (c == 's' && (io[0].seek = parse(optarg)) < 0) || (c == 's' && (io[0].seek = parse(optarg)) < 0)
|| (c == 'B' && (io[1].bs = 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]); 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) if(argc > optind)
return usage(argv[0]); 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){ if((io[p].buf = malloc(io[p].bs * (sizeof io[p].buf))) == NULL){
fprintf(stderr, "%s: Failed to allocate %d bytes\n", ARGV0, fprintf(stderr, "%s: Failed to allocate %d bytes\n", ARGV0,
io[p].bs); io[p].bs);
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(io[p].fd == STDIN_FILENO
do{ io[p].seek -= (io[p].bufuse || io[p].fd == STDOUT_FILENO
= _read(io[p].fd, io[p].buf, || lseek(io[p].fd, io[p].seek, SEEK_SET) == -1){
io[p].bs < io[p].seek ? io[p].bs : io[p].seek)); if(p == 0){
if(io[p].bufuse == 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)); return terminate(output(io));
}while(io[p].seek > 0); while(io[p].seek > 0);
else if(io[p].f == stdout) }else{
for(i = 0; i < io[p].seek; ++i) memset(io[p].buf, '\0', io[p].bs);
putc('\0', io[p].f; /* cheat and use bufuse as the retval for write
else if(fseek(io[p].f, io[p].seek, SEEK_SET) == -1) * (the buffer is zeroed, it doesn't matter) */
return oserr(argv[0], io[p].fn); 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); }while(count == 0 || --count > 0);
for(i = 0; i <= 1; ++i){ return terminate(output(io));
free(io[i].buf);
if(io[i].fd != STDIN_FILENO && io[i].fd != STDOUT_FILENO)
close(io[i].fd);
}
return EX_OK;
} }