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 <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
28Command=${0##*/}
29integer Errors=0
30
31tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32trap "cd /; rm -rf $tmp" EXIT
33
34if	$SHELL -c '[[ ~root == /* ]]'
35then	x=$(print -r -- ~root)
36	[[ $x == ~root ]] || err_exit '~user expanded in subshell prevent ~user from working'
37fi
38
39function home # id
40{
41	typeset IFS=: pwd=/etc/passwd
42	set -o noglob
43	if	[[ -f $pwd ]] && grep -c "^$1:" $pwd > /dev/null
44	then	set -- $(grep "^$1:" $pwd)
45		print -r -- "$6"
46	else	print .
47	fi
48}
49
50OLDPWD=/bin
51if	[[ ~ != $HOME ]]
52then	err_exit '~' not $HOME
53fi
54x=~
55if	[[ $x != $HOME ]]
56then	err_exit x=~ not $HOME
57fi
58x=x:~
59if	[[ $x != x:$HOME ]]
60then	err_exit x=x:~ not x:$HOME
61fi
62if	[[ ~+ != $PWD ]]
63then	err_exit '~' not $PWD
64fi
65x=~+
66if	[[ $x != $PWD ]]
67then	err_exit x=~+ not $PWD
68fi
69if	[[ ~- != $OLDPWD ]]
70then	err_exit '~' not $PWD
71fi
72x=~-
73if	[[ $x != $OLDPWD ]]
74then	err_exit x=~- not $OLDPWD
75fi
76for u in root Administrator
77do	h=$(home $u)
78	if	[[ $h != . ]]
79	then	[[ ~$u -ef $h ]] || err_exit "~$u not $h"
80		x=~$u
81		[[ $x -ef $h ]] || x="~$u not $h"
82		break
83	fi
84done
85x=~g.r.emlin
86if	[[ $x != '~g.r.emlin' ]]
87then	err_exit "x=~g.r.emlin failed -- expected '~g.r.emlin', got '$x'"
88fi
89x=~:~
90if	[[ $x != "$HOME:$HOME" ]]
91then	err_exit "x=~:~ failed, expected '$HOME:$HOME', got '$x'"
92fi
93HOME=/
94[[ ~ == / ]] || err_exit '~ should be /'
95[[ ~/foo == /foo ]] || err_exit '~/foo should be /foo when ~==/'
96print $'print ~+\n[[ $1 ]] && $0' > $tmp/tilde
97chmod +x $tmp/tilde
98nl=$'\n'
99[[ $($tmp/tilde foo) == "$PWD$nl$PWD" ]] 2> /dev/null  || err_exit 'tilde fails inside a script run by name'
100
101exit $((Errors<125?Errors:125))
102