Subject: Re: Purging old library versions
To: Paul Goyette <paul@whooppee.com>
From: Simon Burge <simonb@telstra.com.au>
List: current-users
Date: 08/23/1998 11:04:26
On Sat, 22 Aug 1998 17:23:31 -0700 (PDT)  Paul Goyette wrote:

> Having followed -current for a long time now, I notice that I've got a
> few old versions of shared object libraries in /usr/lib.  Is there some
> relatively easy way to find out which executables I might still have
> lying around linked to these older versions of libedit, libkvm (likely
> not very many here), and libstdc++?  I know I could just find all the
> executables and try to run them, but there must be an easier way to
> check which libraries they're linked against?

I got this ages ago (1994!) and haven't used it in quite a while, so I
don't even know if it works on -current (or anything recent).  A quick
glance doesn't show any obvious problems.  Have a look and let us know
how you go.

Simon.
------------------------------ cut here ------------------------------
#!/usr/bin/perl
# hey emacs, this is a -*-perl-*- script!

# A little script to build a reference listing for dynamic libraries.
# This should help you to get rid of unused ones and find the binaries
# you need to update before removing old lib revisions.

# Martin Husemann, martin@euterpe.owl.de, 25-Apr-1994

# Synopsis:
#   libref [-a]
#
# Description:
#   Lists usage count for all known shared libraries
#
# Options:
#   -a   gives a complete reference table

$doRef = $ARGV[0] eq "-a";	# yes, it may use getopt...

# First ask ldconfig for a list of all shared libs and initialize the
# reference and statistics table
open(LIBS, "ldconfig -r |") || die("could not run ldconfig: $!\n");
while(<LIBS>) {
    chop;
    next if (/ld.so.hints:$/);
    s/^.*=> (\S+)\s.*$/\1/o;
    $reference{$_} = "";
    $count{$_} = 0;
}
close(LIBS);

# now lookup every dynamically linked executable on the whole disk
open(FILES, "find / -type f -print |") || die("could not run find: $!\n");
while (<FILES>) {
    chop;
    if ( -x ) {
	$whatis=`file $_`;
	if ($whatis =~ /dynamically linked/) {
	    $prog="$_";
	    open(LIBS, "ldd $prog |") || die("could not run ldd: $!\n");
	    @libs = <LIBS>;
	    close(LIBS);
	    shift(@libs);
	    foreach (@libs) {
		chop;
		s/^.*=> (\S+)\s.*$/\1/o;
		$count{$_}++;
		$reference{$_} .= " $prog" if ($doRef);
	    }
	}
    } 
}
close(FILES);

# sort statistics
sub ByUsage {
    $count{$a} <=> $count{$b};
}
@sorted = sort ByUsage keys(%count);

# and finaly report statistics...
print "Shared library usage counts\n\n" if ($doRef);
foreach (@sorted) {
    printf "%8d  %s\n", $count{$_}, $_;
}

# and the cross reference
if ($doRef) {
    print "\n\nShared library cross reference\n\n";
    foreach $lib (sort(keys(%reference))) {
	print "$lib:$reference{$lib}\n";
    }
}
exit 0;