[easytag/wip/mingw-fixes] Ugly WIP process spawn improvements



commit a4542ef81371d9281d11206c538dd63d28052fac
Author: David King <amigadave amigadave com>
Date:   Tue Feb 18 22:34:33 2014 +0000

    Ugly WIP process spawn improvements

 src/browser.c |   57 +++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 39 insertions(+), 18 deletions(-)
---
diff --git a/src/browser.c b/src/browser.c
index fc4527c..1624814 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -4286,13 +4286,14 @@ Run_Program_With_Selected_Files (GtkWidget *combobox)
 static gboolean
 Run_Program (const gchar *program_name, GList *args_list)
 {
-    gchar **argv_user;
-    gint    argv_user_number;
+    const gchar *program_args = NULL;
+    gchar **program_args_argv = { NULL };
+    guint n_program_args = 0;
+    gsize i;
     gchar  *msg;
     GPid pid;
     GError *error = NULL;
     gchar **argv;
-    gint    argv_index = 0;
     GList *l;
     gchar *program_path;
 
@@ -4333,27 +4334,47 @@ Run_Program (const gchar *program_name, GList *args_list)
         return FALSE;
     }
 
-    g_free(program_path); // Freed as never used
+    /* If user arguments are included, try to skip them. FIXME: This works
+     * poorly when there are spaces in the absolute path to the binary. */
+    {
+        size_t name_length;
+        size_t path_length;
+
+        name_length = strlen (program_name);
+        path_length = strlen (program_path);
+
+        if (name_length != path_length)
+        {
+            /* Skip the binary name and a delimiter. */
+            program_args = &program_name[path_length + 1];
+        }
+    }
+
+    g_free (program_path);
 
-    argv_user = g_strsplit(program_name," ",0); // the string may contains arguments, space is the delimiter
-    // Number of arguments into 'argv_user'
-    for (argv_user_number=0;argv_user[argv_user_number];argv_user_number++);
+    if (program_args)
+    {
+        /* FIXME: Splitting arguments based on a delimiting space is bogus if
+         * the arguments have been quoted. */
+        program_args_argv = g_strsplit (program_args, " ", 0);
+        n_program_args = g_strv_length (program_args_argv);
+    }
 
-    argv = g_new0(gchar *,argv_user_number + g_list_length(args_list) + 1); // +1 for NULL
+    /* +1 for NULL. */
+    argv = g_new0 (gchar *, n_program_args + g_list_length (args_list) + 1);
 
-    // Load 'user' arguments (program name and more...)
-    while (argv_user[argv_index])
+    for (i = 0; program_args_argv[i] != NULL; i++)
     {
-        argv[argv_index] = argv_user[argv_index];
-        argv_index++;
+        argv[i] = program_args_argv[i];
     }
-    // Load arguments from 'args_list'
-    for (l = args_list; l != NULL; l = g_list_next (l))
+
+    /* Load arguments from 'args_list'. */
+    for (l = args_list; l != NULL; l = g_list_next (l), i++)
     {
-        argv[argv_index] = (gchar *)l->data;
-        argv_index++;
+        argv[i] = (gchar *)l->data;
     }
-    argv[argv_index] = NULL;
+
+    argv[i] = NULL;
 
     /* Execution ... */
     if (g_spawn_async (NULL, argv, NULL,
@@ -4373,7 +4394,7 @@ Run_Program (const gchar *program_name, GList *args_list)
         g_clear_error (&error);
     }
 
-    g_strfreev (argv_user);
+    g_strfreev (program_args_argv);
 
     return TRUE;
 }


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