cgo

[Fork] Gopher Client
git clone https://git.jojolepro.com/cgo.git
Log | Files | Refs | README | LICENSE

commit 73072de685c95402a14145b558d62bf99778cf21
parent 4e50fce8d9971e7588ecfc1e19df37196941b554
Author: Sebastian Steinhauer <s.steinhauer@yahoo.de>
Date:   Mon, 20 Oct 2014 23:05:57 +0200

Split command line into arguments for execvp

Diffstat:
Mcgo.c | 20++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/cgo.c b/cgo.c @@ -466,14 +466,30 @@ void view_file(const char *cmd, const char *host, const char *port, const char *selector) { pid_t pid; - int status; + int status, i, j; + char buffer[1024], *argv[32], *p; if (! download_temp(host, port, selector)) return; + + /* parsed command line string */ + argv[0] = &buffer[0]; + for (p = (char*) cmd, i = 0, j = 1; *p && i < sizeof(buffer) - 1 && j < 30; ) { + if (*p == ' ' || *p == '\t') { + buffer[i++] = 0; + argv[j++] = &buffer[i]; + while (*p == ' ' || *p == '\t') p++; + } else buffer[i++] = *p++; + } + buffer[i] = 0; + argv[j++] = tmpfilename; + argv[j] = NULL; + + /* fork and execute */ printf("executing: %s %s\n", cmd, tmpfilename); pid = fork(); if (pid == 0) { - if (execlp(cmd, cmd, tmpfilename, NULL) == -1) + if (execvp(argv[0], argv) == -1) puts("error: execlp() failed!"); } else if (pid == -1) { puts("error: fork() failed");