K2.6 patches and update.
[tomato.git] / release / src / router / openssl / util / mklink.pl
blob61db12c68f3156475f4f44e2dde2e01d8be001ee
1 #!/usr/local/bin/perl
3 # mklink.pl
5 # The first command line argument is a non-empty relative path
6 # specifying the "from" directory.
7 # Each other argument is a file name not containing / and
8 # names a file in the current directory.
10 # For each of these files, we create in the "from" directory a link
11 # of the same name pointing to the local file.
13 # We assume that the directory structure is a tree, i.e. that it does
14 # not contain symbolic links and that the parent of / is never referenced.
15 # Apart from this, this script should be able to handle even the most
16 # pathological cases.
18 use Cwd;
20 my $from = shift;
21 my @files = @ARGV;
23 my @from_path = split(/[\\\/]/, $from);
24 my $pwd = getcwd();
25 chomp($pwd);
26 my @pwd_path = split(/[\\\/]/, $pwd);
28 my @to_path = ();
30 my $dirname;
31 foreach $dirname (@from_path) {
33 # In this loop, @to_path always is a relative path from
34 # @pwd_path (interpreted is an absolute path) to the original pwd.
36 # At the end, @from_path (as a relative path from the original pwd)
37 # designates the same directory as the absolute path @pwd_path,
38 # which means that @to_path then is a path from there to the original pwd.
40 next if ($dirname eq "" || $dirname eq ".");
42 if ($dirname eq "..") {
43 @to_path = (pop(@pwd_path), @to_path);
44 } else {
45 @to_path = ("..", @to_path);
46 push(@pwd_path, $dirname);
50 my $to = join('/', @to_path);
52 my $file;
53 $symlink_exists=eval {symlink("",""); 1};
54 if ($^O eq "msys") { $symlink_exists=0 };
55 foreach $file (@files) {
56 my $err = "";
57 if ($symlink_exists) {
58 unlink "$from/$file";
59 symlink("$to/$file", "$from/$file") or $err = " [$!]";
60 } else {
61 unlink "$from/$file";
62 open (OLD, "<$file") or die "Can't open $file: $!";
63 open (NEW, ">$from/$file") or die "Can't open $from/$file: $!";
64 binmode(OLD);
65 binmode(NEW);
66 while (<OLD>) {
67 print NEW $_;
69 close (OLD) or die "Can't close $file: $!";
70 close (NEW) or die "Can't close $from/$file: $!";
72 print $file . " => $from/$file$err\n";