bef27594a4c7dc3d7eb1ee1116a8359dd7f9d19f
[p5sagit/p5-mst-13.2.git] / Porting / findrfuncs
1 #!/usr/bin/perl -w
2
3 #
4 # findrfuncs: find reentrant variants of functions used in an executable.
5 # Requires a functional "nm -u".  Searches headers in /usr/include
6 # to find available *_r functions and looks for non-reentrant
7 # variants used in the supplied executable.
8 #
9 # Gurusamy Sarathy
10 # gsar@ActiveState.com
11 #
12 # Hacked to automatically find the executable and shared objects.
13 # --jhi
14
15 use strict;
16 use File::Find;
17
18 my @EXES;
19 my $NMU = 'nm -u';
20 my @INCDIRS = qw(/usr/include);
21 my $SO = 'so';
22 my $EXE = '';
23
24 if (open(CONFIG, "config.sh")) {
25     local $/;
26     my $CONFIG = <CONFIG>;
27     $SO  = $1 if $CONFIG =~ /^so='(\w+)'/m;
28     $EXE = $1 if $CONFIG =~ /^_exe='\.(\w+)'/m;
29     close(CONFIG);
30 }
31
32 push @EXES, "perl$EXE";
33
34 find(sub {push @EXES, $File::Find::name if /\.$SO$/}, '.' );
35
36 push @EXES, @ARGV;
37
38 if ($^O eq 'dec_osf') {
39     $NMU = 'nm -Bu';
40 } elsif ($^O eq 'irix') {
41     $NMU = 'nm -pu';
42 }
43
44 my %rfuncs;
45 my @syms;
46 find(sub {
47         return unless -f $File::Find::name;
48         local *F;
49         open F, "<$File::Find::name"
50             or die "Can't open $File::Find::name: $!";
51         my $line;
52         while (defined ($line = <F>)) {
53             if ($line =~ /\b(\w+_r)\b/) {
54                 #warn "$1 => $File::Find::name\n";
55                 $rfuncs{$1}->{$File::Find::name}++;
56             }
57         }
58         close F;
59      }, @INCDIRS);
60
61 # delete bogus symbols grepped out of comments and such
62 delete $rfuncs{setlocale_r} if $^O eq 'linux';
63
64 # delete obsolete (as promised by man pages) symbols
65 my $netdb_r_obsolete;
66 if ($^O eq 'hpux') {
67     delete $rfuncs{crypt_r};
68     delete $rfuncs{setlocale_r};
69     delete $rfuncs{strerror_r};
70     $netdb_r_obsolete = 1;
71 } elsif ($^O eq 'dec_osf') {
72     delete $rfuncs{crypt_r};
73     delete $rfuncs{strerror_r};
74     $netdb_r_obsolete = 1;
75 }
76 if ($netdb_r_obsolete) {
77     delete @rfuncs{qw(endhostent_r
78                       endnetent_r
79                       endprotoent_r
80                       endservent_r
81                       gethostbyaddr_r
82                       gethostbyname_r
83                       gethostent_r
84                       getnetbyaddr_r
85                       getnetbyname_r
86                       getnetent_r
87                       getprotobynumber_r
88                       getprotobyname_r
89                       getprotoent_r
90                       getservbyname_r
91                       getservbyport_r
92                       getservent_r
93                       sethostent_r
94                       setnetent_r
95                       setprotoent_r
96                       setservent_r)};
97 }
98
99 my %syms;
100
101 for my $exe (@EXES) {
102     # warn "#--- $exe\n";
103     for my $sym (`$NMU $exe`) {
104         chomp $sym;
105         $sym =~ s/^\s+//;
106         $sym =~ s/^([0-9A-Fa-f]+\s+)?[Uu]\s+//;
107         $sym =~ s/\s+[Uu]\s+-$//;
108         next if /\s/;
109         $sym =~ s/\@.*\z//;     # remove @@GLIBC_2.0 etc
110         # warn "#### $sym\n";
111         if (exists $rfuncs{"${sym}_r"} && ! $syms{"$sym:$exe"}++) {
112             push @syms, $sym;
113         }
114     }
115     
116     if (@syms) {
117         print "\nFollowing symbols in $exe have reentrant versions:\n";
118         for my $sym (@syms) {
119             my @f = sort keys %{$rfuncs{$sym . '_r'}};
120             print "$sym => $sym" . "_r (@f)\n";
121         }
122     }
123     @syms = ();
124 }
125