changeset 1711:4d552a89ceaa

[project @ 1996-01-08 01:12:38 by jwe]
author jwe
date Mon, 08 Jan 1996 01:13:57 +0000
parents 43d4a1a25dd2
children 26a5b528968c
files src/octave.cc src/sysdep.cc src/utils.cc
diffstat 3 files changed, 92 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/octave.cc	Mon Jan 08 01:02:20 1996 +0000
+++ b/src/octave.cc	Mon Jan 08 01:13:57 1996 +0000
@@ -197,20 +197,20 @@
   char *odb = getenv ("OCTAVE_DB_DIR");
 
   if (odb)
-    putenv (strconcat ("TEXMF=", odb));
+    oct_putenv ("TEXMF", odb);
   else
     {
       char *oh = getenv ("OCTAVE_HOME");
 
       if (oh)
 	{
-	  int len = strlen (oh) + 18;
-	  char *putenv_cmd = new char [len];
-	  sprintf (putenv_cmd, "TEXMF=%s/lib/octave", oh);
-	  putenv (putenv_cmd);
+	  int len = strlen (oh) + 12;
+	  char *putenv_val = new char [len];
+	  sprintf (putenv_val, "%s/lib/octave", oh);
+	  oct_putenv ("TEXMF", putenv_val);
 	}
       else  
-	putenv (strsave ("TEXMF=" OCTAVE_DATADIR "/octave"));
+	oct_putenv ("TEXMF", OCTAVE_DATADIR "/octave");
     }
 
   exec_path = default_exec_path ();
--- a/src/sysdep.cc	Mon Jan 08 01:02:20 1996 +0000
+++ b/src/sysdep.cc	Mon Jan 08 01:13:57 1996 +0000
@@ -558,9 +558,6 @@
   return retval;
 }
 
-// XXX FIXME XXX -- this should be smart, like the xputenv function in
-// the kpathsea library.
-
 DEFUN ("putenv", Fputenv, Sputenv, 10,
   "putenv (VAR, VALUE): define environment variable VAR=VALUE")
 {
@@ -577,15 +574,7 @@
 	  const char *val = args(1).string_value (); 
 
 	  if (! error_state)
-	    {
-	      int buflen = strlen (var) + strlen (val) + 2;
-
-	      char *buf = new char [buflen];
-
-	      sprintf (buf, "%s=%s", var, val);
-
-	      putenv (buf);
-	    }
+	    oct_putenv (var, val);
 	  else
 	    error ("putenv: second argument should be a string");
 	}
--- a/src/utils.cc	Mon Jan 08 01:02:20 1996 +0000
+++ b/src/utils.cc	Mon Jan 08 01:13:57 1996 +0000
@@ -808,6 +808,91 @@
   return retval;
 }
 
+// This function was adapted from xputenv from Karl Berry's kpathsearch
+// library.
+
+void
+oct_putenv (const char *var_name, const char *value)
+{
+  static const char **saved_env_items = 0;
+  static unsigned saved_len;
+  char *old_item = 0;
+
+  int new_len = strlen (var_name) + strlen (value) + 2;
+
+  char *new_item = new char [new_len];
+
+  sprintf (new_item, "%s=%s", var_name, value);
+
+#ifndef SMART_PUTENV
+
+  // Check if we have saved anything yet.
+
+  if (! saved_env_items)
+    {
+      saved_env_items = new const char * [1];
+      saved_env_items[0] = var_name;
+      saved_len = 1;
+    }
+  else
+    {
+      // Check if we've assigned VAR_NAME before.
+
+      unsigned len = strlen (var_name);
+
+      for (unsigned i = 0; i < saved_len && ! old_item; i++)
+        {
+          if (strcmp (saved_env_items[i], var_name) == 0)
+            {
+              old_item = getenv (var_name);
+
+              assert (old_item);
+
+              // Back up to the `NAME=' in the environment before the
+	      // value that getenv returns.
+
+              old_item -= (len + 1);
+            }
+        }
+      
+      if (! old_item)
+        {
+          // If we haven't seen VAR_NAME before, save it.  Assume it
+	  // is in safe storage.
+
+          saved_len++;
+
+	  const char **tmp = new const char * [saved_len];
+
+	  for (unsigned i = 0; i < saved_len - 1; i++)
+	    tmp[i] = saved_env_items[i];
+
+	  tmp[saved_len - 1] = var_name;
+
+          delete [] saved_env_items;
+
+	  saved_env_items = tmp;
+        }
+    }
+
+#endif
+
+  // As far as I can see there's no way to distinguish between the
+  // various errors; putenv doesn't have errno values.
+
+  if (putenv (new_item) < 0)
+    error ("putenv (%s) failed", new_item);
+  
+#ifndef SMART_PUTENV
+
+  // Can't free `new_item' because its contained value is now in
+  // `environ', but we can free `old_item', since it's been replaced.
+
+  delete [] old_item;
+
+#endif
+}
+
 /*
 ;;; Local Variables: ***
 ;;; mode: C++ ***