aboutsummaryrefslogtreecommitdiff
path: root/newlib/libc/stdio/vfprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'newlib/libc/stdio/vfprintf.c')
-rw-r--r--newlib/libc/stdio/vfprintf.c262
1 files changed, 8 insertions, 254 deletions
diff --git a/newlib/libc/stdio/vfprintf.c b/newlib/libc/stdio/vfprintf.c
index 42272ab..feb1fab 100644
--- a/newlib/libc/stdio/vfprintf.c
+++ b/newlib/libc/stdio/vfprintf.c
@@ -174,270 +174,24 @@ static char *rcsid = "$Id$";
#endif
#ifdef STRING_ONLY
+
# ifdef _FVWRITE_IN_STREAMIO
# define __SPRINT __ssprint_r
+ int __ssprint_r (struct _reent *, FILE *, register struct __suio *);
# else
# define __SPRINT __ssputs_r
+ int __ssputs_r (struct _reent *, FILE *, const char *, size_t);
# endif
-#else
+
+#else /* !STRING_ONLY */
+
# ifdef _FVWRITE_IN_STREAMIO
# define __SPRINT __sprint_r
+ int __sprint_r (struct _reent *, FILE *, register struct __suio *);
# else
# define __SPRINT __sfputs_r
+ int __sfputs_r (struct _reent *, FILE *, const char *buf, size_t);
# endif
-#endif
-
-/* The __sprint_r/__ssprint_r functions are shared between all versions of
- vfprintf and vfwprintf. They must only be defined once, which we do in
- the INTEGER_ONLY versions here. */
-#ifdef STRING_ONLY
-#ifdef INTEGER_ONLY
-#ifndef _FVWRITE_IN_STREAMIO
-int
-__ssputs_r (struct _reent *ptr,
- FILE *fp,
- const char *buf,
- size_t len)
-{
- register int w;
-
- w = fp->_w;
- if (len >= w && fp->_flags & (__SMBF | __SOPT)) {
- /* must be asprintf family */
- unsigned char *str;
- int curpos = (fp->_p - fp->_bf._base);
- /* Choose a geometric growth factor to avoid
- * quadratic realloc behavior, but use a rate less
- * than (1+sqrt(5))/2 to accomodate malloc
- * overhead. asprintf EXPECTS us to overallocate, so
- * that it can add a trailing \0 without
- * reallocating. The new allocation should thus be
- * max(prev_size*1.5, curpos+len+1). */
- int newsize = fp->_bf._size * 3 / 2;
- if (newsize < curpos + len + 1)
- newsize = curpos + len + 1;
- if (fp->_flags & __SOPT)
- {
- /* asnprintf leaves original buffer alone. */
- str = (unsigned char *)_malloc_r (ptr, newsize);
- if (!str)
- {
- _REENT_ERRNO(ptr) = ENOMEM;
- goto err;
- }
- memcpy (str, fp->_bf._base, curpos);
- fp->_flags = (fp->_flags & ~__SOPT) | __SMBF;
- }
- else
- {
- str = (unsigned char *)_realloc_r (ptr, fp->_bf._base,
- newsize);
- if (!str) {
- /* Free unneeded buffer. */
- _free_r (ptr, fp->_bf._base);
- /* Ensure correct errno, even if free
- * changed it. */
- _REENT_ERRNO(ptr) = ENOMEM;
- goto err;
- }
- }
- fp->_bf._base = str;
- fp->_p = str + curpos;
- fp->_bf._size = newsize;
- w = len;
- fp->_w = newsize - curpos;
- }
- if (len < w)
- w = len;
- (void)memmove ((void *) fp->_p, (void *) buf, (size_t) (w));
- fp->_w -= w;
- fp->_p += w;
-
- return 0;
-
-err:
- fp->_flags |= __SERR;
- return EOF;
-}
-#endif
-
-int
-__ssprint_r (struct _reent *ptr,
- FILE *fp,
- register struct __suio *uio)
-{
- register size_t len;
- register int w;
- register struct __siov *iov;
- register const char *p = NULL;
-
- iov = uio->uio_iov;
- len = 0;
-
- if (uio->uio_resid == 0) {
- uio->uio_iovcnt = 0;
- return (0);
- }
-
- do {
- while (len == 0) {
- p = iov->iov_base;
- len = iov->iov_len;
- iov++;
- }
- w = fp->_w;
- if (len >= w && fp->_flags & (__SMBF | __SOPT)) {
- /* must be asprintf family */
- unsigned char *str;
- int curpos = (fp->_p - fp->_bf._base);
- /* Choose a geometric growth factor to avoid
- * quadratic realloc behavior, but use a rate less
- * than (1+sqrt(5))/2 to accomodate malloc
- * overhead. asprintf EXPECTS us to overallocate, so
- * that it can add a trailing \0 without
- * reallocating. The new allocation should thus be
- * max(prev_size*1.5, curpos+len+1). */
- int newsize = fp->_bf._size * 3 / 2;
- if (newsize < curpos + len + 1)
- newsize = curpos + len + 1;
- if (fp->_flags & __SOPT)
- {
- /* asnprintf leaves original buffer alone. */
- str = (unsigned char *)_malloc_r (ptr, newsize);
- if (!str)
- {
- _REENT_ERRNO(ptr) = ENOMEM;
- goto err;
- }
- memcpy (str, fp->_bf._base, curpos);
- fp->_flags = (fp->_flags & ~__SOPT) | __SMBF;
- }
- else
- {
- str = (unsigned char *)_realloc_r (ptr, fp->_bf._base,
- newsize);
- if (!str) {
- /* Free unneeded buffer. */
- _free_r (ptr, fp->_bf._base);
- /* Ensure correct errno, even if free
- * changed it. */
- _REENT_ERRNO(ptr) = ENOMEM;
- goto err;
- }
- }
- fp->_bf._base = str;
- fp->_p = str + curpos;
- fp->_bf._size = newsize;
- w = len;
- fp->_w = newsize - curpos;
- }
- if (len < w)
- w = len;
- (void)memmove ((void *) fp->_p, (void *) p, (size_t) (w));
- fp->_w -= w;
- fp->_p += w;
- w = len; /* pretend we copied all */
- p += w;
- len -= w;
- } while ((uio->uio_resid -= w) != 0);
-
- uio->uio_resid = 0;
- uio->uio_iovcnt = 0;
- return 0;
-
-err:
- fp->_flags |= __SERR;
- uio->uio_resid = 0;
- uio->uio_iovcnt = 0;
- return EOF;
-}
-#else /* !INTEGER_ONLY */
-#ifndef _FVWRITE_IN_STREAMIO
-int __ssputs_r (struct _reent *, FILE *, const char *, size_t);
-#endif
-int __ssprint_r (struct _reent *, FILE *, register struct __suio *);
-#endif /* !INTEGER_ONLY */
-
-#else /* !STRING_ONLY */
-#ifdef INTEGER_ONLY
-
-#ifndef _FVWRITE_IN_STREAMIO
-int
-__sfputs_r (struct _reent *ptr,
- FILE *fp,
- const char *buf,
- size_t len)
-{
- register int i;
-
-#if defined _WIDE_ORIENT && (!defined _ELIX_LEVEL || _ELIX_LEVEL >= 4)
- if (fp->_flags2 & __SWID) {
- wchar_t *p;
-
- p = (wchar_t *) buf;
- for (i = 0; i < (len / sizeof (wchar_t)); i++) {
- if (_fputwc_r (ptr, p[i], fp) == WEOF)
- return -1;
- }
- } else {
-#else
- {
-#endif
- for (i = 0; i < len; i++) {
- if (_fputc_r (ptr, buf[i], fp) == EOF)
- return -1;
- }
- }
- return (0);
-}
-#endif
-/*
- * Flush out all the vectors defined by the given uio,
- * then reset it so that it can be reused.
- */
-int
-__sprint_r (struct _reent *ptr,
- FILE *fp,
- register struct __suio *uio)
-{
- register int err = 0;
-
- if (uio->uio_resid == 0) {
- uio->uio_iovcnt = 0;
- return (0);
- }
-#if defined _WIDE_ORIENT && (!defined _ELIX_LEVEL || _ELIX_LEVEL >= 4)
- if (fp->_flags2 & __SWID) {
- struct __siov *iov;
- wchar_t *p;
- int i, len;
-
- iov = uio->uio_iov;
- for (; uio->uio_resid != 0;
- uio->uio_resid -= len * sizeof (wchar_t), iov++) {
- p = (wchar_t *) iov->iov_base;
- len = iov->iov_len / sizeof (wchar_t);
- for (i = 0; i < len; i++) {
- if (_fputwc_r (ptr, p[i], fp) == WEOF) {
- err = -1;
- goto out;
- }
- }
- }
- } else
-#endif
- err = __sfvwrite_r(ptr, fp, uio);
-out:
- uio->uio_resid = 0;
- uio->uio_iovcnt = 0;
- return (err);
-}
-#else /* !INTEGER_ONLY */
-#ifndef _FVWRITE_IN_STREAMIO
-int __sfputs_r (struct _reent *, FILE *, const char *buf, size_t);
-#endif
-int __sprint_r (struct _reent *, FILE *, register struct __suio *);
-#endif /* !INTEGER_ONLY */
#ifdef _UNBUF_STREAM_OPT
/*