zsh-workers
 help / color / mirror / code / Atom feed
* Is a pristine shell allowed to set HOME? (fwd)
@ 2006-02-05 20:44 Bart Schaefer
  2006-02-06 11:17 ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2006-02-05 20:44 UTC (permalink / raw)
  To: zsh-workers

Zsh seems to be the odd man out, here.

---------- Forwarded message ----------
Date: Sun, 5 Feb 2006 20:14:07 +0100
From: Jens Schweikhardt <schweikh@schweikhardt.net>
To: austin-group-l@opengroup.org
Subject: Is a pristine shell allowed to set HOME?
Resent-Date: 5 Feb 2006 19:15:00 -0000
Resent-From: austin-group-l@opengroup.org
Resent-To: austin-group-l@opengroup.org

hello, world\n

I'm currently pondering the question, if a pristine shell, i.e one
started with "env -i /path/to/shell" is allowed to set (initialize)
HOME. 2.5.3 on shell variables states,

   HOME
    The pathname of the user's home directory. The contents of HOME are
    used in tilde expansion (see Tilde Expansion).

For three other variables, the standard explicitly says "set by the shell",

   LINENO
    Set by the shell to a decimal number...
   PPID
    Set by the shell to the decimal process ID...
   PWD
    Set by the shell to be an absolute pathname...

But it does not explicitly say so for HOME. Different shells behave
differently, e.g on FreeBSD

	# env -i /bin/sh -c set | grep ^HOME=
	# env -i /usr/local/bin/bash -c set | grep ^HOME=
	# env -i /usr/local/bin/zsh -c set|grep ^HOME=
	HOME=/root
	# env -i /usr/local/bin/ksh93 -c set|grep ^HOME=
	# env -i /usr/local/bin/pdksh -c set|grep ^HOME=
	# env -i /bin/csh -c 'echo $HOME'   # = tcsh
	HOME: Undefined variable.

Is it okay for a shell to initialize HOME if it does not come in from
the inherited environment?

Regards,

	Jens
-- 
Jens Schweikhardt http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is a pristine shell allowed to set HOME? (fwd)
  2006-02-05 20:44 Is a pristine shell allowed to set HOME? (fwd) Bart Schaefer
@ 2006-02-06 11:17 ` Peter Stephenson
  2006-02-06 18:51   ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2006-02-06 11:17 UTC (permalink / raw)
  To: zsh-workers

Bart Schaefer wrote:
> Zsh seems to be the odd man out, here.
> 
> ---------- Forwarded message ----------
> Date: Sun, 5 Feb 2006 20:14:07 +0100
> From: Jens Schweikhardt <schweikh@schweikhardt.net>
> To: austin-group-l@opengroup.org
> Subject: Is a pristine shell allowed to set HOME?
> 	# env -i /bin/sh -c set | grep ^HOME=
> 	# env -i /usr/local/bin/bash -c set | grep ^HOME=
> 	# env -i /usr/local/bin/zsh -c set|grep ^HOME=
> 	HOME=/root
> 	# env -i /usr/local/bin/ksh93 -c set|grep ^HOME=
> 	# env -i /usr/local/bin/pdksh -c set|grep ^HOME=
> 	# env -i /bin/csh -c 'echo $HOME'   # = tcsh
> 	HOME: Undefined variable.

Probably better to be compatible here... hope this doesn't break
anything, but it's a very unusual case.

Index: README
===================================================================
RCS file: /cvsroot/zsh/zsh/README,v
retrieving revision 1.23
diff -u -r1.23 README
--- README	3 Oct 2005 09:00:45 -0000	1.23
+++ README	6 Feb 2006 11:13:29 -0000
@@ -31,6 +31,9 @@
 handling of other arguments).  This appears to be the standard shell
 behaviour.
 
+The variable HOME is no longer set by the shell; it must be present
+in the environment.  It is valid for the variable to be unset.
+
 Documentation
 -------------
 
Index: Src/init.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/init.c,v
retrieving revision 1.61
diff -u -r1.61 init.c
--- Src/init.c	3 Dec 2005 00:14:06 -0000	1.61
+++ Src/init.c	6 Feb 2006 11:13:30 -0000
@@ -795,22 +795,23 @@
      * recheck the info for `USERNAME'     */
     cached_uid = getuid();
 
-    /* Get password entry and set info for `HOME' and `USERNAME' */
+    /* Get password entry and set info for `USERNAME' */
 #ifdef HAVE_GETPWUID
     if ((pswd = getpwuid(cached_uid))) {
-	home = metafy(pswd->pw_dir, -1, META_DUP);
 	cached_username = ztrdup(pswd->pw_name);
     } else
 #endif /* HAVE_GETPWUID */
 	   {
-	home = ztrdup("/");
 	cached_username = ztrdup("");
     }
 
-    /* Try a cheap test to see if we can *
-     * initialize `PWD' from `HOME'      */
-    if (ispwd(home))
-	pwd = ztrdup(home);
+    /*
+     * Try a cheap test to see if we can initialize `PWD' from `HOME'.
+     * HOME must come from the environment; we're not allowed to
+     * set it locally.
+     */
+    if ((ptr = getenv("HOME")) && ispwd(ptr))
+	pwd = ztrdup(ptr);
     else if ((ptr = zgetenv("PWD")) && (strlen(ptr) < PATH_MAX) &&
 	     (ptr = metafy(ptr, -1, META_STATIC), ispwd(ptr)))
 	pwd = ztrdup(ptr);
@@ -1105,8 +1106,11 @@
 
     queue_signals();
     if (emulation == EMULATE_SH || emulation == EMULATE_KSH ||
-	!(h = getsparam("ZDOTDIR")))
+	!(h = getsparam("ZDOTDIR"))) {
 	h = home;
+	if (!h)
+	    return;
+    }
 
     {
 	/* Let source() complain if path is too long */
Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.107
diff -u -r1.107 params.c
--- Src/params.c	7 Nov 2005 09:37:34 -0000	1.107
+++ Src/params.c	6 Feb 2006 11:13:31 -0000
@@ -257,7 +257,7 @@
 IPDEF2("USERNAME", username_gsu, PM_DONTIMPORT|PM_RESTRICTED),
 IPDEF2("-", dash_gsu, PM_READONLY),
 IPDEF2("histchars", histchars_gsu, PM_DONTIMPORT),
-IPDEF2("HOME", home_gsu, 0),
+IPDEF2("HOME", home_gsu, PM_UNSET),
 IPDEF2("TERM", term_gsu, 0),
 IPDEF2("WORDCHARS", wordchars_gsu, 0),
 IPDEF2("IFS", ifs_gsu, PM_DONTIMPORT),
@@ -690,9 +690,6 @@
     *envp = '\0';
     opts[ALLEXPORT] = oae;
 
-    pm = (Param) paramtab->getnode(paramtab, "HOME");
-    if (!(pm->flags & PM_EXPORTED))
-	addenv(pm, home);
     pm = (Param) paramtab->getnode(paramtab, "LOGNAME");
     if (!(pm->flags & PM_EXPORTED))
 	addenv(pm, pm->u.str);
Index: Src/subst.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/subst.c,v
retrieving revision 1.43
diff -u -r1.43 subst.c
--- Src/subst.c	1 Nov 2005 18:04:25 -0000	1.43
+++ Src/subst.c	6 Feb 2006 11:13:32 -0000
@@ -417,7 +417,7 @@
 
 	val = zstrtol(str + 1, &ptr, 10);
 	if (isend(str[1])) {   /* ~ */
-	    *namptr = dyncat(home, str + 1);
+	    *namptr = dyncat(home ? home : "", str + 1);
 	    return 1;
 	} else if (str[1] == '+' && isend(str[2])) {   /* ~+ */
 	    *namptr = dyncat(pwd, str + 2);
Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.115
diff -u -r1.115 utils.c
--- Src/utils.c	13 Jan 2006 17:13:49 -0000	1.115
+++ Src/utils.c	6 Feb 2006 11:13:34 -0000
@@ -630,8 +630,8 @@
      * whenever a node is added to or removed from the hash table, and *
      * whenever the value of $HOME changes.  (On startup, too.)        */
     if (!s) {
-	homenode.dir = home;
-	homenode.diff = strlen(home);
+	homenode.dir = home ? home : "";
+	homenode.diff = home ? strlen(home) : 0;
 	if(homenode.diff==1)
 	    homenode.diff = 0;
 	if(!finddir_full)
Index: Src/Modules/newuser.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Modules/newuser.c,v
retrieving revision 1.3
diff -u -r1.3 newuser.c
--- Src/Modules/newuser.c	10 Dec 2005 00:27:11 -0000	1.3
+++ Src/Modules/newuser.c	6 Feb 2006 11:13:34 -0000
@@ -67,8 +67,11 @@
     if (emulation != EMULATE_ZSH)
 	return 0;
 
-    if (!dotdir)
+    if (!dotdir) {
 	dotdir = home;
+	if (!dotdir)
+	    return;
+    }
 
     if (check_dotfile(dotdir, ".zshenv") == 0 ||
 	check_dotfile(dotdir, ".zprofile") == 0 ||

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is a pristine shell allowed to set HOME? (fwd)
  2006-02-06 11:17 ` Peter Stephenson
@ 2006-02-06 18:51   ` Bart Schaefer
  2006-02-07 11:21     ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2006-02-06 18:51 UTC (permalink / raw)
  To: zsh-workers

On Feb 6, 11:17am, Peter Stephenson wrote:
} Subject: Re: Is a pristine shell allowed to set HOME? (fwd)
}
} Bart Schaefer wrote:
} > Zsh seems to be the odd man out, here.
} 
} Probably better to be compatible here... hope this doesn't break
} anything, but it's a very unusual case.

I was thinking perhaps it should continue to set HOME when in "zsh
emulation" but not when in any other shell emulations.  I should have
said so before, but I was in a hurry when I was forwarding that ...


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is a pristine shell allowed to set HOME? (fwd)
  2006-02-06 18:51   ` Bart Schaefer
@ 2006-02-07 11:21     ` Peter Stephenson
  2006-02-08  2:10       ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2006-02-07 11:21 UTC (permalink / raw)
  To: Zsh hackers list

Bart Schaefer wrote:
> I was thinking perhaps it should continue to set HOME when in "zsh
> emulation" but not when in any other shell emulations.  I should have
> said so before, but I was in a hurry when I was forwarding that ...

I was hoping to avoid too much complexity, but I suppose you're right.

Index: README
===================================================================
RCS file: /cvsroot/zsh/zsh/README,v
retrieving revision 1.25
diff -u -r1.25 README
--- README	6 Feb 2006 12:05:32 -0000	1.25
+++ README	7 Feb 2006 11:10:20 -0000
@@ -31,8 +31,9 @@
 handling of other arguments).  This appears to be the standard shell
 behaviour.
 
-The variable HOME is no longer set by the shell; it must be present
-in the environment.  It is valid for the variable to be unset.
+The variable HOME is no longer set by the shell if zsh is emulating any
+other shell at startup; it must be present in the environment or set
+subsequently by the user.  It is valid for the variable to be unset.
 
 Zsh has previously been lax about whether it allows octets with the
 top bit set to be part of a shell identifier.  With --enable-multibyte set,
Index: Src/init.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/init.c,v
retrieving revision 1.62
diff -u -r1.62 init.c
--- Src/init.c	6 Feb 2006 11:57:05 -0000	1.62
+++ Src/init.c	7 Feb 2006 11:10:21 -0000
@@ -798,19 +798,28 @@
     /* Get password entry and set info for `USERNAME' */
 #ifdef HAVE_GETPWUID
     if ((pswd = getpwuid(cached_uid))) {
+	if (emulation == EMULATE_ZSH)
+	    home = metafy(pswd->pw_dir, -1, META_DUP);
 	cached_username = ztrdup(pswd->pw_name);
-    } else
+    }
+    else
 #endif /* HAVE_GETPWUID */
-	   {
+    {
+	if (emulation == EMULATE_ZSH)
+	    home = ztrdup("/");
 	cached_username = ztrdup("");
     }
 
     /*
      * Try a cheap test to see if we can initialize `PWD' from `HOME'.
-     * HOME must come from the environment; we're not allowed to
-     * set it locally.
+     * In non-native emulations HOME must come from the environment;
+     * we're not allowed to set it locally.
      */
-    if ((ptr = getenv("HOME")) && ispwd(ptr))
+    if (emulation == EMULATE_ZSH)
+	ptr = home;
+    else
+	ptr = getenv("HOME");
+    if (ptr && ispwd(ptr))
 	pwd = ztrdup(ptr);
     else if ((ptr = zgetenv("PWD")) && (strlen(ptr) < PATH_MAX) &&
 	     (ptr = metafy(ptr, -1, META_STATIC), ispwd(ptr)))
Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.108
diff -u -r1.108 params.c
--- Src/params.c	6 Feb 2006 11:57:06 -0000	1.108
+++ Src/params.c	7 Feb 2006 11:10:22 -0000
@@ -690,6 +690,17 @@
     *envp = '\0';
     opts[ALLEXPORT] = oae;
 
+    if (emulation == EMULATE_ZSH)
+    {
+	/*
+	 * For native emulation we always set the variable home
+	 * (see setupvals()).
+	 */
+	pm = (Param) paramtab->getnode(paramtab, "HOME");
+	pm->flags &= ~PM_UNSET;
+	if (!(pm->flags & PM_EXPORTED))
+	    addenv(pm, home);
+    }
     pm = (Param) paramtab->getnode(paramtab, "LOGNAME");
     if (!(pm->flags & PM_EXPORTED))
 	addenv(pm, pm->u.str);
Index: Doc/Zsh/params.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/params.yo,v
retrieving revision 1.27
diff -u -r1.27 params.yo
--- Doc/Zsh/params.yo	7 Nov 2005 09:37:34 -0000	1.27
+++ Doc/Zsh/params.yo	7 Feb 2006 11:14:21 -0000
@@ -805,7 +805,10 @@
 )
 vindex(HOME)
 item(tt(HOME) <S>)(
-The default argument for the tt(cd) command.
+The default argument for the tt(cd) command.  This is not set automatically
+by the shell in tt(sh), tt(ksh) or tt(csh) emulation, but it is typically
+present in the environment anyway, and if it becomes set it has its usual
+special behaviour.
 )
 vindex(IFS)
 item(tt(IFS) <S>)(

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is a pristine shell allowed to set HOME? (fwd)
  2006-02-07 11:21     ` Peter Stephenson
@ 2006-02-08  2:10       ` Bart Schaefer
  2006-02-08 10:23         ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2006-02-08  2:10 UTC (permalink / raw)
  To: Zsh hackers list

On Feb 7, 11:21am, Peter Stephenson wrote:
}
} I was hoping to avoid too much complexity, but I suppose you're right.

Was anything but the part in params.c (*) necessary?  Why would it
hurt to set the internal "home" variable, but ignore it when not in
zsh emulation?

(*) and the doc, of course


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is a pristine shell allowed to set HOME? (fwd)
  2006-02-08  2:10       ` Bart Schaefer
@ 2006-02-08 10:23         ` Peter Stephenson
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Stephenson @ 2006-02-08 10:23 UTC (permalink / raw)
  To: Zsh hackers list

Bart Schaefer wrote:
> Was anything but the part in params.c (*) necessary?  Why would it
> hurt to set the internal "home" variable, but ignore it when not in
> zsh emulation?

The internal code uses "home" without reference to the actual parameter
(in particular, whether it's actually set), so it would appear in some
places as if $HOME was set after all.  Or, to put it another way, the
catch is in the "ignore it when not in zsh emulation"---that's
significantly more complicated than not setting it in the first places
as well as being less consistent.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-02-08 10:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-02-05 20:44 Is a pristine shell allowed to set HOME? (fwd) Bart Schaefer
2006-02-06 11:17 ` Peter Stephenson
2006-02-06 18:51   ` Bart Schaefer
2006-02-07 11:21     ` Peter Stephenson
2006-02-08  2:10       ` Bart Schaefer
2006-02-08 10:23         ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).