diff options
Diffstat (limited to 'ntpq/libntpq.c')
-rw-r--r-- | ntpq/libntpq.c | 67 |
1 files changed, 35 insertions, 32 deletions
diff --git a/ntpq/libntpq.c b/ntpq/libntpq.c index 3070e47b53c7..e7f02668a9ca 100644 --- a/ntpq/libntpq.c +++ b/ntpq/libntpq.c @@ -57,41 +57,44 @@ struct ntpq_varlist ntpq_varlist[MAXLIST]; int ntpq_stripquotes ( char *resultbuf, char *srcbuf, int datalen, int maxlen ) { - char* tmpbuf = srcbuf; - - while ( *tmpbuf != 0 ) - { - if ( *tmpbuf == '\"' ) - { - tmpbuf++; - continue; - } - - if ( *tmpbuf == '\\' ) - { - tmpbuf++; - switch ( *tmpbuf ) - { - /* ignore if end of string */ - case 0: - continue; + char* dst = resultbuf; + char* dep = resultbuf + maxlen - 1; + char* src = srcbuf; + char* sep = srcbuf + (datalen >= 0 ? datalen : 0); + int esc = 0; + int ch; + + if (maxlen <= 0) + return 0; + + while ((dst != dep) && (src != sep) && (ch = (u_char)*src++) != 0) { + if (esc) { + esc = 0; + switch (ch) { /* skip and do not copy */ - case '\"': /* quotes */ - case 'n': /*newline*/ - case 'r': /*carriage return*/ - case 'g': /*bell*/ - case 't': /*tab*/ - tmpbuf++; - continue; + /* case '"':*/ /* quotes */ + case 'n': /*newline*/ + case 'r': /*carriage return*/ + case 'g': /*bell*/ + case 't': /*tab*/ + continue; + default: + break; } - } - - *resultbuf++ = *tmpbuf++; - + } else { + switch (ch) { + case '\\': + esc = 1; + case '"': + continue; + default: + break; + } + } + *dst++ = (char)ch; } - - *resultbuf = 0; - return strlen(resultbuf); + *dst = '\0'; + return (int)(dst - resultbuf); } |