1*4887Schin########################################################################
2*4887Schin#                                                                      #
3*4887Schin#               This software is part of the ast package               #
4*4887Schin#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5*4887Schin#                      and is licensed under the                       #
6*4887Schin#                  Common Public License, Version 1.0                  #
7*4887Schin#                      by AT&T Knowledge Ventures                      #
8*4887Schin#                                                                      #
9*4887Schin#                A copy of the License is available at                 #
10*4887Schin#            http://www.opensource.org/licenses/cpl1.0.txt             #
11*4887Schin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12*4887Schin#                                                                      #
13*4887Schin#              Information and Software Systems Research               #
14*4887Schin#                            AT&T Research                             #
15*4887Schin#                           Florham Park NJ                            #
16*4887Schin#                                                                      #
17*4887Schin#                  David Korn <dgk@research.att.com>                   #
18*4887Schin#                                                                      #
19*4887Schin########################################################################
20*4887Schinfunction err_exit
21*4887Schin{
22*4887Schin	print -u2 -n "\t"
23*4887Schin	print -u2 -r ${Command}[$1]: "${@:2}"
24*4887Schin	let Errors+=1
25*4887Schin}
26*4887Schinalias err_exit='err_exit $LINENO'
27*4887Schin
28*4887SchinCommand=${0##*/}
29*4887Schininteger Errors=0
30*4887Schinmkdir /tmp/ksh$$
31*4887Schincd /tmp/ksh$$
32*4887Schintrap "PATH=$PATH; cd /; rm -rf /tmp/ksh$$" EXIT
33*4887Schin(PATH="/bin")
34*4887Schin[[ $($SHELL -c 'print -r -- "$PATH"') == "$PATH" ]] || err_exit 'export PATH lost in subshell'
35*4887Schincat > bug1 <<- \EOF
36*4887Schin	print print ok > /tmp/ok$$
37*4887Schin	/bin/chmod 755 /tmp/ok$$
38*4887Schin	trap 'cd /; rm -f /tmp/ok$$' EXIT
39*4887Schin	function a
40*4887Schin	{
41*4887Schin	        typeset -x PATH=/tmp
42*4887Schin	        ok$$
43*4887Schin	}
44*4887Schin	path=$PATH
45*4887Schin	unset PATH
46*4887Schin	a
47*4887Schin	PATH=$path
48*4887Schin}
49*4887SchinEOF
50*4887Schin[[ $($SHELL ./bug1  2> /dev/null) == ok ]]  || err_exit "PATH in function not working"
51*4887Schincat > bug1 <<- \EOF
52*4887Schin	function lock_unlock
53*4887Schin	{
54*4887Schin	typeset PATH=/usr/bin
55*4887Schin	typeset -x PATH=''
56*4887Schin	}
57*4887Schin
58*4887Schin	PATH=/usr/bin
59*4887Schin	: $(PATH=/usr/bin getconf PATH)
60*4887Schin	typeset -ft lock_unlock
61*4887Schin	lock_unlock
62*4887SchinEOF
63*4887Schin($SHELL ./bug1)  2> /dev/null || err_exit "path_delete bug"
64*4887Schinmkdir tdir$$
65*4887Schinif	$SHELL tdir$$ > /dev/null 2>&1
66*4887Schinthen	err_exit 'not an error to run ksh on a directory'
67*4887Schinfi
68*4887Schin
69*4887Schinprint 'print hi' > ls
70*4887Schinif	[[ $($SHELL ls 2> /dev/null) != hi ]]
71*4887Schinthen	err_exit "$SHELL name not executing version in current directory"
72*4887Schinfi
73*4887Schinif	[[ $(ls -d . 2>/dev/null) == . && $(PATH=/bin:/usr/bin:$PATH ls -d . 2>/dev/null) != . ]]
74*4887Schinthen	err_exit 'PATH export in command substitution not working'
75*4887Schinfi
76*4887Schinpwd=$PWD
77*4887Schin# get rid of leading and trailing : and trailing :.
78*4887SchinPATH=${PATH%.}
79*4887SchinPATH=${PATH%:}
80*4887SchinPATH=${PATH#.}
81*4887SchinPATH=${PATH#:}
82*4887Schinpath=$PATH
83*4887Schinvar=$(whence date)
84*4887Schindir=$(basename "$var")
85*4887Schinfor i in 1 2 3 4 5 6 7 8 9 0
86*4887Schindo	if	! whence notfound$i 2> /dev/null
87*4887Schin	then	cmd=notfound$i
88*4887Schin		break
89*4887Schin	fi
90*4887Schindone
91*4887Schinprint 'print hello' > date
92*4887Schinchmod +x date
93*4887Schinprint 'print notfound' >  $cmd
94*4887Schinchmod +x "$cmd"
95*4887Schin> foo
96*4887Schinchmod 755 foo
97*4887Schinfor PATH in $path :$path $path: .:$path $path: $path:. $PWD::$path $PWD:.:$path $path:$PWD $path:.:$PWD
98*4887Schindo
99*4887Schin#	print path=$PATH $(whence date)
100*4887Schin#	print path=$PATH $(whence "$cmd")
101*4887Schin		date
102*4887Schin		"$cmd"
103*4887Schindone > /dev/null 2>&1
104*4887Schinbuiltin -d date 2> /dev/null
105*4887Schinif	[[ $(PATH=:/usr/bin; date) != 'hello' ]]
106*4887Schinthen	err_exit "leading : in path not working"
107*4887Schinfi
108*4887Schin(
109*4887Schin	PATH=$PWD:
110*4887Schin	builtin chmod
111*4887Schin	print 'print cannot execute' > noexec
112*4887Schin	chmod 644 noexec
113*4887Schin	if	[[ ! -x noexec ]]
114*4887Schin	then	noexec > /dev/null 2>&1
115*4887Schin	else	exit 126
116*4887Schin	fi
117*4887Schin)
118*4887Schinstatus=$?
119*4887Schin[[ $status == 126 ]] || err_exit "exit status of non-executable is $status -- 126 expected"
120*4887Schinbuiltin -d rm 2> /dev/null
121*4887Schinrm=$(whence rm)
122*4887Schind=$(dirname "$rm")
123*4887Schinunset FPATH
124*4887SchinPATH=/dev/null
125*4887Schinif	date > /dev/null 2>&1
126*4887Schinthen	err_exit 'programs in . should not be found'
127*4887Schinfi
128*4887Schin[[ $(whence ./foo) != "$PWD/"./foo ]] && err_exit 'whence ./foo not working'
129*4887Schin[[ $(whence "$PWD/foo") != "$PWD/foo" ]] && err_exit 'whence $PWD/foo not working'
130*4887Schin[[ $(whence ./xxxxx) ]] && err_exit 'whence ./xxxx not working'
131*4887SchinPATH=$d:
132*4887Schincp "$rm" kshrm$$
133*4887Schinif	[[ $(whence kshrm$$) != $PWD/kshrm$$  ]]
134*4887Schinthen	err_exit 'trailing : in pathname not working'
135*4887Schinfi
136*4887Schincp "$rm" rm
137*4887SchinPATH=:$d
138*4887Schinif	[[ $(whence rm) != $PWD/rm ]]
139*4887Schinthen	err_exit 'leading : in pathname not working'
140*4887Schinfi
141*4887SchinPATH=$d: whence rm > /dev/null
142*4887Schinif	[[ $(whence rm) != $PWD/rm ]]
143*4887Schinthen	err_exit 'pathname not restored after scoping'
144*4887Schinfi
145*4887Schinmkdir bin
146*4887Schinprint 'print ok' > bin/tst
147*4887Schinchmod +x bin/tst
148*4887Schinif	[[ $(PATH=$PWD/bin tst 2>/dev/null) != ok ]]
149*4887Schinthen	err_exit '(PATH=$PWD/bin foo) does not find $PWD/bin/foo'
150*4887Schinfi
151*4887Schincd /
152*4887Schinif	whence ls > /dev/null
153*4887Schinthen	PATH=
154*4887Schin	if	[[ $(whence rm) ]]
155*4887Schin	then	err_exit 'setting PATH to Null not working'
156*4887Schin	fi
157*4887Schin	unset PATH
158*4887Schin	if	[[ $(whence rm) != /*rm ]]
159*4887Schin	then	err_exit 'unsetting path  not working'
160*4887Schin	fi
161*4887Schinfi
162*4887SchinPATH=/dev:/tmp/ksh$$
163*4887Schinx=$(whence rm)
164*4887Schintypeset foo=$(PATH=/xyz:/abc :)
165*4887Schiny=$(whence rm)
166*4887Schin[[ $x != "$y" ]] && err_exit 'PATH not restored after command substitution'
167*4887Schinwhence getconf > /dev/null  &&  err_exit 'getconf should not be found'
168*4887Schinbuiltin /bin/getconf
169*4887SchinPATH=/bin
170*4887SchinPATH=$(getconf PATH)
171*4887Schinx=$(whence ls)
172*4887SchinPATH=.:$PWD:${x%/ls}
173*4887Schin[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when .:$PWD in path'
174*4887SchinPATH=$PWD:.:${x%/ls}
175*4887Schin[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when :$PWD:. in path'
176*4887Schincd   "${x%/ls}"
177*4887Schin[[ $(whence ls) == /* ]] || err_exit 'whence not generating absolute pathname'
178*4887Schinstatus=$($SHELL -c $'trap \'print $?\' EXIT;/a/b/c/d/e 2> /dev/null')
179*4887Schin[[ $status == 127 ]] || err_exit "not found command exit status $status -- expected 127"
180*4887Schinstatus=$($SHELL -c $'trap \'print $?\' EXIT;/dev/null 2> /dev/null')
181*4887Schin[[ $status == 126 ]] || err_exit "non executable command exit status $status -- expected 126"
182*4887Schinstatus=$($SHELL -c $'trap \'print $?\' ERR;/a/b/c/d/e 2> /dev/null')
183*4887Schin[[ $status == 127 ]] || err_exit "not found command with ERR trap exit status $status -- expected 127"
184*4887Schinstatus=$($SHELL -c $'trap \'print $?\' ERR;/dev/null 2> /dev/null')
185*4887Schin[[ $status == 126 ]] || err_exit "non executable command ERR trap exit status $status -- expected 126"
186*4887Schinexit $((Errors))
187