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