14887Schin######################################################################## 24887Schin# # 34887Schin# This software is part of the ast package # 4*10898Sroland.mainz@nrubsig.org# Copyright (c) 1982-2009 AT&T Intellectual Property # 54887Schin# and is licensed under the # 64887Schin# Common Public License, Version 1.0 # 78462SApril.Chin@Sun.COM# by AT&T Intellectual Property # 84887Schin# # 94887Schin# A copy of the License is available at # 104887Schin# http://www.opensource.org/licenses/cpl1.0.txt # 114887Schin# (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) # 124887Schin# # 134887Schin# Information and Software Systems Research # 144887Schin# AT&T Research # 154887Schin# Florham Park NJ # 164887Schin# # 174887Schin# David Korn <dgk@research.att.com> # 184887Schin# # 194887Schin######################################################################## 204887Schinfunction err_exit 214887Schin{ 224887Schin print -u2 -n "\t" 234887Schin print -u2 -r ${Command}[$1]: "${@:2}" 244887Schin let Errors+=1 254887Schin} 264887Schinalias err_exit='err_exit $LINENO' 27*10898Sroland.mainz@nrubsig.org 284887SchinCommand=${0##*/} 294887Schininteger Errors=0 308462SApril.Chin@Sun.COM 31*10898Sroland.mainz@nrubsig.orgtmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; } 32*10898Sroland.mainz@nrubsig.orgtrap "cd /; rm -rf $tmp" EXIT 33*10898Sroland.mainz@nrubsig.org 34*10898Sroland.mainz@nrubsig.orgcd $tmp || exit 358462SApril.Chin@Sun.COMtype /xxxxxx > out1 2> out2 368462SApril.Chin@Sun.COM[[ -s out1 ]] && err_exit 'type should not write on stdout for not found case' 378462SApril.Chin@Sun.COM[[ -s out2 ]] || err_exit 'type should write on stderr for not found case' 388462SApril.Chin@Sun.COMmkdir dir1 dir2 398462SApril.Chin@Sun.COMcat > dir1/foobar << '+++' 408462SApril.Chin@Sun.COMfoobar() { print foobar1;} 418462SApril.Chin@Sun.COMfunction dir1 { print dir1;} 428462SApril.Chin@Sun.COM+++ 438462SApril.Chin@Sun.COMcat > dir2/foobar << '+++' 448462SApril.Chin@Sun.COMfoobar() { print foobar2;} 458462SApril.Chin@Sun.COMfunction dir2 { print dir2;} 468462SApril.Chin@Sun.COM+++ 478462SApril.Chin@Sun.COMchmod +x dir[12]/foobar 488462SApril.Chin@Sun.COMp=$PATH 498462SApril.Chin@Sun.COMFPATH=$PWD/dir1 508462SApril.Chin@Sun.COMPATH=$FPATH:$p 518462SApril.Chin@Sun.COM[[ $( foobar) == foobar1 ]] || err_exit 'foobar should output foobar1' 528462SApril.Chin@Sun.COMFPATH=$PWD/dir2 538462SApril.Chin@Sun.COMPATH=$FPATH:$p 548462SApril.Chin@Sun.COM[[ $(foobar) == foobar2 ]] || err_exit 'foobar should output foobar2' 558462SApril.Chin@Sun.COMFPATH=$PWD/dir1 568462SApril.Chin@Sun.COMPATH=$FPATH:$p 578462SApril.Chin@Sun.COM[[ $(foobar) == foobar1 ]] || err_exit 'foobar should output foobar1 again' 588462SApril.Chin@Sun.COMFPATH=$PWD/dir2 598462SApril.Chin@Sun.COMPATH=$FPATH:$p 608462SApril.Chin@Sun.COM[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${}' 618462SApril.Chin@Sun.COM[[ ${ dir2;} == dir2 ]] || err_exit 'should be dir2' 628462SApril.Chin@Sun.COM[[ ${ dir1;} == dir1 ]] 2> /dev/null && err_exit 'should not be be dir1' 638462SApril.Chin@Sun.COMFPATH=$PWD/dir1 648462SApril.Chin@Sun.COMPATH=$FPATH:$p 658462SApril.Chin@Sun.COM[[ ${ foobar;} == foobar1 ]] || err_exit 'foobar should output foobar1 with ${}' 668462SApril.Chin@Sun.COM[[ ${ dir1;} == dir1 ]] || err_exit 'should be dir1' 678462SApril.Chin@Sun.COM[[ ${ dir2;} == dir2 ]] 2> /dev/null && err_exit 'should not be be dir2' 688462SApril.Chin@Sun.COMFPATH=$PWD/dir2 698462SApril.Chin@Sun.COMPATH=$FPATH:$p 708462SApril.Chin@Sun.COM[[ ${ foobar;} == foobar2 ]] || err_exit 'foobar should output foobar2 with ${} again' 718462SApril.Chin@Sun.COMPATH=$p 724887Schin(PATH="/bin") 734887Schin[[ $($SHELL -c 'print -r -- "$PATH"') == "$PATH" ]] || err_exit 'export PATH lost in subshell' 74*10898Sroland.mainz@nrubsig.orgcat > bug1 <<- EOF 75*10898Sroland.mainz@nrubsig.org print print ok > $tmp/ok 76*10898Sroland.mainz@nrubsig.org /bin/chmod 755 $tmp/ok 774887Schin function a 784887Schin { 79*10898Sroland.mainz@nrubsig.org typeset -x PATH=$tmp 80*10898Sroland.mainz@nrubsig.org ok 814887Schin } 82*10898Sroland.mainz@nrubsig.org path=\$PATH 834887Schin unset PATH 844887Schin a 85*10898Sroland.mainz@nrubsig.org PATH=\$path 864887Schin} 874887SchinEOF 88*10898Sroland.mainz@nrubsig.org[[ $($SHELL ./bug1 2>/dev/null) == ok ]] || err_exit "PATH in function not working" 894887Schincat > bug1 <<- \EOF 904887Schin function lock_unlock 914887Schin { 924887Schin typeset PATH=/usr/bin 934887Schin typeset -x PATH='' 944887Schin } 95*10898Sroland.mainz@nrubsig.org 964887Schin PATH=/usr/bin 974887Schin : $(PATH=/usr/bin getconf PATH) 984887Schin typeset -ft lock_unlock 994887Schin lock_unlock 1004887SchinEOF 1014887Schin($SHELL ./bug1) 2> /dev/null || err_exit "path_delete bug" 102*10898Sroland.mainz@nrubsig.orgmkdir tdir 103*10898Sroland.mainz@nrubsig.orgif $SHELL tdir > /dev/null 2>&1 1044887Schinthen err_exit 'not an error to run ksh on a directory' 1054887Schinfi 1064887Schin 1074887Schinprint 'print hi' > ls 1084887Schinif [[ $($SHELL ls 2> /dev/null) != hi ]] 1094887Schinthen err_exit "$SHELL name not executing version in current directory" 1104887Schinfi 1114887Schinif [[ $(ls -d . 2>/dev/null) == . && $(PATH=/bin:/usr/bin:$PATH ls -d . 2>/dev/null) != . ]] 1124887Schinthen err_exit 'PATH export in command substitution not working' 1134887Schinfi 1144887Schinpwd=$PWD 1154887Schin# get rid of leading and trailing : and trailing :. 1164887SchinPATH=${PATH%.} 1174887SchinPATH=${PATH%:} 1184887SchinPATH=${PATH#.} 1194887SchinPATH=${PATH#:} 1204887Schinpath=$PATH 1214887Schinvar=$(whence date) 1224887Schindir=$(basename "$var") 1234887Schinfor i in 1 2 3 4 5 6 7 8 9 0 1244887Schindo if ! whence notfound$i 2> /dev/null 125*10898Sroland.mainz@nrubsig.org then cmd=notfound$i 1264887Schin break 1274887Schin fi 1284887Schindone 1294887Schinprint 'print hello' > date 1304887Schinchmod +x date 1314887Schinprint 'print notfound' > $cmd 1324887Schinchmod +x "$cmd" 1334887Schin> foo 1344887Schinchmod 755 foo 1354887Schinfor PATH in $path :$path $path: .:$path $path: $path:. $PWD::$path $PWD:.:$path $path:$PWD $path:.:$PWD 136*10898Sroland.mainz@nrubsig.orgdo 1374887Schin# print path=$PATH $(whence date) 1384887Schin# print path=$PATH $(whence "$cmd") 1394887Schin date 1404887Schin "$cmd" 1414887Schindone > /dev/null 2>&1 1424887Schinbuiltin -d date 2> /dev/null 1434887Schinif [[ $(PATH=:/usr/bin; date) != 'hello' ]] 1444887Schinthen err_exit "leading : in path not working" 1454887Schinfi 1464887Schin( 1474887Schin PATH=$PWD: 1484887Schin builtin chmod 1494887Schin print 'print cannot execute' > noexec 1504887Schin chmod 644 noexec 1514887Schin if [[ ! -x noexec ]] 1524887Schin then noexec > /dev/null 2>&1 1534887Schin else exit 126 1544887Schin fi 1554887Schin) 1564887Schinstatus=$? 1574887Schin[[ $status == 126 ]] || err_exit "exit status of non-executable is $status -- 126 expected" 1584887Schinbuiltin -d rm 2> /dev/null 159*10898Sroland.mainz@nrubsig.orgchmod=$(whence chmod) 1604887Schinrm=$(whence rm) 1614887Schind=$(dirname "$rm") 162*10898Sroland.mainz@nrubsig.org 163*10898Sroland.mainz@nrubsig.orgchmod=$(whence chmod) 164*10898Sroland.mainz@nrubsig.org 165*10898Sroland.mainz@nrubsig.orgfor cmd in date foo 166*10898Sroland.mainz@nrubsig.orgdo exp="$cmd found" 167*10898Sroland.mainz@nrubsig.org print print $exp > $cmd 168*10898Sroland.mainz@nrubsig.org $chmod +x $cmd 169*10898Sroland.mainz@nrubsig.org got=$($SHELL -c "unset FPATH; PATH=/dev/null; $cmd" 2>&1) 170*10898Sroland.mainz@nrubsig.org [[ $got == $exp ]] && err_exit "$cmd as last command should not find ./$cmd with PATH=/dev/null" 171*10898Sroland.mainz@nrubsig.org got=$($SHELL -c "unset FPATH; PATH=/dev/null; $cmd" 2>&1) 172*10898Sroland.mainz@nrubsig.org [[ $got == $exp ]] && err_exit "$cmd should not find ./$cmd with PATH=/dev/null" 173*10898Sroland.mainz@nrubsig.org exp=$PWD/./$cmd 174*10898Sroland.mainz@nrubsig.org got=$(unset FPATH; PATH=/dev/null; whence ./$cmd) 175*10898Sroland.mainz@nrubsig.org [[ $got == $exp ]] || err_exit "whence $cmd should find ./$cmd with PATH=/dev/null" 176*10898Sroland.mainz@nrubsig.org exp=$PWD/$cmd 177*10898Sroland.mainz@nrubsig.org got=$(unset FPATH; PATH=/dev/null; whence $PWD/$cmd) 178*10898Sroland.mainz@nrubsig.org [[ $got == $exp ]] || err_exit "whence \$PWD/$cmd should find ./$cmd with PATH=/dev/null" 179*10898Sroland.mainz@nrubsig.orgdone 180*10898Sroland.mainz@nrubsig.org 181*10898Sroland.mainz@nrubsig.orgexp='' 182*10898Sroland.mainz@nrubsig.orggot=$($SHELL -c "unset FPATH; PATH=/dev/null; whence ./notfound" 2>&1) 183*10898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "whence ./$cmd failed -- expected '$exp', got '$got'" 184*10898Sroland.mainz@nrubsig.orggot=$($SHELL -c "unset FPATH; PATH=/dev/null; whence $PWD/notfound" 2>&1) 185*10898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd failed -- expected '$exp', got '$got'" 186*10898Sroland.mainz@nrubsig.org 1874887Schinunset FPATH 1884887SchinPATH=/dev/null 189*10898Sroland.mainz@nrubsig.orgfor cmd in date foo 190*10898Sroland.mainz@nrubsig.orgdo exp="$cmd found" 191*10898Sroland.mainz@nrubsig.org print print $exp > $cmd 192*10898Sroland.mainz@nrubsig.org $chmod +x $cmd 193*10898Sroland.mainz@nrubsig.org got=$($cmd 2>&1) 194*10898Sroland.mainz@nrubsig.org [[ $got == $exp ]] && err_exit "$cmd as last command should not find ./$cmd with PATH=/dev/null" 195*10898Sroland.mainz@nrubsig.org got=$($cmd 2>&1; :) 196*10898Sroland.mainz@nrubsig.org [[ $got == $exp ]] && err_exit "$cmd should not find ./$cmd with PATH=/dev/null" 197*10898Sroland.mainz@nrubsig.org exp=$PWD/./$cmd 198*10898Sroland.mainz@nrubsig.org got=$(whence ./$cmd) 199*10898Sroland.mainz@nrubsig.org [[ $got == $exp ]] || err_exit "whence ./$cmd should find ./$cmd with PATH=/dev/null" 200*10898Sroland.mainz@nrubsig.org exp=$PWD/$cmd 201*10898Sroland.mainz@nrubsig.org got=$(whence $PWD/$cmd) 202*10898Sroland.mainz@nrubsig.org [[ $got == $exp ]] || err_exit "whence \$PWD/$cmd should find ./$cmd with PATH=/dev/null" 203*10898Sroland.mainz@nrubsig.orgdone 204*10898Sroland.mainz@nrubsig.orgexp='' 205*10898Sroland.mainz@nrubsig.orggot=$(whence ./notfound) 206*10898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "whence ./$cmd failed -- expected '$exp', got '$got'" 207*10898Sroland.mainz@nrubsig.orggot=$(whence $PWD/notfound) 208*10898Sroland.mainz@nrubsig.org[[ $got == $exp ]] || err_exit "whence \$PWD/$cmd failed -- expected '$exp', got '$got'" 209*10898Sroland.mainz@nrubsig.org 2104887SchinPATH=$d: 211*10898Sroland.mainz@nrubsig.orgcp "$rm" kshrm 212*10898Sroland.mainz@nrubsig.orgif [[ $(whence kshrm) != $PWD/kshrm ]] 2134887Schinthen err_exit 'trailing : in pathname not working' 2144887Schinfi 2154887Schincp "$rm" rm 2164887SchinPATH=:$d 2174887Schinif [[ $(whence rm) != $PWD/rm ]] 2184887Schinthen err_exit 'leading : in pathname not working' 2194887Schinfi 2204887SchinPATH=$d: whence rm > /dev/null 2214887Schinif [[ $(whence rm) != $PWD/rm ]] 2224887Schinthen err_exit 'pathname not restored after scoping' 2234887Schinfi 2244887Schinmkdir bin 2254887Schinprint 'print ok' > bin/tst 2264887Schinchmod +x bin/tst 2274887Schinif [[ $(PATH=$PWD/bin tst 2>/dev/null) != ok ]] 2284887Schinthen err_exit '(PATH=$PWD/bin foo) does not find $PWD/bin/foo' 2294887Schinfi 2304887Schincd / 2314887Schinif whence ls > /dev/null 2324887Schinthen PATH= 2334887Schin if [[ $(whence rm) ]] 2344887Schin then err_exit 'setting PATH to Null not working' 2354887Schin fi 2364887Schin unset PATH 2374887Schin if [[ $(whence rm) != /*rm ]] 2384887Schin then err_exit 'unsetting path not working' 2394887Schin fi 2404887Schinfi 241*10898Sroland.mainz@nrubsig.orgPATH=/dev:$tmp 2424887Schinx=$(whence rm) 2434887Schintypeset foo=$(PATH=/xyz:/abc :) 2444887Schiny=$(whence rm) 2454887Schin[[ $x != "$y" ]] && err_exit 'PATH not restored after command substitution' 2464887Schinwhence getconf > /dev/null && err_exit 'getconf should not be found' 2474887Schinbuiltin /bin/getconf 2484887SchinPATH=/bin 2494887SchinPATH=$(getconf PATH) 2504887Schinx=$(whence ls) 2514887SchinPATH=.:$PWD:${x%/ls} 2524887Schin[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when .:$PWD in path' 2534887SchinPATH=$PWD:.:${x%/ls} 2544887Schin[[ $(whence ls) == "$x" ]] || err_exit 'PATH search bug when :$PWD:. in path' 2554887Schincd "${x%/ls}" 2564887Schin[[ $(whence ls) == /* ]] || err_exit 'whence not generating absolute pathname' 257*10898Sroland.mainz@nrubsig.orgstatus=$($SHELL -c $'trap \'print $?\' EXIT;/xxx/a/b/c/d/e 2> /dev/null') 2584887Schin[[ $status == 127 ]] || err_exit "not found command exit status $status -- expected 127" 2594887Schinstatus=$($SHELL -c $'trap \'print $?\' EXIT;/dev/null 2> /dev/null') 2604887Schin[[ $status == 126 ]] || err_exit "non executable command exit status $status -- expected 126" 261*10898Sroland.mainz@nrubsig.orgstatus=$($SHELL -c $'trap \'print $?\' ERR;/xxx/a/b/c/d/e 2> /dev/null') 2624887Schin[[ $status == 127 ]] || err_exit "not found command with ERR trap exit status $status -- expected 127" 2634887Schinstatus=$($SHELL -c $'trap \'print $?\' ERR;/dev/null 2> /dev/null') 2644887Schin[[ $status == 126 ]] || err_exit "non executable command ERR trap exit status $status -- expected 126" 2658462SApril.Chin@Sun.COM 2668462SApril.Chin@Sun.COM# universe via PATH 2678462SApril.Chin@Sun.COM 2688462SApril.Chin@Sun.COMbuiltin getconf 2698462SApril.Chin@Sun.COMgetconf UNIVERSE - att # override sticky default 'UNIVERSE = foo' 2708462SApril.Chin@Sun.COM 2718462SApril.Chin@Sun.COM[[ $(PATH=/usr/ucb/bin:/usr/bin echo -n ucb) == 'ucb' ]] || err_exit "ucb universe echo ignores -n option" 2728462SApril.Chin@Sun.COM[[ $(PATH=/usr/xpg/bin:/usr/bin echo -n att) == '-n att' ]] || err_exit "att universe echo does not ignore -n option" 2738462SApril.Chin@Sun.COM 2748462SApril.Chin@Sun.COMPATH=$path 2758462SApril.Chin@Sun.COM 276*10898Sroland.mainz@nrubsig.orgscr=$tmp/script 2778462SApril.Chin@Sun.COMexp=126 2788462SApril.Chin@Sun.COM 2798462SApril.Chin@Sun.COM: > $scr 2808462SApril.Chin@Sun.COMchmod a=x $scr 2818462SApril.Chin@Sun.COM{ got=$($scr; print $?); } 2>/dev/null 2828462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "unreadable empty script should fail -- expected $exp, got $got" 2838462SApril.Chin@Sun.COM{ got=$(command $scr; print $?); } 2>/dev/null 2848462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "command of unreadable empty script should fail -- expected $exp, got $got" 2858462SApril.Chin@Sun.COM[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable empty script in [[ ... ]] should fail -- expected $exp" 2868462SApril.Chin@Sun.COM[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable empty script in [[ ... ]] should fail -- expected $exp" 2878462SApril.Chin@Sun.COMgot=$($SHELL -c "$scr; print \$?" 2>/dev/null) 2888462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable empty script should fail -- expected $exp, got" $got 2898462SApril.Chin@Sun.COMgot=$($SHELL -c "command $scr; print \$?" 2>/dev/null) 2908462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable empty script should fail -- expected $exp, got" $got 2918462SApril.Chin@Sun.COM 2928462SApril.Chin@Sun.COMrm -f $scr 2938462SApril.Chin@Sun.COMprint : > $scr 2948462SApril.Chin@Sun.COMchmod a=x $scr 2958462SApril.Chin@Sun.COM{ got=$($scr; print $?); } 2>/dev/null 2968462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "unreadable non-empty script should fail -- expected $exp, got $got" 2978462SApril.Chin@Sun.COM{ got=$(command $scr; print $?); } 2>/dev/null 2988462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "command of unreadable non-empty script should fail -- expected $exp, got $got" 2998462SApril.Chin@Sun.COM[[ "$(:; $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "unreadable non-empty script in [[ ... ]] should fail -- expected $exp" 3008462SApril.Chin@Sun.COM[[ "$(:; command $scr; print $?)" == "$exp" ]] 2>/dev/null || err_exit "command unreadable non-empty script in [[ ... ]] should fail -- expected $exp" 3018462SApril.Chin@Sun.COMgot=$($SHELL -c "$scr; print \$?" 2>/dev/null) 3028462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of unreadable non-empty script should fail -- expected $exp, got" $got 3038462SApril.Chin@Sun.COMgot=$($SHELL -c "command $scr; print \$?" 2>/dev/null) 3048462SApril.Chin@Sun.COM[[ "$got" == "$exp" ]] || err_exit "\$SHELL -c of command of unreadable non-empty script should fail -- expected $exp, got" $got 3058462SApril.Chin@Sun.COM 3064887Schinexit $((Errors)) 307