From d521275a86bb72f824dd76e6cb4e35d899f385da Mon Sep 17 00:00:00 2001 From: Manuel Novoa III Date: Mon, 19 Feb 2001 00:28:09 +0000 Subject: Lots of stdio cleanups. Several bug fixes, addition of a number of functions to supplement macros in stdio.h, change perror to use stdio package instead of "write". Also add back in weak stdio initialization for static lib case. --- libc/stdio/printf.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'libc/stdio/printf.c') diff --git a/libc/stdio/printf.c b/libc/stdio/printf.c index 898d0b94e..f6ca8dda4 100644 --- a/libc/stdio/printf.c +++ b/libc/stdio/printf.c @@ -119,6 +119,7 @@ #include #include #include +#include #if WANT_GNU_ERRNO #include @@ -242,7 +243,7 @@ int vprintf(const char *fmt, va_list ap) int vfprintf(FILE * op, register __const char *fmt, register va_list ap) { - return (vfnprintf(op, -1, fmt, ap)); + return vfnprintf(op, -1, fmt, ap); } #endif @@ -258,35 +259,49 @@ int vsprintf(char *sp, __const char *fmt, va_list ap) int vsnprintf(char *sp, size_t size, __const char *fmt, va_list ap) { int rv; -#if 0 - FILE f = {0, 0, (char *) (unsigned) -1, 0, (char *) (unsigned) -1, -1, - _IOFBF | __MODE_WRITE}; -#else - /* As we're only using the putc macro in vfnprintf, we don't need to - initialize all FILE fields. */ FILE f; - f.bufwrite = (char *) (unsigned) -1; + /* + * As we're only using the putc macro in vfnprintf, we don't need to + * initialize all FILE f's fields. + */ + f.bufwrite = (char *) ((unsigned) -1); f.bufpos = sp; f.mode = _IOFBF | __MODE_WRITE; -#endif rv = vfnprintf(&f, size, fmt, ap); - if (size) { - *(f.bufpos) = 0; + if (size) { /* If this is going to a buffer, */ + *(f.bufpos) = 0; /* don't forget to nul-terminate. */ } return rv; } #endif #ifdef L_vdprintf -#warning rewrite vdprintf ... fd may have an associated file!!! plus buffer? +/* + * Note: If fd has an associated buffered FILE, bad things happen. + */ extern int vdprintf(int fd, const char *fmt, va_list ap) { +#if 0 FILE f = {f.unbuf, f.unbuf, f.unbuf, f.unbuf, f.unbuf + sizeof(f.unbuf), - fd, _IONBF | __MODE_WRITE | __MODE_IOTRAN}; + fd, _IONBF | __MODE_WRITE}; + + assert(fd >= 0); /* fd==0 may no longer be stdin */ return vfnprintf(&f, -1, fmt, ap); +#else + char buf[BUFSIZ]; + FILE f = {buf, buf, buf, buf, buf + sizeof(buf), + fd, _IOFBF | __MODE_WRITE}; + int rv; + + assert(fd >= 0); /* fd==0 may no longer be stdin */ + + rv = vfnprintf(&f, -1, fmt, ap); + fflush(&f); + return rv; +#endif } #endif -- cgit v1.2.3