Skip to content

Commit 7c6a684

Browse files
committed
nk/pspawn: Fix argv for multiple argument commands.
Also remove the quoting scheme; it's difficult to perform shell-like quoting without excessive allocation.
1 parent 5e4defc commit 7c6a684

1 file changed

Lines changed: 15 additions & 32 deletions

File tree

nk/pspawn.c

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
memcpy(argbuf, (STRVAL), SL); \
2222
argbuf[SL] = 0; \
2323
argv[curv] = argbuf; argv[++curv] = NULL; \
24-
argbuf += SL; argbuflen -= SL; \
24+
argbuf += SL + 1; argbuflen -= SL + 1; \
2525
} while (0)
2626

2727
int nk_pspawn(pid_t *pid, const char *command,
@@ -49,40 +49,23 @@ int nk_pspawn(pid_t *pid, const char *command,
4949
if (args) {
5050
p = args;
5151
const char *q = args;
52-
bool squote = false, dquote = false, atend = false;
52+
bool atend = false;
5353
for (;; ++p) {
54-
switch (*p) {
55-
default: continue;
56-
case '\0':
57-
atend = true;
58-
goto endarg;
59-
case ' ':
60-
if (!squote && !dquote)
61-
goto endarg;
54+
if (*p == '\0') {
55+
atend = true;
56+
} else if (*p != ' ')
6257
continue;
63-
case '\'':
64-
if (!dquote)
65-
squote = !squote;
66-
continue;
67-
case '"':
68-
if (!squote)
69-
dquote = !dquote;
70-
continue;
71-
}
72-
endarg:
73-
{
74-
if (p == q) break;
75-
// Push an argument.
76-
if (q > p) {
77-
static const char errstr[] = "nk_execute: argument length too long\n";
78-
safe_write(STDERR_FILENO, errstr, sizeof errstr);
79-
_Exit(EXIT_FAILURE);
80-
}
81-
NK_GEN_ARG(q, (size_t)(p - q));
82-
q = p + 1;
83-
if (atend || curv >= (MAX_ARGS - 1))
84-
break;
58+
if (p == q) break;
59+
// Push an argument.
60+
if (q > p) {
61+
static const char errstr[] = "nk_execute: argument length too long\n";
62+
safe_write(STDERR_FILENO, errstr, sizeof errstr);
63+
_Exit(EXIT_FAILURE);
8564
}
65+
NK_GEN_ARG(q, (size_t)(p - q));
66+
q = p + 1;
67+
if (atend || curv >= (MAX_ARGS - 1))
68+
break;
8669
}
8770
}
8871
return posix_spawn(pid, command, file_actions, attrp, argv, envp);

0 commit comments

Comments
 (0)