1#!/bin/sh
2# Exercise chdir-long's sample main program.
3
4# Copyright (C) 2005-2023 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 3 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, see <https://www.gnu.org/licenses/>.
18
19
20# FIXME: add traps and choose top level names so that
21# temporary directories are easier to remove.
22# FIXME: don't clobber a.out
23
24gcc -DTEST_CHDIR -g -O -W -Wall \
25  chdir-long.c libcoreutils.a
26
27vg='valgrind --track-fds=yes --leak-check=yes --quiet --num-callers=9'
28vg="$vg --leak-resolution=high"
29
30# Create a directory with name of the specified length.
31# Caveat: assumes the requested length is longer than that of $TMPDIR or /tmp.
32function mkdir_len
33{
34  local n
35  case $# in 1) n=$1;; *) echo "Usage: $FUNCNAME N" 1>&2; return 1;; esac
36  local root=${TMPDIR=/tmp}
37  test -n "$ROOT" && root=$ROOT
38  ( cd $root &&
39    perl -e 'my $len='$n'-length "'$root'";$i=100;$d="z"x$i;
40                  while ($i+2 < $len) {
41                    $len -= $i + 1;
42                    mkdir $d,0700 or die "$!\n";
43                    chdir $d} $d="z"x($len-1);
44                    mkdir $d or die "mkdir_len: $d: $!\n"' )
45}
46
47size_list=
48pow_2=128
49for i in 7 8 9 10 11 12 13 14 15 16 17; do
50  n=$pow_2
51  nm1=`expr $pow_2 - 1`
52  np1=`expr $pow_2 + 1`
53  size_list="$size_list $nm1 $n $np1"
54  pow_2=`expr $pow_2 \* 2`
55done
56
57for t in . /t /tmp /var/tmp; do
58  test -d $t || continue
59  printf "$t\n"
60  export TMPDIR=$t
61  for i in `echo $size_list 99999 11`; do
62    printf "$t\t$i\n"
63    mkdir_len $i
64    find $TMPDIR/zz*|tail -n1 > in
65    tt=`echo $t|tr / -`
66    # strace -o /t/k-$tt-$i ./a.out < in > out
67    ./a.out < in > out
68    # eval "$vg ./a.out no-pwd < in"
69    rm -rf $TMPDIR/zz*
70    if test "$TMPDIR" = . ; then
71      (pwd|tr -d '\n'; sed 's/^\.//' in) > k; rm -f in; mv k in
72    fi
73    diff -u in out > diff \
74      || { echo FAIL $t:$i; cut -b 1-35 diff; } \
75      && rm -f diff in out
76  done
77done
78
79rm -f a.out
80