Lines 223-228
Link Here
|
223 |
return (pnew - str); |
223 |
return (pnew - str); |
224 |
} |
224 |
} |
225 |
|
225 |
|
|
|
226 |
spif_charptr_t |
227 |
escape_string(spif_charptr_t str, spif_char_t quote, spif_int32_t maxlen) |
228 |
{ |
229 |
spif_charptr_t buff, s = str, pbuff; |
230 |
|
231 |
D_STRINGS(("escape_string(%s %c %ld)\n", (char *) str, quote, maxlen)); |
232 |
if (! quote) { |
233 |
quote = '\"'; |
234 |
} |
235 |
|
236 |
/* The escaped string will be at most twice the length of the original. */ |
237 |
buff = SPIF_CAST(charptr) MALLOC(strlen(SPIF_CAST_PTR(char) str) * 2 + 1); |
238 |
|
239 |
/* Copy and escape the string from str into buff. */ |
240 |
for (pbuff = buff; (*s); s++, pbuff++) { |
241 |
if (*s == quote) { |
242 |
D_STRINGS(("Double-escaping \'%c\' at position %d\n", *s, s - str)); |
243 |
*pbuff = '\\'; |
244 |
pbuff++; |
245 |
*pbuff = '\\'; |
246 |
pbuff++; |
247 |
} else { |
248 |
if (quote == '\"') { |
249 |
if ((*s == '\\') || (*s == '`')) { |
250 |
D_STRINGS(("Escaping \'%c\' at position %d\n", *s, s - str)); |
251 |
*pbuff = '\\'; |
252 |
pbuff++; |
253 |
} |
254 |
} |
255 |
} |
256 |
D_STRINGS(("Copying \'%c\' at position %d\n", *s, s - str)); |
257 |
*pbuff = *s; |
258 |
} |
259 |
*pbuff = 0; |
260 |
|
261 |
if (maxlen) { |
262 |
/* Given maxlen, we know "str" can hold at least "maxlen" chars. */ |
263 |
if (!spiftool_safe_strncpy(str, buff, maxlen)) { |
264 |
str[maxlen] = 0; |
265 |
} |
266 |
FREE(buff); |
267 |
return str; |
268 |
} else { |
269 |
return buff; |
270 |
} |
271 |
} |
272 |
|
226 |
char * |
273 |
char * |
227 |
safe_print_string(const char *str, unsigned long len) |
274 |
safe_print_string(const char *str, unsigned long len) |
228 |
{ |
275 |
{ |