1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +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
	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

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: