blob: bdf64544cd8ab22d0a7f6faf559c7ab3734ed7a2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "spawn_library.h"
BUFFER *argv_to_cmdline_buffer(const char **argv) {
BUFFER *wb = buffer_create(0, NULL);
for(size_t i = 0; argv[i] ;i++) {
const char *s = argv[i];
size_t len = strlen(s);
buffer_need_bytes(wb, len * 2 + 1);
bool needs_quotes = false;
for(const char *c = s; !needs_quotes && *c ; c++) {
switch(*c) {
case ' ':
case '\v':
case '\t':
case '\n':
case '"':
needs_quotes = true;
break;
default:
break;
}
}
if(needs_quotes && buffer_strlen(wb))
buffer_strcat(wb, " \"");
else if(buffer_strlen(wb))
buffer_putc(wb, ' ');
for(const char *c = s; *c ; c++) {
switch(*c) {
case '"':
buffer_putc(wb, '\\');
// fall through
default:
buffer_putc(wb, *c);
break;
}
}
if(needs_quotes)
buffer_strcat(wb, "\"");
}
return wb;
}
|