X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fff-printf.c;h=8825a2880437dff93a19e29b5e947d68b294a64b;hb=434c630cf7aa4b4facc1d2fcfb6e9bc4cf21dff1;hp=395e04b2155529c2610eb59f610544c851c096a5;hpb=4d343838fefa75faeca13c7a144a6d8ac207261d;p=libucw.git diff --git a/lib/ff-printf.c b/lib/ff-printf.c index 395e04b2..8825a288 100644 --- a/lib/ff-printf.c +++ b/lib/ff-printf.c @@ -1,7 +1,7 @@ /* - * Sherlock Library -- Printf on Fastbuf Streams + * UCW Library -- Printf on Fastbuf Streams * - * (c) 2002 Martin Mares + * (c) 2002--2005 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -10,18 +10,41 @@ #include "lib/lib.h" #include "lib/fastbuf.h" +#include #include int -vbprintf(struct fastbuf *b, byte *msg, va_list args) +vbprintf(struct fastbuf *b, char *msg, va_list args) { byte *buf; - int r, len = 256; + int len, r; + va_list args2; + + len = bdirect_write_prepare(b, &buf); + if (len >= 16) + { + va_copy(args2, args); + r = vsnprintf(buf, len, msg, args2); + va_end(args2); + if (r < 0) + len = 256; + else if (r < len) + { + bdirect_write_commit(b, buf+r); + return r; + } + else + len = r+1; + } + else + len = 256; while (1) { buf = alloca(len); - r = vsnprintf(buf, len, msg, args); + va_copy(args2, args); + r = vsnprintf(buf, len, msg, args2); + va_end(args2); if (r < 0) len += len; else if (r < len) @@ -35,7 +58,7 @@ vbprintf(struct fastbuf *b, byte *msg, va_list args) } int -bprintf(struct fastbuf *b, byte *msg, ...) +bprintf(struct fastbuf *b, char *msg, ...) { va_list args; int res; @@ -51,7 +74,8 @@ bprintf(struct fastbuf *b, byte *msg, ...) int main(void) { struct fastbuf *b = bfdopen_shared(1, 65536); - bprintf(b, "13=%d str=<%s> msg=%m\n", 13, "str"); + for (int i=0; i<10000; i++) + bprintf(b, "13=%d str=<%s> msg=%m\n", 13, "str"); bclose(b); return 0; }