Skip to content
Snippets Groups Projects
Commit 0ed48764 authored by Alexander Turenko's avatar Alexander Turenko Committed by Kirill Yukhin
Browse files

popen: quote multiword command arguments


Of course it is still not fair shell-style quoting: at least we should
also escape quotes inside arguments. But it gives correct output for
most of typical commands and has straightforward implementation.

Part of #4031

Acked-by: default avatarCyrill Gorcunov <gorcunov@gmail.com>
parent c66617e3
No related branches found
No related tags found
Loading
...@@ -154,7 +154,7 @@ handle_new(struct popen_opts *opts) ...@@ -154,7 +154,7 @@ handle_new(struct popen_opts *opts)
for (i = 0; i < opts->nr_argv; i++) { for (i = 0; i < opts->nr_argv; i++) {
if (opts->argv[i] == NULL) if (opts->argv[i] == NULL)
continue; continue;
size += strlen(opts->argv[i]) + 1; size += strlen(opts->argv[i]) + 3;
} }
handle = malloc(sizeof(*handle) + size); handle = malloc(sizeof(*handle) + size);
...@@ -168,8 +168,13 @@ handle_new(struct popen_opts *opts) ...@@ -168,8 +168,13 @@ handle_new(struct popen_opts *opts)
for (i = 0; i < opts->nr_argv-1; i++) { for (i = 0; i < opts->nr_argv-1; i++) {
if (opts->argv[i] == NULL) if (opts->argv[i] == NULL)
continue; continue;
bool is_multiword = strchr(opts->argv[i], ' ') != NULL;
if (is_multiword)
*pos++ = '\'';
strcpy(pos, opts->argv[i]); strcpy(pos, opts->argv[i]);
pos += strlen(opts->argv[i]); pos += strlen(opts->argv[i]);
if (is_multiword)
*pos++ = '\'';
*pos++ = ' '; *pos++ = ' ';
} }
pos[-1] = '\0'; pos[-1] = '\0';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment