From: Todd C. Miller Date: Wed, 22 Aug 2007 22:31:07 +0000 (+0000) Subject: Add lbuf_append_quoted() which takes a set of characters which X-Git-Tag: SUDO_1_7_0~418 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=14d1b79a4a8607645165514d6f7d597d931a6e56;p=sudo Add lbuf_append_quoted() which takes a set of characters which should be quoted with a backslash when displayed. --- diff --git a/lbuf.c b/lbuf.c index 4266e070a..76d6d5fc2 100644 --- a/lbuf.c +++ b/lbuf.c @@ -92,6 +92,68 @@ lbuf_destroy(lbuf) lbuf->buf = NULL; } +/* + * Append strings to the buffer, expanding it as needed. + */ +void +#ifdef __STDC__ +lbuf_append_quoted(struct lbuf *lbuf, const char *set, ...) +#else +lbuf_appen_quotedd(lbuf, va_alist) + struct lbuf *lbuf; + const char *set; + va_dcl +#endif +{ + va_list ap; + int len = 0; + char *cp, *s; + +#ifdef __STDC__ + va_start(ap, set); +#else + va_start(ap); +#endif + while ((s = va_arg(ap, char *)) != NULL) { + len += strlen(s); + for (cp = s; (cp = strpbrk(cp, set)) != NULL; cp++) + len++; + } + va_end(ap); + + /* Expand buffer as needed. */ + if (lbuf->len + len >= lbuf->size) { + do { + lbuf->size += 256; + } while (lbuf->len + len >= lbuf->size); + lbuf->buf = erealloc(lbuf->buf, lbuf->size); + } + +#ifdef __STDC__ + va_start(ap, set); +#else + va_start(ap); +#endif + /* Append each string. */ + while ((s = va_arg(ap, char *)) != NULL) { + while ((cp = strpbrk(s, set)) != NULL) { + len = (int)(cp - s); + memcpy(lbuf->buf + lbuf->len, s, len); + lbuf->len += len; + lbuf->buf[lbuf->len++] = '\\'; + lbuf->buf[lbuf->len++] = *cp; + s = cp + 1; + } + if (*s != '\0') { + len = strlen(s); + memcpy(lbuf->buf + lbuf->len, s, len); + lbuf->len += len; + } + } + lbuf->buf[lbuf->len] = '\0'; + va_end(ap); +} + /* * Append strings to the buffer, expanding it as needed. */ diff --git a/lbuf.h b/lbuf.h index a9e1a05e7..26dceac0b 100644 --- a/lbuf.h +++ b/lbuf.h @@ -35,6 +35,7 @@ struct lbuf { void lbuf_init __P((struct lbuf *, char *, int, int)); void lbuf_destroy __P((struct lbuf *)); void lbuf_append __P((struct lbuf *, ...)); +void lbuf_append_quoted __P((struct lbuf *, const char *, ...)); void lbuf_print __P((struct lbuf *)); #endif /* _SUDO_LBUF_H */