Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Zsh 3.1.{6,9} patches



Bart wrote:
> On Jul 13, 12:12pm, Peter Stephenson wrote:
> } Subject: Re: Zsh 3.1.{6,9} patches
> }
> } Would anyone object if I got rid of hostnam and made the prompt code
> } read $HOST? The only user-visible change would be that the prompt
> } reflects changes in $HOST, as it already does with $HOME.
> 
> I think that would be fine.

Here it is.  We save almost a quarter of a kilobyte, since hostnam was
always initialised to 256 bytes which were never freed.  (And I checked out
this morning's %-1m, which allows you to display your .com address in the
prompt.)

Index: Src/init.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/init.c,v
retrieving revision 1.5
diff -u -r1.5 init.c
--- Src/init.c	2000/05/27 08:32:58	1.5
+++ Src/init.c	2000/07/13 16:26:23
@@ -595,9 +595,6 @@
     gettimeofday(&shtimer, &dummy_tz);	/* init $SECONDS */
     srand((unsigned int)(shtimer.tv_sec + shtimer.tv_usec)); /* seed $RANDOM */
 
-    hostnam     = (char *) zalloc(256);
-    gethostname(hostnam, 256);
-
     /* Set default path */
     path    = (char **) zalloc(sizeof(*path) * 5);
     path[0] = ztrdup("/bin");
Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.22
diff -u -r1.22 params.c
--- Src/params.c	2000/07/04 15:04:17	1.22
+++ Src/params.c	2000/07/13 16:26:27
@@ -56,7 +56,6 @@
 /**/
 char *argzero,		/* $0           */
      *home,		/* $HOME        */
-     *hostnam,		/* $HOST        */
      *nullcmd,		/* $NULLCMD     */
      *oldpwd,		/* $OLDPWD      */
      *zoptarg,		/* $OPTARG      */
@@ -458,7 +457,7 @@
     Param ip, pm;
     char **new_environ, **envp, **envp2, **sigptr, **t;
     char **old_environ = environ;
-    char buf[50], *str, *iname;
+    char buf[50], *str, *iname, *hostnam;
     int num_env, oae = opts[ALLEXPORT];
 #ifdef HAVE_UNAME
     struct utsname unamebuf;
@@ -494,7 +493,12 @@
     setsparam("TMPPREFIX", ztrdup(DEFAULT_TMPPREFIX));
     setsparam("TIMEFMT", ztrdup(DEFAULT_TIMEFMT));
     setsparam("WATCHFMT", ztrdup(default_watchfmt));
+
+    hostnam = (char *)zalloc(256);
+    gethostname(hostnam, 256);
     setsparam("HOST", ztrdup(hostnam));
+    zfree(hostnam, 256);
+
     setsparam("LOGNAME", ztrdup((str = getlogin()) && *str ? str : cached_username));
 
     /* Copy the environment variables we are inheriting to dynamic *
Index: Src/prompt.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/prompt.c,v
retrieving revision 1.2
diff -u -r1.2 prompt.c
--- Src/prompt.c	2000/07/13 11:20:46	1.2
+++ Src/prompt.c	2000/07/13 16:26:30
@@ -201,7 +201,7 @@
 static int
 putpromptchar(int doprint, int endchar)
 {
-    char *ss, *tmbuf = NULL;
+    char *ss, *tmbuf = NULL, *hostnam;
     int t0, arg, test, sep;
     struct tm *tm;
     time_t timet;
@@ -372,11 +372,14 @@
 		bp += strlen(bp);
 		break;
 	    case 'M':
-		stradd(hostnam);
+		if ((hostnam = getsparam("HOST")))
+		    stradd(hostnam);
 		break;
 	    case 'm':
 		if (!arg)
 		    arg++;
+		if (!(hostnam = getsparam("HOST")))
+		    break;
 		if (arg < 0) {
 		    for (ss = hostnam + strlen(hostnam); ss > hostnam; ss--)
 			if (ss[-1] == '.' && !++arg)

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxxxxxxxxxx>
Cambridge Silicon Radio, Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070



Messages sorted by: Reverse Date, Date, Thread, Author