xref: /titanic_44/usr/src/lib/libshell/common/tests/path.sh (revision 54925bf60766fbb4f1f2d7c843721406a7b7a3fb)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                      by AT&T Knowledge Ventures                      #
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'
27
28Command=${0##*/}
29integer Errors=0
30mkdir /tmp/ksh$$
31cd /tmp/ksh$$
32trap "PATH=$PATH; cd /; rm -rf /tmp/ksh$$" EXIT
33(PATH="/bin")
34[[ $($SHELL -c 'print -r -- "$PATH"') == "$PATH" ]] || err_exit 'export PATH lost in subshell'
35cat > bug1 <<- \EOF
36	print print ok > /tmp/ok$$
37	/bin/chmod 755 /tmp/ok$$
38	trap 'cd /; rm -f /tmp/ok$$' EXIT
39	function a
40	{
41	        typeset -x PATH=/tmp
42	        ok$$
43	}
44	path=$PATH
45	unset PATH
46	a
47	PATH=$path
48}
49EOF
50[[ $($SHELL ./bug1  2> /dev/null) == ok ]]  || err_exit "PATH in function not working"
51cat > bug1 <<- \EOF
52	function lock_unlock
53	{
54	typeset PATH=/usr/bin
55	typeset -x PATH=''
56	}
57
58	PATH=/usr/bin
59	: $(PATH=/usr/bin getconf PATH)
60	typeset -ft lock_unlock
61	lock_unlock
62EOF
63($SHELL ./bug1)  2> /dev/null || err_exit "path_delete bug"
64mkdir tdir$$
65if	$SHELL tdir$$ > /dev/null 2>&1
66then	err_exit 'not an error to run ksh on a directory'
67fi
68
69print 'print hi' > ls
70if	[[ $($SHELL ls 2> /dev/null) != hi ]]
71then	err_exit "$SHELL name not executing version in current directory"
72fi
73if	[[ $(ls -d . 2>/dev/null) == . && $(PATH=/bin:/usr/bin:$PATH ls -d . 2>/dev/null) != . ]]
74then	err_exit 'PATH export in command substitution not working'
75fi
76pwd=$PWD
77# get rid of leading and trailing : and trailing :.
78PATH=${PATH%.}
79PATH=${PATH%:}
80PATH=${PATH#.}
81PATH=${PATH#:}
82path=$PATH
83var=$(whence date)
84dir=$(basename "$var")
85for i in 1 2 3 4 5 6 7 8 9 0
86do	if	! whence notfound$i 2> /dev/null
87	then	cmd=notfound$i
88		break
89	fi
90done
91print 'print hello' > date
92chmod +x date
93print 'print notfound' >  $cmd
94chmod +x "$cmd"
95> foo
96chmod 755 foo
97for PATH in $path :$path $path: .:$path $path: $path:. $PWD::$path $PWD:.:$path $path:$PWD $path:.:$PWD
98do
99#	print path=$PATH $(whence date)
100#	print path=$PATH $(whence "$cmd")
101		date
102		"$cmd"
103done > /dev/null 2>&1
104builtin -d date 2> /dev/null
105if	[[ $(PATH=:/usr/bin; date) != 'hello' ]]
106then	err_exit "leading : in path not working"
107fi
108(
109	PATH=$PWD:
110	builtin chmod
111	print 'print cannot execute' > noexec
112	chmod 644 noexec
113	if	[[ ! -x noexec ]]
114	then	noexec > /dev/null 2>&1
115	else	exit 126
116	fi
117)
118status=$?
119[[ $status == 126 ]] || err_exit "exit status of non-executable is $status -- 126 expected"
120builtin -d rm 2> /dev/null
121rm=$(whence rm)
122d=$(dirname "$rm")
123unset FPATH
124PATH=/dev/null
125if	date > /dev/null 2>&1
126then	err_exit 'programs in . should not be found'
127fi
128[[ $(whence ./foo) != "$PWD/"./foo ]] && err_exit 'whence ./foo not working'
129[[ $(whence "$PWD/foo") != "$PWD/foo" ]] && err_exit 'whence $PWD/foo not working'
130[[ $(whence ./xxxxx) ]] && err_exit 'whence ./xxxx not working'
131PATH=$d:
132cp "$rm" kshrm$$
133if	[[ $(whence kshrm$$) != $PWD/kshrm$$  ]]
134then	err_exit 'trailing : in pathname not working'
135fi
136cp "$rm" rm
137PATH=:$d
138if	[[ $(whence rm) != $PWD/rm ]]
139then	err_exit 'leading : in pathname not working'
140fi
141PATH=$d: whence rm > /dev/null
142if	[[ $(whence rm) != $PWD/rm ]]
143then	err_exit 'pathname not restored after scoping'
144fi
145mkdir bin
146print 'print ok' > bin/tst
147chmod +x bin/tst
148if	[[ $(PATH=$PWD/bin tst 2>/dev/null) != ok ]]
149then	err_exit '(PATH=$PWD/bin foo) does not find $PWD/bin/foo'
150fi
151cd /
152if	whence ls > /dev/null
153then	PATH=
154	if	[[ $(whence rm) ]]
155	then	err_exit 'setting PATH to Null not working'
156	fi
157	unset PATH
158	if	[[ $(whence rm) != /*rm ]]
159	then	err_exit 'unsetting path  not working'
160	fi
161fi
162PATH=/dev:/tmp/ksh$$
163x=$(whence rm)
164typeset foo=$(PATH=/xyz:/abc :)
165y=$(whence rm)
166[[ $x != "$y" ]] && err_exit 'PATH not restored after command substitution'
167whence getconf > /dev/null  &&  err_exit 'getconf should not be found'
168builtin /bin/getconf
169PATH=/bin
170PATH=$(getconf PATH)
171x=$(whence ls)
172PATH=.:$PWD:${x%/ls}
173[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when .:$PWD in path'
174PATH=$PWD:.:${x%/ls}
175[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when :$PWD:. in path'
176cd   "${x%/ls}"
177[[ $(whence ls) == /* ]] || err_exit 'whence not generating absolute pathname'
178status=$($SHELL -c $'trap \'print $?\' EXIT;/a/b/c/d/e 2> /dev/null')
179[[ $status == 127 ]] || err_exit "not found command exit status $status -- expected 127"
180status=$($SHELL -c $'trap \'print $?\' EXIT;/dev/null 2> /dev/null')
181[[ $status == 126 ]] || err_exit "non executable command exit status $status -- expected 126"
182status=$($SHELL -c $'trap \'print $?\' ERR;/a/b/c/d/e 2> /dev/null')
183[[ $status == 127 ]] || err_exit "not found command with ERR trap exit status $status -- expected 127"
184status=$($SHELL -c $'trap \'print $?\' ERR;/dev/null 2> /dev/null')
185[[ $status == 126 ]] || err_exit "non executable command ERR trap exit status $status -- expected 126"
186exit $((Errors))
187