2 # Ensure that pwd works even when run from a very deep directory.
4 # Copyright (C) 2006-2007 Free Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 $PERL -e 1 > /dev/null 2>&1 || {
24 echo 1>&2 "$0: configure didn't find a usable version of Perl," \
25 "so can't run this test"
30 pwd=`"${BUILD_SRC_DIR?}"/pwd` || framework_failure=1
31 t0=`echo "$0"|sed 's,.*/,,'`.tmp; tmp=$t0/$$
32 trap 'status=$?; cd "$pwd" && chmod -R u+rwx $t0 && rm -rf $t0 && exit $status' 0
33 trap '(exit $?); exit $?' 1 2 13 15
35 mkdir -p $tmp || framework_failure=1
36 cd $tmp || framework_failure=1
38 if test $framework_failure = 1; then
39 echo "$0: failure in testing framework" 1>&2
51 # Show that pwd works even when the length of the resulting
52 # directory name is longer than PATH_MAX.
55 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
57 sub normalize_to_cwd_relative ($$$)
59 my ($dir, $dev, $ino) = @_;
64 $slash = index $dir, '/', $slash + 1;
66 and die "$ME: $dir does not contain old CWD\n";
67 my $dir_prefix = $slash ? substr ($dir, 0, $slash) : '/';
68 my ($d, $i) = (stat $dir_prefix)[0, 1];
69 $d == $dev && $i == $ino
70 and return substr $dir, $slash + 1;
74 # Set up a safe, well-known environment
75 delete @ENV{qw(BASH_ENV CDPATH ENV PATH)};
78 # Save CWD's device and inode numbers.
79 my ($dev, $ino) = (stat '.')[0, 1];
81 # Construct the expected "."-relative part of pwd's output.
84 my $expected = "/$z" x $n;
85 # Remove the leading "/".
86 substr ($expected, 0, 1) = '';
92 or die "$ME: at depth $i: $!\n";
97 my $build_src_dir = $ENV{BUILD_SRC_DIR};
99 or die "$ME: envvar BUILD_SRC_DIR not defined\n";
100 if ($build_src_dir !~ m!^([-+.:/\w]+)$!)
102 warn "$0: skipping this test; odd build source directory name:\n"
103 . "$build_src_dir\n";
108 my $pwd_binary = "$build_src_dir/pwd";
111 or die "$ME: $pwd_binary is not an executable file\n";
112 chomp (my $actual = `$pwd_binary`);
114 # Convert the absolute name from pwd into a $CWD-relative name.
115 # This is necessary in order to avoid a spurious failure when run
116 # from a directory in a bind-mounted partition. What happens is
117 # pwd reads a ".." that contains two or more entries with identical
118 # dev,ino that match the ones we're looking for, and it chooses a
119 # name that does not correspond to the one already recorded in $CWD.
120 $actual = normalize_to_cwd_relative $actual, $dev, $ino;
122 if ($expected ne $actual)
124 my $e_len = length $expected;
125 my $a_len = length $actual;
126 warn "expected len: $e_len\n";
127 warn "actual len: $a_len\n";
128 warn "expected: $expected\n";
129 warn "actual: $actual\n";