int vbuf_write(VBUF *bp, const char *buf, int len) { int count; const char *cp; int n; #if 0 for (count = 0; count < len; count++) if (VBUF_PUT(bp, buf[count]) < 0) break; return (count); #else for (cp = buf, count = len; count > 0; cp += n, count -= n) { if (bp->cnt <= 0 && bp->put_ready(bp) != 0) break; n = (count < bp->cnt ? count : bp->cnt); memcpy(bp->ptr, cp, n); bp->ptr += n; bp->cnt -= n; } return (len - count); #endif }
VBUF *vbuf_print(VBUF *bp, const char *format, va_list ap) { const char *myname = "vbuf_print"; static VSTRING *fmt; /* format specifier */ unsigned char *cp; int width; /* width and numerical precision */ int prec; /* are signed for overflow defense */ unsigned long_flag; /* long or plain integer */ int ch; char *s; size_t size; /* APPLE */ /* * Assume that format strings are short. */ if (fmt == 0) fmt = vstring_alloc(INT_SPACE); /* * Iterate over characters in the format string, picking up arguments * when format specifiers are found. */ for (cp = (unsigned char *) format; *cp; cp++) { if (*cp != '%') { VBUF_PUT(bp, *cp); /* ordinary character */ } else if (cp[1] == '%') { VBUF_PUT(bp, *cp++); /* %% becomes % */ } else { /* * Handle format specifiers one at a time, since we can only deal * with arguments one at a time. Try to determine the end of the * format specifier. We do not attempt to fully parse format * strings, since we are ging to let sprintf() do the hard work. * In regular expression notation, we recognize: * * %-?0?([0-9]+|\*)?\.?([0-9]+|\*)?l?[a-zA-Z] * * which includes some combinations that do not make sense. Garbage * in, garbage out. */ VSTRING_RESET(fmt); /* clear format string */ VSTRING_ADDCH(fmt, *cp++); if (*cp == '-') /* left-adjusted field? */ VSTRING_ADDCH(fmt, *cp++); if (*cp == '+') /* signed field? */ VSTRING_ADDCH(fmt, *cp++); if (*cp == '0') /* zero-padded field? */ VSTRING_ADDCH(fmt, *cp++); if (*cp == '*') { /* dynamic field width */ width = va_arg(ap, int); VSTRING_ADDNUM(fmt, width); cp++; } else { /* hard-coded field width */ for (width = 0; ch = *cp, ISDIGIT(ch); cp++) { width = width * 10 + ch - '0'; VSTRING_ADDCH(fmt, ch); } } if (width < 0) { msg_warn("%s: bad width %d in %.50s", myname, width, format); width = 0; } if (*cp == '.') /* width/precision separator */ VSTRING_ADDCH(fmt, *cp++); if (*cp == '*') { /* dynamic precision */ prec = va_arg(ap, int); VSTRING_ADDNUM(fmt, prec); cp++; } else { /* hard-coded precision */
int vbuf_put(VBUF *bp, int ch) { return (bp->put_ready(bp) ? VBUF_EOF : VBUF_PUT(bp, ch)); }