blob: d7b997ada72371edeac5009405c0d652b0038840 [file] [log] [blame]
Bodo Möller065e1061999-04-28 22:07:28 +00001#!/usr/local/bin/perl
2
Bodo Möller21f213e1999-05-20 01:43:28 +00003# mklink.pl
Bodo Möller065e1061999-04-28 22:07:28 +00004
5# The first command line argument is a non-empty relative path
6# specifying the "from" directory.
Bodo Möllerbf6841f1999-04-28 23:42:28 +00007# Each other argument is a file name not containing / and
8# names a file in the current directory.
Bodo Möller065e1061999-04-28 22:07:28 +00009#
10# For each of these files, we create in the "from" directory a link
11# of the same name pointing to the local file.
12#
Bodo Möllerbf6841f1999-04-28 23:42:28 +000013# 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.
Bodo Möller065e1061999-04-28 22:07:28 +000015# Apart from this, this script should be able to handle even the most
16# pathological cases.
17
18my $from = shift;
19my @files = @ARGV;
20
21my @from_path = split(/\//, $from);
22my $pwd = `pwd`;
23chop($pwd);
24my @pwd_path = split(/\//, $pwd);
25
26my @to_path = ();
Bodo Möller81c8ee01999-04-30 11:32:41 +000027
28my $dirname;
29foreach $dirname (@from_path) {
Bodo Möller065e1061999-04-28 22:07:28 +000030
31 # In this loop, @to_path always is a relative path from
32 # @pwd_path (interpreted is an absolute path) to the original pwd.
33
34 # At the end, @from_path (as a relative path from the original pwd)
35 # designates the same directory as the absolute path @pwd_path,
36 # which means that @to_path then is a path from there to the original pwd.
37
38 next if ($dirname eq "" || $dirname eq ".");
39
40 if ($dirname eq "..") {
41 @to_path = (pop(@pwd_path), @to_path);
42 } else {
43 @to_path = ("..", @to_path);
44 push(@pwd_path, $dirname);
45 }
46}
47
48my $to = join('/', @to_path);
49
Bodo Möller81c8ee01999-04-30 11:32:41 +000050my $file;
51foreach $file (@files) {
Bodo Möller9ef876f2000-06-14 10:09:46 +000052 my $err = "";
53 symlink("$to/$file", "$from/$file") or $err = " [$!]";
54 print $file . " => $from/$file$err\n";
Bodo Möller065e1061999-04-28 22:07:28 +000055}