xref: /titanic_41/usr/src/lib/libshell/common/tests/tilde.sh (revision 1816cb7076d3ec8a78ef9ac9f895574e13c43645)
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: "$@"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28function home # id
29{
30	typeset IFS=: pwd=/etc/passwd
31	set -o noglob
32	if	[[ -f $pwd ]] && grep -c "^$1:" $pwd > /dev/null
33	then	set -- $(grep "^$1:" $pwd)
34		print -r -- "$6"
35	else	print .
36	fi
37}
38
39Command=${0##*/}
40integer Errors=0
41OLDPWD=/bin
42if	[[ ~ != $HOME ]]
43then	err_exit '~' not $HOME
44fi
45x=~
46if	[[ $x != $HOME ]]
47then	err_exit x=~ not $HOME
48fi
49x=x:~
50if	[[ $x != x:$HOME ]]
51then	err_exit x=x:~ not x:$HOME
52fi
53if	[[ ~+ != $PWD ]]
54then	err_exit '~' not $PWD
55fi
56x=~+
57if	[[ $x != $PWD ]]
58then	err_exit x=~+ not $PWD
59fi
60if	[[ ~- != $OLDPWD ]]
61then	err_exit '~' not $PWD
62fi
63x=~-
64if	[[ $x != $OLDPWD ]]
65then	err_exit x=~- not $OLDPWD
66fi
67for u in root Administrator
68do	h=$(home $u)
69	if	[[ $h != . ]]
70	then	[[ ~$u -ef $h ]] || err_exit "~$u not $h"
71		x=~$u
72		[[ $x -ef $h ]] || "x=~$u not $h"
73		break
74	fi
75done
76x=~%%
77if	[[ $x != '~%%' ]]
78then	err_exit 'x='~%%' not '~%%
79fi
80x=~:~
81if	[[ $x != "$HOME:$HOME" ]]
82then	err_exit x=~:~ not $HOME:$HOME
83fi
84HOME=/
85[[ ~ == / ]] || err_exit '~ should be /'
86trap 'rm -rf /tmp/kshtilde$$' EXIT
87[[ ~/foo == /foo ]] || err_exit '~/foo should be /foo when ~==/'
88print $'print ~+\n[[ $1 ]] && $0' > /tmp/kshtilde$$
89chmod +x /tmp/kshtilde$$
90nl=$'\n'
91[[ $(/tmp/kshtilde$$ foo) == "$PWD$nl$PWD" ]] 2> /dev/null  || err_exit 'tilde fails inside a script run by name'
92exit $((Errors))
93