dj(1): reformatting

This commit is contained in:
Emma Tebibyte 2024-07-12 15:23:57 -06:00
parent 8f8de5de2b
commit 6cf7fd9794
Signed by untrusted user: emma
GPG Key ID: 06FA419A1698C270

177
src/dj.c
View File

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2024 DTB <trinity@trinity.moe> * Copyright (c) 2024 DTB <trinity@trinity.moe>
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: AGPL-3.0-or-later * SPDX-License-Identifier: AGPL-3.0-or-later
* *
* This program is free software: you can redistribute it and/or modify it under * This program is free software: you can redistribute it and/or modify it under
@ -27,8 +28,8 @@
#endif #endif
#include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2), #include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2),
* optarg, optind, STDIN_FILENO, STDOUT_FILENO */ * optarg, optind, STDIN_FILENO, STDOUT_FILENO */
#include <sys/stat.h> /* S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, #include <sys/stat.h> /* S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR */
S_IWUSR */
extern int errno; extern int errno;
char *program_name = "dj"; char *program_name = "dj";
@ -67,8 +68,7 @@ static int write_flags = O_WRONLY | O_CREAT;
/* Macro to check if fd is stdin or stdout */ /* Macro to check if fd is stdin or stdout */
#define fdisstd(fd) ((fd) == STDIN_FILENO || (fd) == STDOUT_FILENO) #define fdisstd(fd) ((fd) == STDIN_FILENO || (fd) == STDOUT_FILENO)
static struct Io * static struct Io * Io_read(struct Io *io) {
Io_read(struct Io *io){
int t; int t;
assert(io->bs > 0); assert(io->bs > 0);
@ -89,8 +89,7 @@ Io_read(struct Io *io){
return io; return io;
} }
static struct Io * static struct Io * Io_write(struct Io *io) {
Io_write(struct Io *io){
int t; int t;
assert(io->bufuse > 0); assert(io->bufuse > 0);
@ -109,20 +108,24 @@ Io_write(struct Io *io){
return io; return io;
} }
static int static int oserr(char *e, int n) {
oserr(char *e, int n){
fprintf(stderr, "%s: %s: %s\n", program_name, e, strerror(n)); fprintf(stderr, "%s: %s: %s\n", program_name, e, strerror(n));
return EX_OSERR; return EX_OSERR;
} }
/* Prints statistics regarding the use of dj, particularly partially and /* Prints statistics regarding the use of dj, particularly partially and
* completely read and written records. */ * completely read and written records. */
static void static void fprintio(FILE *stream, char *fmt, struct Io io[2]) {
fprintio(FILE *stream, char *fmt, struct Io io[2]){ fprintf(
stream,
fprintf(stream, fmt, fmt,
io[0].rec, io[0].prec, io[1].rec, io[1].prec, io[0].rec,
io[0].bytes, io[1].bytes); io[0].prec,
io[1].rec,
io[1].prec,
io[0].bytes,
io[1].bytes
);
return; return;
} }
@ -130,8 +133,7 @@ fprintio(FILE *stream, char *fmt, struct Io io[2]){
/* Parses the string s to an integer, returning either the integer or in the /* Parses the string s to an integer, returning either the integer or in the
* case of an error a negative integer. This is used for argument parsing * case of an error a negative integer. This is used for argument parsing
* (e.g. -B [int]) in dj and no negative integer would be valid anyway. */ * (e.g. -B [int]) in dj and no negative integer would be valid anyway. */
static long static long parse(char *s){
parse(char *s){
long r; long r;
errno = 0; errno = 0;
@ -141,13 +143,12 @@ parse(char *s){
: -1; : -1;
} }
static int static int usage(char *s){
usage(char *s){ fprintf(
stderr, "Usage: %s [-Hn] [-a byte] [-c count]\n"
fprintf(stderr, "Usage: %s [-Hn] [-a byte] [-c count]\n"
"\t[-i file] [-b block_size] [-s offset]\n" "\t[-i file] [-b block_size] [-s offset]\n"
"\t[-o file] [-B block_size] [-S offset]\n", "\t[-o file] [-B block_size] [-S offset]\n", program_name
program_name); );
return EX_USAGE; return EX_USAGE;
} }
@ -178,44 +179,56 @@ int main(int argc, char *argv[]){
io[i].seek = 0; io[i].seek = 0;
} }
if(argc > 0){ if (!argc < 0) { usage(program_name); }
int c; int c;
program_name = argv[0]; program_name = argv[0];
while((c = getopt(argc, argv, ":a:b:B:c:i:hHns:S:o:")) != -1) while ((c = getopt(argc, argv, "a:b:B:c:i:hHns:S:o:")) != -1) {
switch (c) { switch (c) {
case 'i': case 'o': i = (c == 'o'); case 'i': case 'o': /* input, output */
if(optarg[0] == '-' && optarg[1] == '\0'){ /* optarg == "-" */ i = (c == 'o');
/* optarg == "-" (stdin/stdout) */
if (optarg[0] == '-' && optarg[1] == '\0') {
io[i].fd = i == 0 ? STDIN_FILENO : STDOUT_FILENO; io[i].fd = i == 0 ? STDIN_FILENO : STDOUT_FILENO;
io[i].fn = i == 0 ? stdin_name : stdout_name; io[i].fn = i == 0 ? stdin_name : stdout_name;
break; break;
} else { } else {
int fd; int fd;
if((fd = open(optarg, io[i].fl, creat_mode)) != -1 if ((
&& (fdisstd(io[i].fd) || close(io[i].fd) == 0)){ fd = open(optarg, io[i].fl, creat_mode)) != -1
&& (fdisstd(io[i].fd) || close(io[i].fd) == 0
)) {
io[i].fd = fd; io[i].fd = fd;
io[i].fn = optarg; io[i].fn = optarg;
break; break;
} }
} }
return oserr(optarg, errno);
case 'n': noerror = 1; break; return oserr(optarg, errno); /* break; */
case 'H': fmt = fmt_human; break; case 'n': noerror = 1; break; /* retry failed reads once */
case 'a': case 'H': fmt = fmt_human; break; /* human-readable output */
case 'a': /* input buffer padding */
if (optarg[0] == '\0' || optarg[1] == '\0') { if (optarg[0] == '\0' || optarg[1] == '\0') {
align = optarg[0]; align = optarg[0];
break; break;
} }
/* FALLTHROUGH */ /* FALLTHROUGH */
case 'c': case 'b': case 's': case 'B': case 'S': /* numbers */ case 'c': /* number of reads */
if(c == 'c' && (count = parse(optarg)) >= 0) case 'b': case 'B': /* input/output block size */
break; case 's': case 'S': /* (s)kip/(S)eek in input/output */
if (c == 'c' && (count = parse(optarg)) >= 0) { break; }
i = (c >= 'A' && c <= 'Z'); i = (c >= 'A' && c <= 'Z');
c |= 0x20 /* 0b 0010 0000 */; /* (ASCII) make lowercase */ c |= 0b00100000; /* (ASCII) make lowercase */
if((c == 'b' && (io[i].bs = parse(optarg)) > 0)
|| (c == 's' && (io[i].seek = parse(optarg)) >= 0)) if((
break; c == 'b' && (io[i].bs = parse(optarg)) > 0)
|| (c == 's' && (io[i].seek = parse(optarg)) >= 0
)) { break; }
/* FALLTHROUGH */ /* FALLTHROUGH */
default: default:
return usage(program_name); return usage(program_name);
@ -225,8 +238,7 @@ int main(int argc, char *argv[]){
assert(io->fd != STDIN_FILENO || io->fl == read_flags); assert(io->fd != STDIN_FILENO || io->fl == read_flags);
assert(io->fd != STDOUT_FILENO || io->fl == write_flags); assert(io->fd != STDOUT_FILENO || io->fl == write_flags);
if(argc > optind) if (argc > optind) { return usage(program_name); }
return usage(program_name);
for (i = 0; i < (sizeof io) / (sizeof *io); ++i) { for (i = 0; i < (sizeof io) / (sizeof *io); ++i) {
/* buffer allocation */ /* buffer allocation */
@ -235,22 +247,29 @@ int main(int argc, char *argv[]){
program_name, io[i].bs); program_name, io[i].bs);
return EX_OSERR; return EX_OSERR;
} }
/* easy seeking */ /* easy seeking */
if(!fdisstd(io[i].fd) && lseek(io[i].fd, io[i].seek, SEEK_SET) != -1) if (!fdisstd(io[i].fd) && lseek(io[i].fd, io[i].seek, SEEK_SET) != -1) {
io[i].seek = 0; io[i].seek = 0;
} }
}
/* hard seeking */ /* hard seeking */
if (io[1].seek > 0) { if (io[1].seek > 0) {
size_t t; size_t t;
do { do {
memset(io[1].buf, '\0', memset(
(t = io[1].bufuse = MIN(io[1].bs, io[1].seek))); io[1].buf, '\0',
if(Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0) (t = io[1].bufuse = MIN(io[1].bs, io[1].seek))
);
if (Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0) {
Io_write(&io[1]); /* second chance */ Io_write(&io[1]); /* second chance */
if(io[1].error != 0) }
return oserr(io[1].fn, io[1].error); if (io[1].error != 0) { return oserr(io[1].fn, io[1].error); }
} while ((io[1].seek -= (t - io[1].bufuse)) > 0 && io[1].bufuse != t); } while ((io[1].seek -= (t - io[1].bufuse)) > 0 && io[1].bufuse != t);
io[1].bufuse = 0; io[1].bufuse = 0;
} }
@ -259,33 +278,40 @@ int main(int argc, char *argv[]){
return oserr(io[1].fn, errno); return oserr(io[1].fn, errno);
} }
do{ do { /* while(count == 0 || --count > 0); */
assert(io[0].bufuse == 0); assert(io[0].bufuse == 0);
{ /* read */ { /* read */
long skipping; long skipping;
size_t t;
/* hack to intentionally get a partial read from Io_read */ /* hack to intentionally get a partial read from Io_read */
if((skipping = MIN(io[0].seek, io[0].bs)) > 0) if ((skipping = MIN(io[0].seek, io[0].bs)) > 0) {
io[0].bufuse = io[0].bs - (size_t)skipping; io[0].bufuse = io[0].bs - (size_t)skipping;
}
t = io[0].bufuse; size_t t = io[0].bufuse;
if(Io_read(&io[0])->bufuse == t && !noerror && io[0].error == 0) if (Io_read(&io[0])->bufuse == t && !noerror && io[0].error == 0) {
Io_read(&io[0]); /* second chance */ Io_read(&io[0]); /* second chance */
}
assert(io[0].bufuse >= t); assert(io[0].bufuse >= t);
if(io[0].bufuse == t) /* that's all she wrote */
break; if (io[0].bufuse == t) /* that's all she wrote */ { break; }
if (/* t < io[0].bufuse && */ io[0].bufuse < io[0].bs) { if (/* t < io[0].bufuse && */ io[0].bufuse < io[0].bs) {
fprintf(stderr, "%s: Partial read:\n\t", program_name); fprintf(stderr, "%s: Partial read:\n\t", program_name);
fprintio(stderr, fmt, io); fprintio(stderr, fmt, io);
if(!noerror)
count = 1; if (!noerror) { count = 1; }
if (align >= 0) { if (align >= 0) {
/* fill the rest of the ibuf with padding */ /* fill the rest of the ibuf with padding */
memset(&(io[0].buf)[io[0].bufuse], align, memset(
io[0].bs - io[0].bufuse); &(io[0].buf)[io[0].bufuse],
align,
io[0].bs - io[0].bufuse
);
io->bufuse = io->bs; io->bufuse = io->bs;
} }
} }
@ -299,36 +325,47 @@ int main(int argc, char *argv[]){
} }
/* write */ /* write */
do{ do { /* while(io[0].bufuse > 0); */
int t; int t;
if (io[0].bs <= io[1].bs) { if (io[0].bs <= io[1].bs) {
int n; int n;
/* saturate obuf */ /* saturate obuf */
memcpy(io[1].buf, io[0].buf, memcpy(
(io[1].bufuse = (n = MIN(io[0].bufuse, io[1].bs)))); io[1].buf, io[0].buf,
(io[1].bufuse = (n = MIN(io[0].bufuse, io[1].bs)))
);
/* permute the copied units out of ibuf */ /* permute the copied units out of ibuf */
memmove(io[0].buf, &(io[0].buf)[n], (io[0].bufuse -= n)); memmove(io[0].buf, &(io[0].buf)[n], (io[0].bufuse -= n));
} else /* if(io[0].bs < io[1].bs) */ { } else /* if(io[0].bs < io[1].bs) */ {
int n; int n;
/* drain what we can from ibuf */ /* drain what we can from ibuf */
memcpy(&(io[1].buf)[io[1].bufuse], io[0].buf, memcpy(
(n = MIN(io[0].bufuse, io[1].bs - io[1].bufuse))); &(io[1].buf)[io[1].bufuse], io[0].buf,
(n = MIN(io[0].bufuse, io[1].bs - io[1].bufuse))
);
io[1].bufuse += n; io[1].bufuse += n;
/* permute out the copied units */ /* permute out the copied units */
memmove(io[0].buf, &(io[0].buf)[n], io[0].bs - n); memmove(io[0].buf, &(io[0].buf)[n], io[0].bs - n);
io[0].bufuse -= n; io[0].bufuse -= n;
if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1) if(io[0].bs + io[1].bufuse <= io[1].bs && count != 1) {
continue; /* obuf not saturated - we could write more */ continue; /* obuf not saturated - we could write more */
} }
}
t = io[1].bufuse; t = io[1].bufuse;
if(Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0) if (Io_write(&io[1])->bufuse == t && !noerror && io[1].error == 0) {
Io_write(&io[1]); /* second chance */ Io_write(&io[1]); /* second chance */
}
assert(io[1].bufuse <= t); assert(io[1].bufuse <= t);
if (io[1].bufuse == t) { /* no more love */ if (io[1].bufuse == t) { /* no more love */
count = 1; count = 1;
break; break;
@ -337,17 +374,17 @@ int main(int argc, char *argv[]){
if (0 < io[1].bufuse /* && io[1].bufuse < t */) { if (0 < io[1].bufuse /* && io[1].bufuse < t */) {
fprintf(stderr, "%s: Partial write:\n\t", program_name); fprintf(stderr, "%s: Partial write:\n\t", program_name);
fprintio(stderr, fmt, io); fprintio(stderr, fmt, io);
if(!noerror)
count = 1; if(!noerror) { count = 1; }
} }
} while(io[0].bufuse > 0); } while(io[0].bufuse > 0);
} while(count == 0 || --count > 0); } while(count == 0 || --count > 0);
fprintio(stderr, fmt, io); fprintio(stderr, fmt, io);
for(i = 0; i < (sizeof io) / (sizeof *io); ++i) for (i = 0; i < (sizeof io) / (sizeof *io); ++i) {
if(io[i].error) if (io[i].error) { return oserr(io[i].fn, io[i].error); }
return oserr(io[i].fn, io[i].error); }
return EX_OK; return EX_OK;
} }