Index: trunk/psLib/src/sys/psString.c
===================================================================
--- trunk/psLib/src/sys/psString.c	(revision 7766)
+++ trunk/psLib/src/sys/psString.c	(revision 7831)
@@ -13,6 +13,6 @@
  *  @author David Robbins, MHPCC
  *
- *  @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-30 02:20:06 $
+ *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-07-06 22:26:13 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -218,14 +218,35 @@
 // given the input string, search for all copies of the key, and replace with the replacement value
 // the input string may be freed if not needed
-char *psStringSubstitute(char *input, const char *replace, const char *key)
-{
-    if (key == NULL || strlen(key) == 0) {
-        return input;
-    }
-
+char *psStringSubstitute(char *input,
+                         const char *replace,
+                         const char *key)
+{
+    if (input == NULL ) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                "Invalid string in psStringSubstitute.  Input cannot be NULL or empty.\n");
+        return NULL;
+    } else if (strlen(input) == 0) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                "Invalid string in psStringSubstitute.  Input cannot be NULL or empty.\n");
+        return NULL;
+    }
+    if (replace == NULL) {
+        return NULL;
+    }
+    if (key == NULL ) {
+        return NULL;
+    } else if (strlen(key) == 0) {
+        return NULL;
+    }
+
+    psString in = psStringCopy(input);
     while (true) {
-        char *p = strstr (input, key);
+        //        char *p = strstr (input, key);
+        char *p = strstr (in, key);
         if (!p) {
-            return input;
+            //            return input;
+            //            strcpy(input, in);
+            //            psFree(in);
+            return in;
         }
 
@@ -234,9 +255,12 @@
 
         // this is safe since we will subtract strlen(key) elements from input
-        char *output = psAlloc(strlen(input) + strlen(replace) + 1);
-        int Nc = p - input;
+        //        char *output = psAlloc(strlen(input) + strlen(replace) + 1);
+        //        int Nc = p - input;
+        int Nc = p - in;
 
         // copy the first segement into 'output'
-        strncpy(output, input, Nc);
+        psString output = psStringNCopy(in, strlen(in)+strlen(replace)+1);
+
+        //        strncpy(output, input, Nc);
 
         // copy the key replacement to the start of the key
@@ -247,8 +271,18 @@
         strcpy(&output[Nc], p + strlen(key));
 
-        psFree(input);
-        input = output;
-    }
-    return input;
+        //        psFree(input);
+        //        input = output;
+        //        psFree(in);
+        //        strcpy(in, "\0");
+        //in = psStringCopy("\0");
+        psFree(in);
+        //        input = output;
+        //        strcpy(in, output);
+        in = psStringCopy(output);
+        psFree(output);
+    }
+    //    strcpy(input, in);
+    //    psFree(in);
+    return in;
 }
 
