xref: /titanic_44/usr/src/lib/libshell/common/tests/path.sh (revision a6a911618075176ed839dbe7f7c90604d0954b46)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2008 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27Command=${0##*/}
28integer Errors=0
29
30mkdir /tmp/ksh$$
31cd /tmp/ksh$$
32trap "PATH=$PATH; cd /; rm -rf /tmp/ksh$$" EXIT
33type /xxxxxx > out1 2> out2
34[[ -s out1 ]] && err_exit 'type should not write on stdout for not found case'
35[[ -s out2 ]] || err_exit 'type should write on stderr for not found case'
36mkdir dir1 dir2
37cat  > dir1/foobar << '+++'
38foobar() { print foobar1;}
39function dir1 { print dir1;}
40+++
41cat  > dir2/foobar << '+++'
42foobar() { print foobar2;}
43function dir2 { print dir2;}
44+++
45chmod +x dir[12]/foobar
46p=$PATH
47FPATH=$PWD/dir1
48PATH=$FPATH:$p
49[[ $( foobar) == foobar1 ]] || err_exit 'foobar should output foobar1'
50FPATH=$PWD/dir2
51PATH=$FPATH:$p
52[[ $(foobar) == foobar2 ]] || err_exit 'foobar should output foobar2'
53FPATH=$PWD/dir1
54PATH=$FPATH:$p
55[[ $(foobar) == foobar1 ]] || err_exit 'foobar should output foobar1 again'
56FPATH=$PWD/dir2
57PATH=$FPATH:$p
58[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${}'
59[[ ${ dir2;} == dir2 ]] || err_exit 'should be dir2'
60[[ ${ dir1;} == dir1 ]] 2> /dev/null &&  err_exit 'should not be be dir1'
61FPATH=$PWD/dir1
62PATH=$FPATH:$p
63[[ ${ foobar;} == foobar1 ]] || err_exit 'foobar should output foobar1 with ${}'
64[[ ${ dir1;} == dir1 ]] || err_exit 'should be dir1'
65[[ ${ dir2;} == dir2 ]] 2> /dev/null &&  err_exit 'should not be be dir2'
66FPATH=$PWD/dir2
67PATH=$FPATH:$p
68[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${} again'
69PATH=$p
70(PATH="/bin")
71[[ $($SHELL -c 'print -r -- "$PATH"') == "$PATH" ]] || err_exit 'export PATH lost in subshell'
72cat > bug1 <<- \EOF
73	print print ok > /tmp/ok$$
74	/bin/chmod 755 /tmp/ok$$
75	trap 'cd /; rm -f /tmp/ok$$' EXIT
76	function a
77	{
78	        typeset -x PATH=/tmp
79	        ok$$
80	}
81	path=$PATH
82	unset PATH
83	a
84	PATH=$path
85}
86EOF
87[[ $($SHELL ./bug1  2> /dev/null) == ok ]]  || err_exit "PATH in function not working"
88cat > bug1 <<- \EOF
89	function lock_unlock
90	{
91	typeset PATH=/usr/bin
92	typeset -x PATH=''
93	}
94
95	PATH=/usr/bin
96	: $(PATH=/usr/bin getconf PATH)
97	typeset -ft lock_unlock
98	lock_unlock
99EOF
100($SHELL ./bug1)  2> /dev/null || err_exit "path_delete bug"
101mkdir tdir$$
102if	$SHELL tdir$$ > /dev/null 2>&1
103then	err_exit 'not an error to run ksh on a directory'
104fi
105
106print 'print hi' > ls
107if	[[ $($SHELL ls 2> /dev/null) != hi ]]
108then	err_exit "$SHELL name not executing version in current directory"
109fi
110if	[[ $(ls -d . 2>/dev/null) == . && $(PATH=/bin:/usr/bin:$PATH ls -d . 2>/dev/null) != . ]]
111then	err_exit 'PATH export in command substitution not working'
112fi
113pwd=$PWD
114# get rid of leading and trailing : and trailing :.
115PATH=${PATH%.}
116PATH=${PATH%:}
117PATH=${PATH#.}
118PATH=${PATH#:}
119path=$PATH
120var=$(whence date)
121dir=$(basename "$var")
122for i in 1 2 3 4 5 6 7 8 9 0
123do	if	! whence notfound$i 2> /dev/null
124	then	cmd=notfound$i
125		break
126	fi
127done
128print 'print hello' > date
129chmod +x date
130print 'print notfound' >  $cmd
131chmod +x "$cmd"
132> foo
133chmod 755 foo
134for PATH in $path :$path $path: .:$path $path: $path:. $PWD::$path $PWD:.:$path $path:$PWD $path:.:$PWD
135do
136#	print path=$PATH $(whence date)
137#	print path=$PATH $(whence "$cmd")
138		date
139		"$cmd"
140done > /dev/null 2>&1
141builtin -d date 2> /dev/null
142if	[[ $(PATH=:/usr/bin; date) != 'hello' ]]
143then	err_exit "leading : in path not working"
144fi
145(
146	PATH=$PWD:
147	builtin chmod
148	print 'print cannot execute' > noexec
149	chmod 644 noexec
150	if	[[ ! -x noexec ]]
151	then	noexec > /dev/null 2>&1
152	else	exit 126
153	fi
154)
155status=$?
156[[ $status == 126 ]] || err_exit "exit status of non-executable is $status -- 126 expected"
157builtin -d rm 2> /dev/null
158rm=$(whence rm)
159d=$(dirname "$rm")
160unset FPATH
161PATH=/dev/null
162if	date > /dev/null 2>&1
163then	err_exit 'programs in . should not be found'
164fi
165[[ $(whence ./foo) != "$PWD/"./foo ]] && err_exit 'whence ./foo not working'
166[[ $(whence "$PWD/foo") != "$PWD/foo" ]] && err_exit 'whence $PWD/foo not working'
167[[ $(whence ./xxxxx) ]] && err_exit 'whence ./xxxx not working'
168PATH=$d:
169cp "$rm" kshrm$$
170if	[[ $(whence kshrm$$) != $PWD/kshrm$$  ]]
171then	err_exit 'trailing : in pathname not working'
172fi
173cp "$rm" rm
174PATH=:$d
175if	[[ $(whence rm) != $PWD/rm ]]
176then	err_exit 'leading : in pathname not working'
177fi
178PATH=$d: whence rm > /dev/null
179if	[[ $(whence rm) != $PWD/rm ]]
180then	err_exit 'pathname not restored after scoping'
181fi
182mkdir bin
183print 'print ok' > bin/tst
184chmod +x bin/tst
185if	[[ $(PATH=$PWD/bin tst 2>/dev/null) != ok ]]
186then	err_exit '(PATH=$PWD/bin foo) does not find $PWD/bin/foo'
187fi
188cd /
189if	whence ls > /dev/null
190then	PATH=
191	if	[[ $(whence rm) ]]
192	then	err_exit 'setting PATH to Null not working'
193	fi
194	unset PATH
195	if	[[ $(whence rm) != /*rm ]]
196	then	err_exit 'unsetting path  not working'
197	fi
198fi
199PATH=/dev:/tmp/ksh$$
200x=$(whence rm)
201typeset foo=$(PATH=/xyz:/abc :)
202y=$(whence rm)
203[[ $x != "$y" ]] && err_exit 'PATH not restored after command substitution'
204whence getconf > /dev/null  &&  err_exit 'getconf should not be found'
205builtin /bin/getconf
206PATH=/bin
207PATH=$(getconf PATH)
208x=$(whence ls)
209PATH=.:$PWD:${x%/ls}
210[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when .:$PWD in path'
211PATH=$PWD:.:${x%/ls}
212[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when :$PWD:. in path'
213cd   "${x%/ls}"
214[[ $(whence ls) == /* ]] || err_exit 'whence not generating absolute pathname'
215status=$($SHELL -c $'trap \'print $?\' EXIT;/a/b/c/d/e 2> /dev/null')
216[[ $status == 127 ]] || err_exit "not found command exit status $status -- expected 127"
217status=$($SHELL -c $'trap \'print $?\' EXIT;/dev/null 2> /dev/null')
218[[ $status == 126 ]] || err_exit "non executable command exit status $status -- expected 126"
219status=$($SHELL -c $'trap \'print $?\' ERR;/a/b/c/d/e 2> /dev/null')
220[[ $status == 127 ]] || err_exit "not found command with ERR trap exit status $status -- expected 127"
221status=$($SHELL -c $'trap \'print $?\' ERR;/dev/null 2> /dev/null')
222[[ $status == 126 ]] || err_exit "non executable command ERR trap exit status $status -- expected 126"
223
224# universe via PATH
225
226builtin getconf
227getconf UNIVERSE - att # override sticky default 'UNIVERSE = foo'
228
229[[ $(PATH=/usr/ucb/bin:/usr/bin echo -n ucb) == 'ucb' ]] || err_exit "ucb universe echo ignores -n option"
230[[ $(PATH=/usr/xpg/bin:/usr/bin echo -n att) == '-n att' ]] || err_exit "att universe echo does not ignore -n option"
231
232PATH=$path
233
234scr=/tmp/ksh$$/foo
235exp=126
236
237: > $scr
238chmod a=x $scr
239{ got=$($scr; print $?); } 2>/dev/null
240[[ "$got" == "$exp" ]] || err_exit "unreadable empty script should fail -- expected $exp, got $got"
241{ got=$(command $scr; print $?); } 2>/dev/null
242[[ "$got" == "$exp" ]] || err_exit "command of unreadable empty script should fail -- expected $exp, got $got"
243[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable empty script in [[ ... ]] should fail -- expected $exp"
244[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable empty script in [[ ... ]] should fail -- expected $exp"
245got=$($SHELL -c "$scr; print \$?" 2>/dev/null)
246[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable empty script should fail -- expected $exp, got" $got
247got=$($SHELL -c "command $scr; print \$?" 2>/dev/null)
248[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable empty script should fail -- expected $exp, got" $got
249
250rm -f $scr
251print : > $scr
252chmod a=x $scr
253{ got=$($scr; print $?); } 2>/dev/null
254[[ "$got" == "$exp" ]] || err_exit "unreadable non-empty script should fail -- expected $exp, got $got"
255{ got=$(command $scr; print $?); } 2>/dev/null
256[[ "$got" == "$exp" ]] || err_exit "command of unreadable non-empty script should fail -- expected $exp, got $got"
257[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable non-empty script in [[ ... ]] should fail -- expected $exp"
258[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable non-empty script in [[ ... ]] should fail -- expected $exp"
259got=$($SHELL -c "$scr; print \$?" 2>/dev/null)
260[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable non-empty script should fail -- expected $exp, got" $got
261got=$($SHELL -c "command $scr; print \$?" 2>/dev/null)
262[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable non-empty script should fail -- expected $exp, got" $got
263
264exit $((Errors))
265