about summary refs log tree commit diff
path: root/holes.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2017-11-08 18:45:52 +0100
committerLeah Neukirchen <leah@vuxu.org>2017-11-08 18:46:13 +0100
commit9d3830b7ea771ae45d9c1e7b87ae58c813d2aac9 (patch)
tree02a3601d1ce6352ef6899a1f6fe6d7bd60fec1b7 /holes.c
parent55db426fcfb2d3f404a9e8465930e3bf8e296a3c (diff)
downloadholes-9d3830b7ea771ae45d9c1e7b87ae58c813d2aac9.tar.gz
holes-9d3830b7ea771ae45d9c1e7b87ae58c813d2aac9.tar.xz
holes-9d3830b7ea771ae45d9c1e7b87ae58c813d2aac9.zip
add -s for summary
Diffstat (limited to 'holes.c')
-rw-r--r--holes.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/holes.c b/holes.c
index 78a90af..7cfe08e 100644
--- a/holes.c
+++ b/holes.c
@@ -10,6 +10,7 @@ ssize_t minlen = 64;
 char byte = 0;
 int ret = 0;
 char *argv0;
+ssize_t total, totalz;
 
 void
 holes(FILE *input, char *filename)
@@ -42,6 +43,7 @@ holes(FILE *input, char *filename)
 					if (filename)
 						printf("%s: ", filename);
 					printf("%08lx %ld\n", offset - run, run);
+					totalz += run;
 				}
 				run = 0;
 			}
@@ -60,7 +62,10 @@ holes(FILE *input, char *filename)
 		if (filename)
 			printf("%s: ", filename);
 		printf("%08lx %ld\n", offset - run, run);
+		totalz += run;
 	}
+
+	total += offset;
 }
 
 int
@@ -69,10 +74,11 @@ main(int argc, char *argv[])
 	int c, i;
 	long b;
 	char *e;
+	int sflag = 0;
 
 	argv0 = argv[0];
 
-	while ((c = getopt(argc, argv, "b:n:")) != -1)
+	while ((c = getopt(argc, argv, "b:n:s")) != -1)
 		switch (c) {
 		case 'b':
 			errno = 0;
@@ -101,9 +107,10 @@ main(int argc, char *argv[])
 				exit(2);
 			}
 			break;
+		case 's': sflag++; break;
 		default:
 			fprintf(stderr,
-			    "Usage: %s [-b BYTE] [-n MINLEN] [FILES...]\n",
+			    "Usage: %s [-b BYTE] [-n MINLEN] [-s] [FILES...]\n",
 			    argv0);
 			exit(2);
 		}
@@ -124,5 +131,10 @@ main(int argc, char *argv[])
 			}
 		}
 
+	if (sflag)
+		printf("total %jd/%jd (%.2f%%)\n",
+		    totalz, total,
+		    total == 0 ? 0.0 : 100.0*totalz / total);
+
 	return ret;
 }