Linux Audio

Check our new training course

Loading...
v4.17
 1// SPDX-License-Identifier: GPL-2.0
 2#include <errno.h>
 3#include <stdlib.h>
 4#include "strbuf.h"
 5#include "quote.h"
 6#include "util.h"
 7
 8/* Help to copy the thing properly quoted for the shell safety.
 9 * any single quote is replaced with '\'', any exclamation point
10 * is replaced with '\!', and the whole thing is enclosed in a
11 *
12 * E.g.
13 *  original     sq_quote     result
14 *  name     ==> name      ==> 'name'
15 *  a b      ==> a b       ==> 'a b'
16 *  a'b      ==> a'\''b    ==> 'a'\''b'
17 *  a!b      ==> a'\!'b    ==> 'a'\!'b'
18 */
19static inline int need_bs_quote(char c)
20{
21	return (c == '\'' || c == '!');
22}
23
24static int sq_quote_buf(struct strbuf *dst, const char *src)
25{
26	char *to_free = NULL;
27	int ret;
28
29	if (dst->buf == src)
30		to_free = strbuf_detach(dst, NULL);
31
32	ret = strbuf_addch(dst, '\'');
33	while (!ret && *src) {
34		size_t len = strcspn(src, "'!");
35		ret = strbuf_add(dst, src, len);
36		src += len;
37		while (!ret && need_bs_quote(*src))
38			ret = strbuf_addf(dst, "'\\%c\'", *src++);
 
 
 
39	}
40	if (!ret)
41		ret = strbuf_addch(dst, '\'');
42	free(to_free);
43
44	return ret;
45}
46
47int sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
48{
49	int i, ret;
50
51	/* Copy into destination buffer. */
52	ret = strbuf_grow(dst, 255);
53	for (i = 0; !ret && argv[i]; ++i) {
54		ret = strbuf_addch(dst, ' ');
55		if (ret)
56			break;
57		ret = sq_quote_buf(dst, argv[i]);
58		if (maxlen && dst->len > maxlen)
59			return -ENOSPC;
60	}
61	return ret;
62}
v3.5.6
 1#include "cache.h"
 
 
 
 2#include "quote.h"
 
 3
 4/* Help to copy the thing properly quoted for the shell safety.
 5 * any single quote is replaced with '\'', any exclamation point
 6 * is replaced with '\!', and the whole thing is enclosed in a
 7 *
 8 * E.g.
 9 *  original     sq_quote     result
10 *  name     ==> name      ==> 'name'
11 *  a b      ==> a b       ==> 'a b'
12 *  a'b      ==> a'\''b    ==> 'a'\''b'
13 *  a!b      ==> a'\!'b    ==> 'a'\!'b'
14 */
15static inline int need_bs_quote(char c)
16{
17	return (c == '\'' || c == '!');
18}
19
20static void sq_quote_buf(struct strbuf *dst, const char *src)
21{
22	char *to_free = NULL;
 
23
24	if (dst->buf == src)
25		to_free = strbuf_detach(dst, NULL);
26
27	strbuf_addch(dst, '\'');
28	while (*src) {
29		size_t len = strcspn(src, "'!");
30		strbuf_add(dst, src, len);
31		src += len;
32		while (need_bs_quote(*src)) {
33			strbuf_addstr(dst, "'\\");
34			strbuf_addch(dst, *src++);
35			strbuf_addch(dst, '\'');
36		}
37	}
38	strbuf_addch(dst, '\'');
 
39	free(to_free);
 
 
40}
41
42void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
43{
44	int i;
45
46	/* Copy into destination buffer. */
47	strbuf_grow(dst, 255);
48	for (i = 0; argv[i]; ++i) {
49		strbuf_addch(dst, ' ');
50		sq_quote_buf(dst, argv[i]);
 
 
51		if (maxlen && dst->len > maxlen)
52			die("Too many or long arguments");
53	}
 
54}