xref: /titanic_50/usr/src/lib/libshell/common/tests/exit.sh (revision 15d9d0b528387242011cdcc6190c9e598cfe3a07)
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
28function abspath
29{
30        base=$(basename $SHELL)
31        cd ${SHELL%/$base}
32        newdir=$(pwd)
33        cd ~-
34        print $newdir/$base
35}
36#test for proper exit of shell
37Command=${0##*/}
38integer Errors=0
39builtin getconf
40ABSHELL=$(abspath)
41mkdir /tmp/ksh$$ || err_exit "mkdir /tmp/ksh$$ failed"
42cd /tmp/ksh$$ || err_exit "cd /tmp/ksh$$ failed"
43print exit 0 >.profile
44${ABSHELL}  <<!
45HOME=$PWD \
46PATH=$PATH \
47SHELL=$ABSSHELL \
48$(
49	set --noglob
50	ifs=$IFS
51	IFS=,
52	set -- $(getconf LIBPATH)
53	IFS=$ifs
54	for v
55	do	IFS=:
56		set -- $v
57		IFS=$ifs
58		eval [[ \$$2 ]] && eval print -n \" \"\$2=\"\$$2\"
59	done
60) \
61exec -c -a -ksh ${ABSHELL} -c "exit 1" 1>/dev/null 2>&1
62!
63status=$(echo $?)
64if	[[ -o noprivileged && $status != 0 ]]
65then	err_exit 'exit in .profile is ignored'
66elif	[[ -o privileged && $status == 0 ]]
67then	err_exit 'privileged .profile not ignored'
68fi
69if	[[ $(trap 'code=$?; echo $code; trap 0; exit $code' 0; exit 123) != 123 ]]
70then	err_exit 'exit not setting $?'
71fi
72cat > run.sh <<- "EOF"
73	trap 'code=$?; echo $code; trap 0; exit $code' 0
74	( trap 0; exit 123 )
75EOF
76if	[[ $($SHELL ./run.sh) != 123 ]]
77then	err_exit 'subshell trap on exit overwrites parent trap'
78fi
79cd ~- || err_exit "cd back failed"
80rm -r /tmp/ksh$$ || err_exit "rm -r /tmp/ksh$$ failed"
81exit $((Errors))
82