util: add quote_string flag signalising that string is NUL-terminated

It is useful in cases strings with size provided are expected to be
NUL-terminated but are not trustworthy enough to call just plain
printstr(str, -1).

* defs.h (QUOTE_OMIT_TRAILING_0): New constant definition.
* util.c (string_quote): Swallow terminating NUL if
QUOTE_OMIT_TRAILING_0 is set.
This commit is contained in:
Eugene Syromyatnikov 2016-10-03 21:35:37 +03:00 committed by Dmitry V. Levin
parent 04ec08ab62
commit 5cf68f97d5
2 changed files with 6 additions and 2 deletions

5
defs.h
View File

@ -520,8 +520,9 @@ extern unsigned long get_pagesize(void);
extern int string_to_uint(const char *str);
extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
#define QUOTE_0_TERMINATED 0x01
#define QUOTE_OMIT_LEADING_TRAILING_QUOTES 0x02
#define QUOTE_0_TERMINATED 0x01
#define QUOTE_OMIT_LEADING_TRAILING_QUOTES 0x02
#define QUOTE_OMIT_TRAILING_0 0x08
extern int string_quote(const char *, char *, unsigned int, unsigned int);
extern int print_quoted_string(const char *, unsigned int, unsigned int);

3
util.c
View File

@ -619,6 +619,9 @@ string_quote(const char *instr, char *outstr, const unsigned int size,
/* Check for NUL-terminated string. */
if (c == eol)
goto asciz_ended;
if ((i == (size - 1)) &&
(style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
goto asciz_ended;
switch (c) {
case '\"': case '\\':
*s++ = '\\';