1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-15 04:32:24 +00:00

Make ~ expand to home directory after unsetting HOME

There was an issue with tilde expansion if the HOME var is unset.

	$ unset HOME
	$ echo ~
	martijn

Only the username is returned. Users are more likely to expect the
current user's home directory as configured in the OS.

POSIXly, the expansion of ~ is based on the value of HOME. If HOME
is unset, the results are unspecified. After unsetting HOME, in
bash, ~ returns the user's home directory as specified by the OS,
whereas in all other shells, ~ expands to the empty string. Only
ksh93 returns the username. The behaviour of bash is more useful.

Discussion:
https://github.com/ksh93/ksh/pull/225#issuecomment-799074107

src/cmd/ksh93/sh/macro.c,
src/cmd/ksh93/tests/tilde.sh:
- sh_tilde(): Backport fix by Mike Gilbert from ksh2020.
  See:	https://github.com/att/ast/issues/1391
	https://github.com/att/ast/pull/1396
	https://github.com/att/ast/commit/070d365d
- Add test.

src/cmd/ksh93/COMPATIBILITY:
- Note this change.
This commit is contained in:
Martijn Dekker 2021-03-15 21:49:02 +00:00
parent ef4fe4106c
commit 1df6a82a8a
5 changed files with 26 additions and 6 deletions

5
NEWS
View file

@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh
Any uppercase BUG_* names are modernish shell bug IDs.
2021-03-15:
- If the HOME variable is unset, the bare tilde ~ now expands to the current
user's system-configured home directory instead of merely the username.
2021-03-13:
- Fixed a file descriptor leak that occurred when ksh used /dev/fd for

View file

@ -104,6 +104,9 @@ For more details, see the NEWS file and for complete details, see the git log.
For example, the following no longer produces an unexpected error:
$ ksh -o nounset -c 'float n; [[ -v n ]] && echo $n'
20. If the HOME variable is unset, the bare tilde ~ now expands to the
current user's system home directory instead of merely the username.
____________________________________________________________________________
KSH-93 VS. KSH-88

View file

@ -20,7 +20,7 @@
#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
#define SH_RELEASE_SVER "1.0.0-alpha" /* semantic version number: https://semver.org */
#define SH_RELEASE_DATE "2021-03-13" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_DATE "2021-03-15" /* must be in this format for $((.sh.version)) */
#define SH_RELEASE_CPYR "(c) 2020-2021 Contributors to ksh " SH_RELEASE_FORK
/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */

View file

@ -2692,7 +2692,7 @@ static char *sh_tilde(Shell_t *shp,register const char *string)
{
register char *cp;
register int c;
register struct passwd *pw;
register struct passwd *pw = NIL(struct passwd*);
register Namval_t *np=0;
unsigned int save;
static Dt_t *logins_tree;
@ -2700,9 +2700,13 @@ static char *sh_tilde(Shell_t *shp,register const char *string)
return(NIL(char*));
if((c = *string)==0)
{
if(!(cp=nv_getval(sh_scoped(shp,HOME))))
cp = getlogin();
return(cp);
static char *username;
if(cp = nv_getval(sh_scoped(shp, HOME)))
return(cp);
/* Fallback for unset HOME: get username and treat ~ like ~username */
if(!username && !((pw = getpwuid(getuid())) && (username = sh_strdup(pw->pw_name))))
return(NIL(char*));
string = username;
}
if((c=='-' || c=='+') && string[1]==0)
{
@ -2741,7 +2745,7 @@ static char *sh_tilde(Shell_t *shp,register const char *string)
#endif /* _WINIX */
if(logins_tree && (np=nv_search(string,logins_tree,0)))
return(nv_getval(np));
if(!(pw = getpwnam(string)))
if(!pw && !(pw = getpwnam(string)))
return(NIL(char*));
#if _WINIX
skip:

View file

@ -87,4 +87,12 @@ chmod +x $tmp/tilde
nl=$'\n'
[[ $($tmp/tilde foo) == "$PWD$nl$PWD" ]] 2> /dev/null || err_exit 'tilde fails inside a script run by name'
# ======
# After unsetting HOME, ~ should expand to the current user's OS-configured home directory.
unset HOME
got=~
[[ $got == /* && -d $got ]] || err_exit "expansion of bare tilde breaks after unsetting HOME (value: $(printf %q "$got"))"
HOME=$tmp
# ======
exit $((Errors<125?Errors:125))