Tizen 2.0 Release
[external/tizen-coreutils.git] / tests / misc / pwd-long
1 #!/bin/sh
2 # Ensure that pwd works even when run from a very deep directory.
3
4 # Copyright (C) 2006-2007 Free Software Foundation, Inc.
5
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.
10
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.
15
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
19 # 02110-1301, USA.
20
21 : ${PERL=perl}
22
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"
26   exit 77
27 }
28
29 framework_failure=0
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
34
35 mkdir -p $tmp || framework_failure=1
36 cd $tmp || framework_failure=1
37
38 if test $framework_failure = 1; then
39   echo "$0: failure in testing framework" 1>&2
40   (exit 1); exit 1
41 fi
42
43 ARGV_0=$0
44 export ARGV_0
45
46 CWD=$pwd/$tmp
47 export CWD
48
49 $PERL -Tw -- - <<\EOF
50
51 # Show that pwd works even when the length of the resulting
52 # directory name is longer than PATH_MAX.
53 use strict;
54
55 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
56
57 sub normalize_to_cwd_relative ($$$)
58 {
59   my ($dir, $dev, $ino) = @_;
60   my $slash = -1;
61   my $next_slash;
62   while (1)
63     {
64       $slash = index $dir, '/', $slash + 1;
65       $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;
71     }
72 }
73
74 # Set up a safe, well-known environment
75 delete @ENV{qw(BASH_ENV CDPATH ENV PATH)};
76 $ENV{IFS}  = '';
77
78 # Save CWD's device and inode numbers.
79 my ($dev, $ino) = (stat '.')[0, 1];
80
81 # Construct the expected "."-relative part of pwd's output.
82 my $z = 'z' x 31;
83 my $n = 256;
84 my $expected = "/$z" x $n;
85 # Remove the leading "/".
86 substr ($expected, 0, 1) = '';
87
88 my $i = 0;
89 do
90   {
91     mkdir $z, 0700
92       or die "$ME: at depth $i: $!\n";
93     chdir $z;
94   }
95 until (++$i == $n);
96
97 my $build_src_dir = $ENV{BUILD_SRC_DIR};
98 $build_src_dir
99   or die "$ME: envvar BUILD_SRC_DIR not defined\n";
100 if ($build_src_dir !~ m!^([-+.:/\w]+)$!)
101   {
102     warn "$0: skipping this test; odd build source directory name:\n"
103       . "$build_src_dir\n";
104     exit 77;
105   }
106 $build_src_dir = $1;
107
108 my $pwd_binary = "$build_src_dir/pwd";
109
110 -x $pwd_binary
111   or die "$ME: $pwd_binary is not an executable file\n";
112 chomp (my $actual = `$pwd_binary`);
113
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;
121
122 if ($expected ne $actual)
123   {
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";
130     exit 1;
131   }
132 EOF