1
0
Fork 0

2023 work

This commit is contained in:
dtb 2023-12-31 22:28:49 -07:00
parent e5ebf44fbc
commit eec81218d9
1 changed files with 39 additions and 6 deletions

View File

@ -8,6 +8,16 @@
#define ARGV0 (argv[0] == NULL ? "<no argv[0]>" : argv[0])
/* read(2) but with a second chance if it returns zero */
static int
_read(int fd, void *b, size_t bs){
int retval;
return ((retval = read(fd, buf, bs)) == 0)
? read(fd, buf, bs)
: retval;
}
static int /* on error returns -1, else returns non-negative */
parse(char *s){
int r;
@ -27,6 +37,28 @@ oserr(char *argv0, char *s){
return EX_OSERR;
}
struct Io *
output(struct Io io[2]){
fprintf(stderr, "%d:%d / %d:%d\n",
io[0].rec, io[0].prec, io[1].rec, io[1].prec);;
return io;
}
static int
terminate(struct Io io[2]){
for(;; ++io){
if(io->fd != STDIN_FILENO && io->fd != STDOUT_FILENO)
close(io->fd);
free(io->buf);
if(io->index == 1)
break;
}
return EX_OK;
}
static int
usage(char *argv0){
fprintf(stderr, "Usage: %s (-f) (-c [count]\n"
@ -42,6 +74,7 @@ static char stdout_name = "<stdout>";
int main(int argc, char *argv[]){
struct Io{
size_t index;
char align;
char *fn;
int rec;
@ -67,6 +100,8 @@ int main(int argc, char *argv[]){
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)
@ -113,13 +148,11 @@ int main(int argc, char *argv[]){
}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));
= _read(io[p].fd, io[p].buf,
io[p].bs < io[p].seek ? io[p].bs : io[p].seek));
if(io[p].bufuse == 0)
while(io[p].seek > 0);
}
for(i = 0; i < io[p].seek; ++i)
(void)getc(io[p].f);
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;