about summary refs log tree commit diff
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
parent55db426fcfb2d3f404a9e8465930e3bf8e296a3c (diff)
downloadholes-9d3830b7ea771ae45d9c1e7b87ae58c813d2aac9.tar.gz
holes-9d3830b7ea771ae45d9c1e7b87ae58c813d2aac9.tar.xz
holes-9d3830b7ea771ae45d9c1e7b87ae58c813d2aac9.zip
add -s for summary
-rw-r--r--README7
-rw-r--r--holes.15
-rw-r--r--holes.c16
3 files changed, 23 insertions, 5 deletions
diff --git a/README b/README
index 91f6cca..3edaccd 100644
--- a/README
+++ b/README
@@ -4,7 +4,7 @@ NAME
      holes – find runs of zero bytes
 
 SYNOPSIS
-     holes [-b byte] [-n minlen] [files ...]
+     holes [-b byte] [-n minlen] [-s] [files ...]
 
 DESCRIPTION
      holes looks for runs of zero bytes (a.k.a. holes) in the specified input
@@ -22,6 +22,9 @@ DESCRIPTION
              Change minimum size of holes reported.  By default, only holes of
              at least 64 bytes are reported.
 
+     -s      Print a summary at the end how many percent of the file are runs
+             of zeroes.
+
 EXIT STATUS
      The holes utility exits 0 on success, and >0 if an error occurs.
 
@@ -39,4 +42,4 @@ LICENSE
 
            http://creativecommons.org/publicdomain/zero/1.0/
 
-Void Linux                      August 21, 2017                     Void Linux
+Void Linux                     November 8, 2017                     Void Linux
diff --git a/holes.1 b/holes.1
index 2edbaf9..284fe6e 100644
--- a/holes.1
+++ b/holes.1
@@ -1,4 +1,4 @@
-.Dd August 21, 2017
+.Dd November 8, 2017
 .Dt HOLES 1
 .Os
 .Sh NAME
@@ -8,6 +8,7 @@
 .Nm
 .Op Fl b Ar byte
 .Op Fl n Ar minlen
+.Op Fl s
 .Op Ar files\ ...
 .Sh DESCRIPTION
 .Nm
@@ -30,6 +31,8 @@ instead of zero bytes.
 Change minimum size of holes reported.
 By default,
 only holes of at least 64 bytes are reported.
+.It Fl s
+Print a summary at the end how many percent of the file are runs of zeroes.
 .El
 .Sh EXIT STATUS
 .Ex -std
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;
 }