7 /* Help to copy the thing properly quoted for the shell safety.
8 * any single quote is replaced with '\'', any exclamation point
9 * is replaced with '\!', and the whole thing is enclosed in a
12 * original sq_quote result
13 * name ==> name ==> 'name'
14 * a b ==> a b ==> 'a b'
15 * a'b ==> a'\''b ==> 'a'\''b'
16 * a!b ==> a'\!'b ==> 'a'\!'b'
18 static inline int need_bs_quote(char c)
20 return (c == '\'' || c == '!');
23 static int sq_quote_buf(struct strbuf *dst, const char *src)
29 to_free = strbuf_detach(dst, NULL);
31 ret = strbuf_addch(dst, '\'');
32 while (!ret && *src) {
33 size_t len = strcspn(src, "'!");
34 ret = strbuf_add(dst, src, len);
36 while (!ret && need_bs_quote(*src))
37 ret = strbuf_addf(dst, "'\\%c\'", *src++);
40 ret = strbuf_addch(dst, '\'');
46 int sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
50 /* Copy into destination buffer. */
51 ret = strbuf_grow(dst, 255);
52 for (i = 0; !ret && argv[i]; ++i) {
53 ret = strbuf_addch(dst, ' ');
56 ret = sq_quote_buf(dst, argv[i]);
57 if (maxlen && dst->len > maxlen)