updated: [fe95221] Rewrote the shell_escape function in order to make us of GString and g_string_append_c



The following commit has been merged in the master branch:
commit fe95221f05e3cc4a80effdcb886768a9eb77efa7
Author: Patrick Winnertz <winnie debian org>
Date:   Fri Feb 6 14:32:09 2009 +0100

    Rewrote the shell_escape function in order to make us of GString and g_string_append_c
    
    As we decided to fully switch back to glb we needed to rewrite this function in order to use
    glib functions. This means in this case mostly that *ptr = c; ptr++; is replaced by something
    like this: g_string_append(str,c); with str a GString*.
    
    Signed-off-by: Patrick Winnertz <winnie debian org>

diff --git a/src/util.c b/src/util.c
index 9924f35..96396c0 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1550,16 +1550,18 @@ Q_ (const char *s)
  string for escaping
 
  \returns
- return escaped string (later need to free)
+ return escaped string (which needs to be freed later)
  */
 char*
 shell_escape(const char* src)
 {
+	GString *str;
+	char *result = NULL;
+
 	if ((src==NULL)||(!(*src)))
 		return strdup("");
 
-	char* buffer = calloc(1, strlen(src)*2+2);
-	char* ptr = buffer;
+	str = g_string_new("");
 
 	/* look for the first char to escape */
 	while (1)
@@ -1568,19 +1570,19 @@ shell_escape(const char* src)
 		/* copy over all chars not to escape */
 		while ((c=(*src)) && shell_escape_nottoesc(c))
 		{
-			*ptr = c;
-			ptr++;
+			g_string_append_c(str,c);
 			src++;
 		}
 
 		/* at this point we either have an \0 or an char to escape */
-		if (!c)
-			return buffer;
+		if (!c) {
+			result = str->str;
+			g_string_free(str,FALSE);
+			return result;
+		}
 
-		*ptr = '\\';
-		ptr++;
-		*ptr = c;
-		ptr++;
+		g_string_append_c(str,'\\');
+		g_string_append_c(str,c);
 		src++;
 	}
 }

-- 
Midnight Commander Development


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]