dj(1): statistics now track hard seeks

This commit is contained in:
dtb
2024-07-04 21:32:05 -06:00
parent 571796fe0d
commit 6ed7089b25
2 changed files with 18 additions and 29 deletions

View File

@@ -56,7 +56,6 @@ static char *fmt_human = "%d+%d > %d+%d; %d > %d\n";
static char *stdin_name = "<stdin>";
static char *stdout_name = "<stdout>";
static int creat_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH
| S_IWOTH; /* Consistent with touch(1p). */
static int read_flags = O_RDONLY; /* Consistent with Busybox dd(1). */
@@ -239,37 +238,29 @@ int main(int argc, char *argv[]){
/* hard seeking */
if(io[1].seek > 0){
memset(io[1].buf, '\0', io[1].bs);
/* We're going to cheat and use bufuse as the retval for write(2),
* which is fine because it'll be zeroed as this function returns
* anyway. */
size_t t;
do{
if((io[1].bufuse = write(
io[1].fd, io[1].buf, MIN(io[1].bs, io[1].seek)))
== 0)
/* second chance */
io[1].bufuse = write(
io[1].fd, io[1].buf, MIN(io[1].bs, io[1].seek));
}while((io[1].seek -= io[1].bufuse) > 0 && io[1].bufuse != 0);
memset(io[1].buf, '\0',
(t = io[1].bufuse = MIN(io[1].bs, io[1].seek)));
if(Io_write(&io[1])->bufuse == t && !noerror)
Io_write(&io[1]); /* second chance */
}while((io[1].seek -= (t - io[1].bufuse)) > 0 && io[1].bufuse != t);
io[1].bufuse = 0;
}
/* Sought bytes aren't counted in the statistics because successful seeking
* is guaranteed here. */
if(io[1].seek > 0)
if(io[1].seek > 0){
fprintio(stderr, fmt, io);
return oserr(io[1].fn);
}
do{
assert(io[0].bufuse == 0);
{ /* read */
static char skipping = 0;
char skipping;
int t;
if(io[0].seek > 0)
skipping = 1;
if(skipping && io[0].seek < io[0].bs)
if((skipping = (io[0].seek > 0)) && io[0].seek < io[0].bs)
io[0].bufuse = io[0].bs - io[0].seek;
t = io[0].bufuse;
@@ -279,11 +270,13 @@ int main(int argc, char *argv[]){
break;
if(/* t < io[0].bufuse && */ io[0].bufuse < io[0].bs){
assert(!skipping);
fprintf(stderr, "%s: Partial read:\n\t", program_name);
fprintio(stderr, fmt, io);
if(!noerror)
count = 1;
if(!skipping && align >= 0){
if(align >= 0){
/* fill the rest of the ibuf with padding */
memset(&(io[0].buf)[io[0].bufuse], align,
io[0].bs - io[0].bufuse);
@@ -292,7 +285,6 @@ int main(int argc, char *argv[]){
}
if(skipping){
skipping = (io[0].seek -= io[0].bufuse - t) > 0;
io[0].bufuse = 0;
count += (count != 0);
continue;