Compare commits

..

No commits in common. "aff658d611e7b221a58b3177f34c7bd5c323d725" and "1cf67af28179d46b6da387afb691944cc5637d85" have entirely different histories.

View File

@ -47,7 +47,7 @@ struct Io{
long seek; /* bytes to seek/skip (will be 0 after skippage) (-sS) */ long seek; /* bytes to seek/skip (will be 0 after skippage) (-sS) */
}; };
/* To be assigned to main:fmt and used with printio(). */ /* To be assigned to main:fmt_output and used with output(). */
static char *fmt_asv = "%d\037%d\036%d\037%d\035%d\036%d\034"; static char *fmt_asv = "%d\037%d\036%d\037%d\035%d\036%d\034";
static char *fmt_human = "%d+%d > %d+%d; %d > %d\n"; static char *fmt_human = "%d+%d > %d+%d; %d > %d\n";
@ -65,6 +65,15 @@ static int write_flags = O_WRONLY | O_CREAT;
|| (fd) == STDOUT_FILENO \ || (fd) == STDOUT_FILENO \
|| (fd) == STDERR_FILENO) || (fd) == STDERR_FILENO)
/* Allocates *io's buffer. Returns NULL if unsuccessful. */
static void *
Io_bufalloc(struct Io *io){
return io != NULL
? (io->buf = malloc(io->bs * (sizeof *io->buf)))
: NULL;
}
/* Fills the unused portion of io's buffer with padding, updating io->bufuse. /* Fills the unused portion of io's buffer with padding, updating io->bufuse.
* Returns io. */ * Returns io. */
static struct Io * static struct Io *
@ -142,6 +151,8 @@ Io_fdseek(struct Io *io){
assert(io->fd != STDOUT_FILENO || io->fl == write_flags); assert(io->fd != STDOUT_FILENO || io->fl == write_flags);
assert(io->fd != STDERR_FILENO || io->fl == write_flags); assert(io->fd != STDERR_FILENO || io->fl == write_flags);
printf("%s\n", io->fn);
if(io->seek == 0 if(io->seek == 0
|| (!fdisstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) != -1)) || (!fdisstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) != -1))
return; return;
@ -213,7 +224,7 @@ oserr(char *s){
/* 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
printio(char *fmt, struct Io io[2]){ output(struct Io io[2], char *fmt){
fprintf(stderr, fmt, fprintf(stderr, fmt,
io[0].rec, io[0].prec, io[1].rec, io[1].prec, io[0].rec, io[0].prec, io[1].rec, io[1].prec,
@ -250,7 +261,7 @@ usage(char *s){
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
int align; /* low 8b used, negative if no alignment is being done */ int align; /* low 8b used, negative if no alignment is being done */
int count; /* 0 if dj(1) runs until no more reads are possible */ int count; /* 0 if dj(1) runs until no more reads are possible */
char *fmt; /* == fmt_asv (default) or fmt_human (-H) */ char *fmt_output; /* == fmt_asv (default) or fmt_human (-H) */
size_t i; /* side of io being modified */ size_t i; /* side of io being modified */
struct Io io[2]; struct Io io[2];
char noerror; /* 0=exits (default) 1=retries on partial reads or writes */ char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
@ -258,7 +269,7 @@ int main(int argc, char *argv[]){
/* Set defaults. */ /* Set defaults. */
align = -1; align = -1;
count = 0; count = 0;
fmt = fmt_asv; fmt_output = fmt_asv;
noerror = 0; noerror = 0;
for(i = 0; i < 2; ++i){ for(i = 0; i < 2; ++i){
io[i].bs = 1024 /* 1 KiB */; /* GNU dd(1) default; POSIX says 512B */ io[i].bs = 1024 /* 1 KiB */; /* GNU dd(1) default; POSIX says 512B */
@ -286,7 +297,7 @@ int main(int argc, char *argv[]){
break; break;
return oserr(optarg); return oserr(optarg);
case 'n': noerror = 1; break; case 'n': noerror = 1; break;
case 'H': fmt = fmt_human; break; case 'H': fmt_output = fmt_human; break;
case 'a': case 'a':
if(optarg[0] == '\0' || optarg[1] == '\0'){ if(optarg[0] == '\0' || optarg[1] == '\0'){
align = optarg[0]; align = optarg[0];
@ -296,7 +307,7 @@ int main(int argc, char *argv[]){
case 'c': case 'b': case 's': case 'B': case 'S': /* numbers */ case 'c': case 'b': case 's': case 'B': case 'S': /* numbers */
if(c == 'c' && (count = parse(optarg)) >= 0) if(c == 'c' && (count = parse(optarg)) >= 0)
break; break;
i = (c >= 'A' && c <= 'Z'); i = (c >= 'A' && c <= 'Z'); /* uppercase changes output */
c |= 0x20 /* 0b 0010 0000 */; /* (ASCII) make lowercase */ c |= 0x20 /* 0b 0010 0000 */; /* (ASCII) make lowercase */
if((c == 'b' && (io[i].bs = parse(optarg)) > 0) if((c == 'b' && (io[i].bs = parse(optarg)) > 0)
|| (c == 's' && (io[i].seek = parse(optarg)) >= 0)) || (c == 's' && (io[i].seek = parse(optarg)) >= 0))
@ -313,7 +324,7 @@ int main(int argc, char *argv[]){
} }
for(i = 0; i < 2; ++i){ for(i = 0; i < 2; ++i){
if((io[i].buf = malloc(io[i].bs * (sizeof *(io[i].buf)))) == NULL){ if(Io_bufalloc(&io[i]) == NULL){
fprintf(stderr, "%s: Failed to allocate %d bytes\n", fprintf(stderr, "%s: Failed to allocate %d bytes\n",
program_name, io[i].bs); program_name, io[i].bs);
return EX_OSERR; return EX_OSERR;
@ -334,7 +345,7 @@ int main(int argc, char *argv[]){
else if(io[0].bufuse < io[0].bs){ else if(io[0].bufuse < io[0].bs){
++io[0].prec; ++io[0].prec;
fprintf(stderr, "%s: Partial read:\n\t", program_name); fprintf(stderr, "%s: Partial read:\n\t", program_name);
printio(fmt, io); output(io, fmt_output);
if(!noerror) if(!noerror)
count = 1; count = 1;
if(align >= 0) if(align >= 0)
@ -363,7 +374,7 @@ int main(int argc, char *argv[]){
}else if(t > io[1].bufuse && io[1].bufuse > 0){ }else if(t > io[1].bufuse && io[1].bufuse > 0){
io[1].prec += 1; io[1].prec += 1;
fprintf(stderr, "%s: Partial write:\n\t", program_name); fprintf(stderr, "%s: Partial write:\n\t", program_name);
printio(fmt, io); output(io, fmt_output);
if(!noerror) if(!noerror)
count = 1; count = 1;
}else if(io[1].bufuse == 0 && t < io[1].bs) }else if(io[1].bufuse == 0 && t < io[1].bs)
@ -373,7 +384,7 @@ int main(int argc, char *argv[]){
}while(io[0].bufuse > 0); }while(io[0].bufuse > 0);
}while(count == 0 || --count > 0); }while(count == 0 || --count > 0);
printio(fmt, io); output(io, fmt_output);
return EX_OK; return EX_OK;
} }