6 /* Help to copy the thing properly quoted for the shell safety.
7 * any single quote is replaced with '\'', any exclamation point
8 * is replaced with '\!', and the whole thing is enclosed in a
11 * original sq_quote result
12 * name ==> name ==> 'name'
13 * a b ==> a b ==> 'a b'
14 * a'b ==> a'\''b ==> 'a'\''b'
15 * a!b ==> a'\!'b ==> 'a'\!'b'
17 static inline int need_bs_quote(char c
)
19 return (c
== '\'' || c
== '!');
22 static int sq_quote_buf(struct strbuf
*dst
, const char *src
)
28 to_free
= strbuf_detach(dst
, NULL
);
30 ret
= strbuf_addch(dst
, '\'');
31 while (!ret
&& *src
) {
32 size_t len
= strcspn(src
, "'!");
33 ret
= strbuf_add(dst
, src
, len
);
35 while (!ret
&& need_bs_quote(*src
))
36 ret
= strbuf_addf(dst
, "'\\%c\'", *src
++);
39 ret
= strbuf_addch(dst
, '\'');
45 int sq_quote_argv(struct strbuf
*dst
, const char** argv
, size_t maxlen
)
49 /* Copy into destination buffer. */
50 ret
= strbuf_grow(dst
, 255);
51 for (i
= 0; !ret
&& argv
[i
]; ++i
) {
52 ret
= strbuf_addch(dst
, ' ');
55 ret
= sq_quote_buf(dst
, argv
[i
]);
56 if (maxlen
&& dst
->len
> maxlen
)
57 die("Too many or long arguments");