#!/usr/bin/perl
#
# StringWaste [opt] file... >doc/waste
#
# This little script calculates the amount of wasted space in an executable due to
# multiple copies of strings.  It feeds each command-line arg to the strings
# command, and sorts the string list by numer of repetitions.  For each string
# that occurs more than once, the wasted space is calculated as (n-1)*strlen(s).

for $f (@ARGV) {
	if ($f eq '-p') {
		$prnt = 1;
		next;
	}
	open(S,"strings $f | sort | uniq -c | sort -rn |")
		|| die "### Can't run  \"strings $f | sort | uniq -c | sort -rn\"";
	$wastf = 0;
S:	for $l (<S>) {
		chop;
		if ($l =~ /\s*(\d+)\s(.*)/) {
			last S if (($n = $1 - 1) < 1);
			$l = length($s = $2);
			$w = $n * $l;	# Waste in this string.
			$wastf += $w;	# Waste in this file.
			$waste += $w;	# Waste in all files.
			printf("%5d (%d*%d) bytes wasted by \"%s\"\n", $n, $l, $w, $s)
				if $prnt;
		}
	}
	printf("%8d bytes wasted in %s.\n", $wastf, $f);
}
printf("%8d bytes wasted total.\n", $waste);

