summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/ntpq/libntpq.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/ntp/ntpq/libntpq.c')
-rw-r--r--contrib/ntp/ntpq/libntpq.c67
1 files changed, 35 insertions, 32 deletions
diff --git a/contrib/ntp/ntpq/libntpq.c b/contrib/ntp/ntpq/libntpq.c
index 3070e47..e7f0266 100644
--- a/contrib/ntp/ntpq/libntpq.c
+++ b/contrib/ntp/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);
}
OpenPOWER on IntegriCloud