@matheusbsbs / matheus-bagliano / commits / 77d604c309
Fork 0

Enhanced sq_quote()

Create function to sq_quote into a buffer Handle !'s for csh-based shells Signed-off-by: H. Peter Anvin <hpa@zytor.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

H. Peter Anvin committed Oct 10, 2005 at 14:46 UTC 77d604c30974587a2e0d36d92c48cef87f6e8e4d
2 files changed +43 -24
quote.c
+34 -22
@@ -2,40 +2,52 @@
2 #include "quote.h"
3
4 /* Help to copy the thing properly quoted for the shell safety.
5 - * any single quote is replaced with '\'', and the caller is
6 - * expected to enclose the result within a single quote pair.
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 */
14 -char *sq_quote(const char *src)
15 -{
16 - static char *buf = NULL;
17 - int cnt, c;
18 - const char *cp;
19 - char *bp;
15 +#define EMIT(x) ( (++len < n) && (*bp++ = (x)) )
16
21 - /* count bytes needed to store the quoted string. */
22 - for (cnt = 3, cp = src; *cp; cnt++, cp++)
23 - if (*cp == '\'')
24 - cnt += 3;
17 +size_t sq_quote_buf(char *dst, size_t n, const char *src)
18 +{
19 + char c;
20 + char *bp = dst;
21 + size_t len = 0;
22
26 - buf = xmalloc(cnt);
27 - bp = buf;
28 - *bp++ = '\'';
23 + EMIT('\'');
24 while ((c = *src++)) {
30 - if (c != '\'')
31 - *bp++ = c;
32 - else {
33 - bp = strcpy(bp, "'\\''");
34 - bp += 4;
25 + if (c == '\'' || c == '!') {
26 + EMIT('\'');
27 + EMIT('\\');
28 + EMIT(c);
29 + EMIT('\'');
30 + } else {
31 + EMIT(c);
32 }
33 }
37 - *bp++ = '\'';
38 - *bp = 0;
34 + EMIT('\'');
35 +
36 + if ( n )
37 + *bp = 0;
38 +
39 + return len;
40 +}
41 +
42 +char *sq_quote(const char *src)
43 +{
44 + char *buf;
45 + size_t cnt;
46 +
47 + cnt = sq_quote_buf(NULL, 0, src) + 1;
48 + buf = xmalloc(cnt);
49 + sq_quote_buf(buf, cnt, src);
50 +
51 return buf;
52 }
53
quote.h
+9 -2
@@ -1,10 +1,12 @@
1 #ifndef QUOTE_H
2 #define QUOTE_H
3
4 +#include <stddef.h>
5
6 /* Help to copy the thing properly quoted for the shell safety.
6 - * any single quote is replaced with '\'', and the whole thing
7 - * is enclosed in a single quote pair.
7 + * any single quote is replaced with '\'', any exclamation point
8 + * is replaced with '\!', and the whole thing is enclosed in a
9 + * single quote pair.
10 *
11 * For example, if you are passing the result to system() as an
12 * argument:
@@ -19,8 +21,13 @@
21 *
22 * Note that the above examples leak memory! Remember to free result from
23 * sq_quote() in a real application.
24 + *
25 + * sq_quote_buf() writes to an existing buffer of specified size; it
26 + * will return the number of characters that would have been written
27 + * excluding the final null regardless of the buffer size.
28 */
29
30 char *sq_quote(const char *src);
31 +size_t sq_quote_buf(char *dst, size_t n, const char *src);
32
33 #endif