2005-07-08 08:58:32 +02:00
|
|
|
#include "cache.h"
|
|
|
|
#include "quote.h"
|
|
|
|
|
|
|
|
/* Help to copy the thing properly quoted for the shell safety.
|
2005-10-10 23:46:10 +02:00
|
|
|
* any single quote is replaced with '\'', any exclamation point
|
|
|
|
* is replaced with '\!', and the whole thing is enclosed in a
|
2005-07-08 08:58:32 +02:00
|
|
|
*
|
|
|
|
* E.g.
|
|
|
|
* original sq_quote result
|
|
|
|
* name ==> name ==> 'name'
|
|
|
|
* a b ==> a b ==> 'a b'
|
|
|
|
* a'b ==> a'\''b ==> 'a'\''b'
|
2005-10-10 23:46:10 +02:00
|
|
|
* a!b ==> a'\!'b ==> 'a'\!'b'
|
2005-07-08 08:58:32 +02:00
|
|
|
*/
|
2005-10-10 23:46:10 +02:00
|
|
|
#define EMIT(x) ( (++len < n) && (*bp++ = (x)) )
|
2005-07-08 08:58:32 +02:00
|
|
|
|
2005-10-10 23:46:10 +02:00
|
|
|
size_t sq_quote_buf(char *dst, size_t n, const char *src)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
char *bp = dst;
|
|
|
|
size_t len = 0;
|
2005-07-08 08:58:32 +02:00
|
|
|
|
2005-10-10 23:46:10 +02:00
|
|
|
EMIT('\'');
|
2005-07-08 08:58:32 +02:00
|
|
|
while ((c = *src++)) {
|
2005-10-10 23:46:10 +02:00
|
|
|
if (c == '\'' || c == '!') {
|
|
|
|
EMIT('\'');
|
|
|
|
EMIT('\\');
|
|
|
|
EMIT(c);
|
|
|
|
EMIT('\'');
|
|
|
|
} else {
|
|
|
|
EMIT(c);
|
2005-07-08 08:58:32 +02:00
|
|
|
}
|
|
|
|
}
|
2005-10-10 23:46:10 +02:00
|
|
|
EMIT('\'');
|
|
|
|
|
|
|
|
if ( n )
|
|
|
|
*bp = 0;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *sq_quote(const char *src)
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
size_t cnt;
|
|
|
|
|
|
|
|
cnt = sq_quote_buf(NULL, 0, src) + 1;
|
|
|
|
buf = xmalloc(cnt);
|
|
|
|
sq_quote_buf(buf, cnt, src);
|
|
|
|
|
2005-07-08 08:58:32 +02:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|