Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/path.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
let Errors+=1
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0
30
31
tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32
trap "cd /; rm -rf $tmp" EXIT
33
34
cd $tmp || exit
35
type /xxxxxx > out1 2> out2
36
[[ -s out1 ]] && err_exit 'type should not write on stdout for not found case'
37
[[ -s out2 ]] || err_exit 'type should write on stderr for not found case'
38
mkdir dir1 dir2
39
cat > dir1/foobar << '+++'
40
foobar() { print foobar1;}
41
function dir1 { print dir1;}
42
+++
43
cat > dir2/foobar << '+++'
44
foobar() { print foobar2;}
45
function dir2 { print dir2;}
46
+++
47
chmod +x dir[12]/foobar
48
p=$PATH
49
FPATH=$PWD/dir1
50
PATH=$FPATH:$p
51
[[ $( foobar) == foobar1 ]] || err_exit 'foobar should output foobar1'
52
FPATH=$PWD/dir2
53
PATH=$FPATH:$p
54
[[ $(foobar) == foobar2 ]] || err_exit 'foobar should output foobar2'
55
FPATH=$PWD/dir1
56
PATH=$FPATH:$p
57
[[ $(foobar) == foobar1 ]] || err_exit 'foobar should output foobar1 again'
58
FPATH=$PWD/dir2
59
PATH=$FPATH:$p
60
[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${}'
61
[[ ${ dir2;} == dir2 ]] || err_exit 'should be dir2'
62
[[ ${ dir1;} == dir1 ]] 2> /dev/null && err_exit 'should not be be dir1'
63
FPATH=$PWD/dir1
64
PATH=$FPATH:$p
65
[[ ${ foobar;} == foobar1 ]] || err_exit 'foobar should output foobar1 with ${}'
66
[[ ${ dir1;} == dir1 ]] || err_exit 'should be dir1'
67
[[ ${ dir2;} == dir2 ]] 2> /dev/null && err_exit 'should not be be dir2'
68
FPATH=$PWD/dir2
69
PATH=$FPATH:$p
70
[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${} again'
71
PATH=$p
72
(PATH="/bin")
73
[[ $($SHELL -c 'print -r -- "$PATH"') == "$PATH" ]] || err_exit 'export PATH lost in subshell'
74
cat > bug1 <<- EOF
75
print print ok > $tmp/ok
76
/bin/chmod 755 $tmp/ok
77
function a
78
{
79
typeset -x PATH=$tmp
80
ok
81
}
82
path=\$PATH
83
unset PATH
84
a
85
PATH=\$path
86
}
87
EOF
88
[[ $($SHELL ./bug1 2>/dev/null) == ok ]] || err_exit "PATH in function not working"
89
cat > bug1 <<- \EOF
90
function lock_unlock
91
{
92
typeset PATH=/usr/bin
93
typeset -x PATH=''
94
}
95
96
PATH=/usr/bin
97
: $(PATH=/usr/bin getconf PATH)
98
typeset -ft lock_unlock
99
lock_unlock
100
EOF
101
($SHELL ./bug1) 2> /dev/null || err_exit "path_delete bug"
102
mkdir tdir
103
if $SHELL tdir > /dev/null 2>&1
104
then err_exit 'not an error to run ksh on a directory'
105
fi
106
107
print 'print hi' > ls
108
if [[ $($SHELL ls 2> /dev/null) != hi ]]
109
then err_exit "$SHELL name not executing version in current directory"
110
fi
111
if [[ $(ls -d . 2>/dev/null) == . && $(PATH=/bin:/usr/bin:$PATH ls -d . 2>/dev/null) != . ]]
112
then err_exit 'PATH export in command substitution not working'
113
fi
114
pwd=$PWD
115
# get rid of leading and trailing : and trailing :.
116
PATH=${PATH%.}
117
PATH=${PATH%:}
118
PATH=${PATH#.}
119
PATH=${PATH#:}
120
path=$PATH
121
var=$(whence date)
122
dir=$(basename "$var")
123
for i in 1 2 3 4 5 6 7 8 9 0
124
do if ! whence notfound$i 2> /dev/null
125
then cmd=notfound$i
126
break
127
fi
128
done
129
print 'print hello' > date
130
chmod +x date
131
print 'print notfound' > $cmd
132
chmod +x "$cmd"
133
> foo
134
chmod 755 foo
135
for PATH in $path :$path $path: .:$path $path: $path:. $PWD::$path $PWD:.:$path $path:$PWD $path:.:$PWD
136
do
137
# print path=$PATH $(whence date)
138
# print path=$PATH $(whence "$cmd")
139
date
140
"$cmd"
141
done > /dev/null 2>&1
142
builtin -d date 2> /dev/null
143
if [[ $(PATH=:/usr/bin; date) != 'hello' ]]
144
then err_exit "leading : in path not working"
145
fi
146
(
147
PATH=$PWD:
148
builtin chmod
149
print 'print cannot execute' > noexec
150
chmod 644 noexec
151
if [[ ! -x noexec ]]
152
then noexec > /dev/null 2>&1
153
else exit 126
154
fi
155
)
156
status=$?
157
[[ $status == 126 ]] || err_exit "exit status of non-executable is $status -- 126 expected"
158
builtin -d rm 2> /dev/null
159
chmod=$(whence chmod)
160
rm=$(whence rm)
161
d=$(dirname "$rm")
162
163
chmod=$(whence chmod)
164
165
for cmd in date foo
166
do exp="$cmd found"
167
print print $exp > $cmd
168
$chmod +x $cmd
169
got=$($SHELL -c "unset FPATH; PATH=/dev/null; $cmd" 2>&1)
170
[[ $got == $exp ]] && err_exit "$cmd as last command should not find ./$cmd with PATH=/dev/null"
171
got=$($SHELL -c "unset FPATH; PATH=/dev/null; $cmd" 2>&1)
172
[[ $got == $exp ]] && err_exit "$cmd should not find ./$cmd with PATH=/dev/null"
173
exp=$PWD/./$cmd
174
got=$(unset FPATH; PATH=/dev/null; whence ./$cmd)
175
[[ $got == $exp ]] || err_exit "whence $cmd should find ./$cmd with PATH=/dev/null"
176
exp=$PWD/$cmd
177
got=$(unset FPATH; PATH=/dev/null; whence $PWD/$cmd)
178
[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd should find ./$cmd with PATH=/dev/null"
179
done
180
181
exp=''
182
got=$($SHELL -c "unset FPATH; PATH=/dev/null; whence ./notfound" 2>&1)
183
[[ $got == $exp ]] || err_exit "whence ./$cmd failed -- expected '$exp', got '$got'"
184
got=$($SHELL -c "unset FPATH; PATH=/dev/null; whence $PWD/notfound" 2>&1)
185
[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd failed -- expected '$exp', got '$got'"
186
187
unset FPATH
188
PATH=/dev/null
189
for cmd in date foo
190
do exp="$cmd found"
191
print print $exp > $cmd
192
$chmod +x $cmd
193
got=$($cmd 2>&1)
194
[[ $got == $exp ]] && err_exit "$cmd as last command should not find ./$cmd with PATH=/dev/null"
195
got=$($cmd 2>&1; :)
196
[[ $got == $exp ]] && err_exit "$cmd should not find ./$cmd with PATH=/dev/null"
197
exp=$PWD/./$cmd
198
got=$(whence ./$cmd)
199
[[ $got == $exp ]] || err_exit "whence ./$cmd should find ./$cmd with PATH=/dev/null"
200
exp=$PWD/$cmd
201
got=$(whence $PWD/$cmd)
202
[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd should find ./$cmd with PATH=/dev/null"
203
done
204
exp=''
205
got=$(whence ./notfound)
206
[[ $got == $exp ]] || err_exit "whence ./$cmd failed -- expected '$exp', got '$got'"
207
got=$(whence $PWD/notfound)
208
[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd failed -- expected '$exp', got '$got'"
209
210
PATH=$d:
211
cp "$rm" kshrm
212
if [[ $(whence kshrm) != $PWD/kshrm ]]
213
then err_exit 'trailing : in pathname not working'
214
fi
215
cp "$rm" rm
216
PATH=:$d
217
if [[ $(whence rm) != $PWD/rm ]]
218
then err_exit 'leading : in pathname not working'
219
fi
220
PATH=$d: whence rm > /dev/null
221
if [[ $(whence rm) != $PWD/rm ]]
222
then err_exit 'pathname not restored after scoping'
223
fi
224
mkdir bin
225
print 'print ok' > bin/tst
226
chmod +x bin/tst
227
if [[ $(PATH=$PWD/bin tst 2>/dev/null) != ok ]]
228
then err_exit '(PATH=$PWD/bin foo) does not find $PWD/bin/foo'
229
fi
230
cd /
231
if whence ls > /dev/null
232
then PATH=
233
if [[ $(whence rm) ]]
234
then err_exit 'setting PATH to Null not working'
235
fi
236
unset PATH
237
if [[ $(whence rm) != /*rm ]]
238
then err_exit 'unsetting path not working'
239
fi
240
fi
241
PATH=/dev:$tmp
242
x=$(whence rm)
243
typeset foo=$(PATH=/xyz:/abc :)
244
y=$(whence rm)
245
[[ $x != "$y" ]] && err_exit 'PATH not restored after command substitution'
246
whence getconf > /dev/null && err_exit 'getconf should not be found'
247
builtin /bin/getconf
248
PATH=/bin
249
PATH=$(getconf PATH)
250
x=$(whence ls)
251
PATH=.:$PWD:${x%/ls}
252
[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when .:$PWD in path'
253
PATH=$PWD:.:${x%/ls}
254
[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when :$PWD:. in path'
255
cd "${x%/ls}"
256
[[ $(whence ls) == /* ]] || err_exit 'whence not generating absolute pathname'
257
status=$($SHELL -c $'trap \'print $?\' EXIT;/xxx/a/b/c/d/e 2> /dev/null')
258
[[ $status == 127 ]] || err_exit "not found command exit status $status -- expected 127"
259
status=$($SHELL -c $'trap \'print $?\' EXIT;/dev/null 2> /dev/null')
260
[[ $status == 126 ]] || err_exit "non executable command exit status $status -- expected 126"
261
status=$($SHELL -c $'trap \'print $?\' ERR;/xxx/a/b/c/d/e 2> /dev/null')
262
[[ $status == 127 ]] || err_exit "not found command with ERR trap exit status $status -- expected 127"
263
status=$($SHELL -c $'trap \'print $?\' ERR;/dev/null 2> /dev/null')
264
[[ $status == 126 ]] || err_exit "non executable command ERR trap exit status $status -- expected 126"
265
266
# universe via PATH
267
268
builtin getconf
269
getconf UNIVERSE - att # override sticky default 'UNIVERSE = foo'
270
271
[[ $(PATH=/usr/ucb/bin:/usr/bin echo -n ucb) == 'ucb' ]] || err_exit "ucb universe echo ignores -n option"
272
[[ $(PATH=/usr/xpg/bin:/usr/bin echo -n att) == '-n att' ]] || err_exit "att universe echo does not ignore -n option"
273
274
PATH=$path
275
276
scr=$tmp/script
277
exp=126
278
279
: > $scr
280
chmod a=x $scr
281
{ got=$($scr; print $?); } 2>/dev/null
282
[[ "$got" == "$exp" ]] || err_exit "unreadable empty script should fail -- expected $exp, got $got"
283
{ got=$(command $scr; print $?); } 2>/dev/null
284
[[ "$got" == "$exp" ]] || err_exit "command of unreadable empty script should fail -- expected $exp, got $got"
285
[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable empty script in [[ ... ]] should fail -- expected $exp"
286
[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable empty script in [[ ... ]] should fail -- expected $exp"
287
got=$($SHELL -c "$scr; print \$?" 2>/dev/null)
288
[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable empty script should fail -- expected $exp, got" $got
289
got=$($SHELL -c "command $scr; print \$?" 2>/dev/null)
290
[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable empty script should fail -- expected $exp, got" $got
291
292
rm -f $scr
293
print : > $scr
294
chmod a=x $scr
295
{ got=$($scr; print $?); } 2>/dev/null
296
[[ "$got" == "$exp" ]] || err_exit "unreadable non-empty script should fail -- expected $exp, got $got"
297
{ got=$(command $scr; print $?); } 2>/dev/null
298
[[ "$got" == "$exp" ]] || err_exit "command of unreadable non-empty script should fail -- expected $exp, got $got"
299
[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable non-empty script in [[ ... ]] should fail -- expected $exp"
300
[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable non-empty script in [[ ... ]] should fail -- expected $exp"
301
got=$($SHELL -c "$scr; print \$?" 2>/dev/null)
302
[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable non-empty script should fail -- expected $exp, got" $got
303
got=$($SHELL -c "command $scr; print \$?" 2>/dev/null)
304
[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable non-empty script should fail -- expected $exp, got" $got
305
306
# whence -a bug fix
307
cd "$tmp"
308
ifs=$IFS
309
IFS=$'\n'
310
PATH=$PATH:
311
> ls
312
chmod +x ls
313
ok=
314
for i in $(whence -a ls)
315
do if [[ $i == *"$PWD/ls" ]]
316
then ok=1
317
break;
318
fi
319
done
320
[[ $ok ]] || err_exit 'whence -a not finding all executables'
321
rm -f ls
322
PATH=${PATH%:}
323
324
#whence -p bug fix
325
function foo
326
{
327
:
328
}
329
[[ $(whence -p foo) == foo ]] && err_exit 'whence -p foo should not find function foo'
330
331
# whence -q bug fix
332
$SHELL -c 'whence -q cat' & pid=$!
333
sleep 3
334
kill $! 2> /dev/null && err_exit 'whence -q appears to be hung'
335
336
FPATH=$PWD
337
print 'function foobar { :;}' > foobar
338
autoload foobar;
339
exec {m}< /dev/null
340
for ((i=0; i < 25; i++))
341
do ( foobar )
342
done
343
exec {m}<& -
344
exec {n}< /dev/null
345
(( n > m )) && err_exit 'autoload function in subshell leaves file open'
346
347
# whence -a bug fix
348
rmdir=rmdir
349
if mkdir "$rmdir"
350
then rm=${ whence rm;}
351
cp "$rm" "$rmdir"
352
{ PATH=:${rm%/rm} $SHELL -c "cd \"$rmdir\";whence -a rm";} > /dev/null 2>&1
353
exitval=$?
354
(( exitval==0 )) || err_exit "whence -a has exitval $exitval"
355
fi
356
357
[[ ! -d bin ]] && mkdir bin
358
[[ ! -d fun ]] && mkdir fun
359
print 'FPATH=../fun' > bin/.paths
360
cat <<- \EOF > fun/myfun
361
function myfun
362
{
363
print myfun
364
}
365
EOF
366
x=$(FPATH= PATH=$PWD/bin $SHELL -c ': $(whence less);myfun') 2> /dev/null
367
[[ $x == myfun ]] || err_exit 'function myfun not found'
368
369
cp $(whence -p echo) user_to_group_relationship.hdr.query
370
FPATH=/foobar:
371
PATH=$FPATH:$PATH:.
372
[[ $(user_to_group_relationship.hdr.query foobar) == foobar ]] 2> /dev/null || err_exit 'Cannot execute command with . in name when PATH and FPATH end in :.'
373
374
mkdir -p $tmp/new/bin
375
mkdir $tmp/new/fun
376
print FPATH=../fun > $tmp/new/bin/.paths
377
print FPATH=../xxfun > $tmp/bin/.paths
378
cp "$(whence -p echo)" $tmp/new/bin
379
PATH=$tmp/bin:$tmp/new/bin:$PATH
380
x=$(whence -p echo 2> /dev/null)
381
[[ $x == "$tmp/new/bin/echo" ]] || err_exit 'nonexistant FPATH directory in .paths file causes path search to fail'
382
383
$SHELL 2> /dev/null <<- \EOF || err_exit 'path search problem with non-existant directories in PATH'
384
PATH=/usr/nogood1/bin:/usr/nogood2/bin:/bin:/usr/bin
385
tail /dev/null && tail /dev/null
386
EOF
387
388
( PATH=/bin:usr/bin
389
cat << END >/dev/null 2>&1
390
${.sh.version}
391
END
392
) || err_exit '${.sh.xxx} variables causes cat not be found'
393
394
exit $((Errors<125?Errors:125))
395
396
397