xref: /openbsd-src/gnu/usr.bin/perl/install_lib.pl (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1#!perl
2
3# Initialisation code and subroutines shared between installperl and installman
4# Probably installhtml needs to join the club.
5
6use strict;
7use vars qw($Is_VMS $Is_W32 $Is_OS2 $Is_Cygwin $Is_Darwin $Is_NetWare $Is_AmigaOS
8	    %opts $packlist);
9use subs qw(unlink link chmod);
10require File::Path;
11require File::Copy;
12
13BEGIN {
14    require Config;
15    if ($Config::Config{userelocatableinc}) {
16	# This might be a considered a hack. Need to get information about the
17	# configuration from Config.pm *before* Config.pm expands any .../
18	# prefixes.
19	#
20	# So we set $^X to pretend that we're the already installed perl, so
21	# Config.pm does its ... expansion off that location.
22
23        my $location = $Config::Config{initialinstalllocation};
24	die <<'OS' unless defined $location;
25$Config{initialinstalllocation} is not defined - can't install a relocatable
26perl without this.
27OS
28	$^X = "$location/perl";
29	# And then remove all trace of ever having loaded Config.pm, so that
30	# it will reload with the revised $^X
31	undef %Config::;
32	delete $INC{"Config.pm"};
33	delete $INC{"Config_heavy.pl"};
34	delete $INC{"Config_git.pl"};
35	# You never saw us. We weren't here.
36
37	require Config;
38    }
39    Config->import;
40}
41
42if ($Config{d_umask}) {
43    umask(022); # umasks like 077 aren't that useful for installations
44}
45
46$Is_VMS = $^O eq 'VMS';
47$Is_W32 = $^O eq 'MSWin32';
48$Is_OS2 = $^O eq 'os2';
49$Is_Cygwin = $^O eq 'cygwin';
50$Is_Darwin = $^O eq 'darwin';
51$Is_NetWare = $Config{osname} eq 'NetWare';
52$Is_AmigaOS = $^O eq 'amigaos';
53
54sub unlink {
55    my(@names) = @_;
56    my($cnt) = 0;
57
58    return scalar(@names) if $Is_VMS;
59
60    foreach my $name (@names) {
61	next unless -e $name;
62	chmod 0777, $name if ($Is_OS2 || $Is_W32 || $Is_Cygwin || $Is_NetWare || $Is_AmigaOS);
63	print "  unlink $name\n" if $opts{verbose};
64	( CORE::unlink($name) and ++$cnt
65	  or warn "Couldn't unlink $name: $!\n" ) unless $opts{notify};
66    }
67    return $cnt;
68}
69
70sub link {
71    my($from,$to) = @_;
72    my($success) = 0;
73
74    my $xfrom = $from;
75    $xfrom =~ s/^\Q$opts{destdir}\E// if $opts{destdir};
76    my $xto = $to;
77    $xto =~ s/^\Q$opts{destdir}\E// if $opts{destdir};
78    print $opts{verbose} ? "  ln $xfrom $xto\n" : "  $xto\n"
79	unless $opts{silent};
80    my $link = $Is_AmigaOS ? \&CORE::symlink : \&CORE::link;
81    eval {
82      $link->($from, $to)
83        ? $success++
84          : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
85            ? die "AFS"  # okay inside eval {}
86              : die "Couldn't link $from to $to: $!\n"
87                unless $opts{notify};
88      $packlist->{$xto} = { from => $xfrom, type => 'link' };
89     };
90    if ($@) {
91	warn "Replacing link() with File::Copy::copy(): $@";
92	print $opts{verbose} ? "  cp $from $xto\n" : "  $xto\n"
93	    unless $opts{silent};
94	print "  creating new version of $xto\n"
95		 if $Is_VMS and -e $to and !$opts{silent};
96	unless ($opts{notify} or File::Copy::copy($from, $to) and ++$success) {
97	    # Might have been that F::C::c can't overwrite the target
98	    warn "Couldn't copy $from to $to: $!\n"
99		unless -f $to and (chmod(0666, $to), unlink $to)
100			and File::Copy::copy($from, $to) and ++$success;
101	}
102	$packlist->{$xto} = { type => 'file' };
103    }
104    $success;
105}
106
107sub chmod {
108    my($mode,$name) = @_;
109
110    return if ($^O eq 'dos');
111    printf "  chmod %o %s\n", $mode, $name if $opts{verbose};
112    CORE::chmod($mode,$name)
113	|| warn sprintf("Couldn't chmod %o %s: $!\n", $mode, $name)
114      unless $opts{notify};
115}
116
117sub samepath {
118    my($p1, $p2) = @_;
119
120    return (lc($p1) eq lc($p2)) if ($Is_W32 || $Is_NetWare);
121
122    return 1
123        if $p1 eq $p2;
124
125    my ($dev1, $ino1) = stat $p1;
126    return 0
127        unless defined $dev1;
128    my ($dev2, $ino2) = stat $p2;
129
130    return $dev1 == $dev2 && $ino1 == $ino2;
131}
132
133sub safe_rename {
134    my($from,$to) = @_;
135    if (-f $to and not unlink($to)) {
136        my($i);
137        for ($i = 1; $i < 50; $i++) {
138            last if rename($to, "$to.$i");
139        }
140        warn("Cannot rename to '$to.$i': $!"), return 0
141           if $i >= 50; # Give up!
142    }
143    link($from,$to) || return 0;
144    unlink($from);
145}
146
147sub mkpath {
148    File::Path::mkpath(shift , $opts{verbose}, 0777) unless $opts{notify};
149}
150
151sub unixtoamiga
152{
153	my $unixpath = shift;
154
155	my @parts = split("/",$unixpath);
156	my $isdir = 0;
157	$isdir = 1 if substr($unixpath,-1) eq "/";
158
159	my $first = 1;
160	my $amigapath = "";
161
162	my $i = 0;
163
164	for($i = 0; $i <= $#parts;$i++)
165	{
166		next if $parts[$i] eq ".";
167		if($parts[$i] eq "..")
168		{
169			$parts[$i] = "/";
170		}
171		if($i == 0)
172		{
173			if($parts[$i] eq "")
174			{
175				$amigapath .= $parts[$i + 1] . ":";
176				$i++;
177				next;
178			}
179		}
180		$amigapath .= $parts[$i];
181		if($i != $#parts)
182		{
183			$amigapath .= "/" unless $parts[$i] eq "/" ;
184		}
185		else
186		{
187			if($isdir)
188			{
189				$amigapath .= "/" unless $parts[$i] eq "/" ;
190			}
191		}
192	}
193
194	return $amigapath;
195}
196
197sub amigaprotect
198{
199	my ($file,$bits) = @_;
200	print "PROTECT: File $file\n";
201	system("PROTECT $file $bits")
202	      unless $opts{notify};
203}
204
2051;
206