From: Jiri Kalvoda Date: Sat, 25 Feb 2023 23:51:04 +0000 (+0100) Subject: Fix expansion in cmd X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=HEAD;p=teatimer.git Fix expansion in cmd Command is expanded after parsing to argument list array, so name couldn't split to more arguments. --- diff --git a/teatimer.c b/teatimer.c index b0e3b5c..c58f789 100644 --- a/teatimer.c +++ b/teatimer.c @@ -58,42 +58,50 @@ quit(void) static int // return pid of new process or 0 if failed expand_and_exec(char *cmd) { - GString *expanded_cmd = g_string_new(""); - if (!expanded_cmd) - return 0; - for (int i=0; cmd[i]; i++) - { - if (cmd[i]=='%' && cmd[i+1]=='%') - { - i++; - g_string_append_c(expanded_cmd, '%'); - } - else - if (cmd[i]=='%' && cmd[i+1]=='n') - { - i++; - const gchar *name = gtk_entry_get_text(GTK_ENTRY(namebox)); - g_string_append(expanded_cmd, name); - } - else - g_string_append_c(expanded_cmd, cmd[i]); - } - GError *err = NULL; gint argc; gchar ** argv = NULL; int pid; - g_shell_parse_argv(expanded_cmd->str, &argc, &argv, &err); - g_string_free(expanded_cmd, 1); + g_shell_parse_argv(cmd, &argc, &argv, &err); + GString ** expanded_argv = malloc(sizeof(expanded_argv[0])*argc); + if (!expanded_argv) + return 0; + gchar ** expanded_argv_gchar = malloc(sizeof(expanded_argv_gchar[0])*(argc+1)); if (err) { - fprintf(stderr, "teatimer: Unable to run command: %s\n", err->message); + fprintf(stderr, "teatimer: Unable to parse command: %s\n", err->message); g_error_free(err); return 0; } - g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &pid, &err); + if(!expanded_argv_gchar) return 0; + for (int i=0; istr; + } + expanded_argv_gchar[argc]=NULL; + g_spawn_async(NULL, expanded_argv_gchar, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &pid, &err); g_strfreev(argv); + for (int i=0; imessage);