From 595d23fca25540206311a6d9954122255f7b6432 Mon Sep 17 00:00:00 2001 From: Giacomo Tesio Date: Fri, 21 Apr 2017 21:27:24 +0200 Subject: [PATCH] jehanne: fix uio initializations; disable optimizations in print related functions --- newlib/configure.host | 1 + newlib/libc/sys/jehanne/Makefile.am | 4 +- newlib/libc/sys/jehanne/Makefile.in | 59 +- newlib/libc/sys/jehanne/fputs.c | 173 ++ newlib/libc/sys/jehanne/fputws.c | 198 +++ newlib/libc/sys/jehanne/fvwrite.c | 271 +++ newlib/libc/sys/jehanne/fvwrite.h | 36 + newlib/libc/sys/jehanne/fwrite.c | 218 +++ newlib/libc/sys/jehanne/local.h | 352 ++++ newlib/libc/sys/jehanne/puts.c | 148 ++ newlib/libc/sys/jehanne/vfieeefp.h | 284 ++++ newlib/libc/sys/jehanne/vfprintf.c | 2359 +++++++++++++++++++++++++++ newlib/libc/sys/jehanne/vfwprintf.c | 2024 +++++++++++++++++++++++ newlib/libc/sys/jehanne/wbuf.c | 101 ++ 14 files changed, 6225 insertions(+), 3 deletions(-) create mode 100644 newlib/libc/sys/jehanne/fputs.c create mode 100644 newlib/libc/sys/jehanne/fputws.c create mode 100644 newlib/libc/sys/jehanne/fvwrite.c create mode 100644 newlib/libc/sys/jehanne/fvwrite.h create mode 100644 newlib/libc/sys/jehanne/fwrite.c create mode 100644 newlib/libc/sys/jehanne/local.h create mode 100644 newlib/libc/sys/jehanne/puts.c create mode 100644 newlib/libc/sys/jehanne/vfieeefp.h create mode 100644 newlib/libc/sys/jehanne/vfprintf.c create mode 100644 newlib/libc/sys/jehanne/vfwprintf.c create mode 100644 newlib/libc/sys/jehanne/wbuf.c diff --git a/newlib/configure.host b/newlib/configure.host index 4ca436096..bd057f830 100644 --- a/newlib/configure.host +++ b/newlib/configure.host @@ -385,6 +385,7 @@ case "${host}" in sys_dir=jehanne syscall_dir=syscalls newlib_cflags="${newlib_cflags} -DREENTRANT_SYSCALLS_PROVIDED -DMALLOC_PROVIDED" + newlib_cflags="${newlib_cflags} -fno-omit-frame-pointer" have_crt0="no" ;; *-*-netware*) diff --git a/newlib/libc/sys/jehanne/Makefile.am b/newlib/libc/sys/jehanne/Makefile.am index 0217e8652..f2e1d7366 100644 --- a/newlib/libc/sys/jehanne/Makefile.am +++ b/newlib/libc/sys/jehanne/Makefile.am @@ -10,7 +10,9 @@ else extra_objs = endif -lib_a_SOURCES = getenv_r.c getenv.c malloc.c mallocr.c free.c freer.c calloc.c callocr.c realloc.c reallocr.c +lib_a_SOURCES = getenv_r.c getenv.c malloc.c mallocr.c free.c freer.c \ + calloc.c callocr.c realloc.c reallocr.c \ + fputs.c fputws.c fvwrite.c fwrite.c puts.c vfprintf.c vfwprintf.c wbuf.c lib_a_LIBADD = $(extra_objs) EXTRA_lib_a_SOURCES = libposix_conf.c syscalls.c lib_a_DEPENDENCIES = $(extra_objs) diff --git a/newlib/libc/sys/jehanne/Makefile.in b/newlib/libc/sys/jehanne/Makefile.in index b932e81fe..509382969 100644 --- a/newlib/libc/sys/jehanne/Makefile.in +++ b/newlib/libc/sys/jehanne/Makefile.in @@ -57,7 +57,11 @@ am_lib_a_OBJECTS = lib_a-getenv_r.$(OBJEXT) lib_a-getenv.$(OBJEXT) \ lib_a-malloc.$(OBJEXT) lib_a-mallocr.$(OBJEXT) \ lib_a-free.$(OBJEXT) lib_a-freer.$(OBJEXT) \ lib_a-calloc.$(OBJEXT) lib_a-callocr.$(OBJEXT) \ - lib_a-realloc.$(OBJEXT) lib_a-reallocr.$(OBJEXT) + lib_a-realloc.$(OBJEXT) lib_a-reallocr.$(OBJEXT) \ + lib_a-fputs.$(OBJEXT) lib_a-fputws.$(OBJEXT) \ + lib_a-fvwrite.$(OBJEXT) lib_a-fwrite.$(OBJEXT) \ + lib_a-puts.$(OBJEXT) lib_a-vfprintf.$(OBJEXT) \ + lib_a-vfwprintf.$(OBJEXT) lib_a-wbuf.$(OBJEXT) lib_a_OBJECTS = $(am_lib_a_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ depcomp = @@ -178,7 +182,10 @@ AM_CCASFLAGS = $(INCLUDES) noinst_LIBRARIES = lib.a @MAY_SUPPLY_SYSCALLS_FALSE@extra_objs = @MAY_SUPPLY_SYSCALLS_TRUE@extra_objs = syscalls.o libposix_conf.o -lib_a_SOURCES = getenv_r.c getenv.c malloc.c mallocr.c free.c freer.c calloc.c callocr.c realloc.c reallocr.c +lib_a_SOURCES = getenv_r.c getenv.c malloc.c mallocr.c free.c freer.c \ + calloc.c callocr.c realloc.c reallocr.c \ + fputs.c fputws.c fvwrite.c fwrite.c puts.c vfprintf.c vfwprintf.c wbuf.c + lib_a_LIBADD = $(extra_objs) EXTRA_lib_a_SOURCES = libposix_conf.c syscalls.c lib_a_DEPENDENCIES = $(extra_objs) @@ -308,6 +315,54 @@ lib_a-reallocr.o: reallocr.c lib_a-reallocr.obj: reallocr.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-reallocr.obj `if test -f 'reallocr.c'; then $(CYGPATH_W) 'reallocr.c'; else $(CYGPATH_W) '$(srcdir)/reallocr.c'; fi` +lib_a-fputs.o: fputs.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fputs.o `test -f 'fputs.c' || echo '$(srcdir)/'`fputs.c + +lib_a-fputs.obj: fputs.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fputs.obj `if test -f 'fputs.c'; then $(CYGPATH_W) 'fputs.c'; else $(CYGPATH_W) '$(srcdir)/fputs.c'; fi` + +lib_a-fputws.o: fputws.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fputws.o `test -f 'fputws.c' || echo '$(srcdir)/'`fputws.c + +lib_a-fputws.obj: fputws.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fputws.obj `if test -f 'fputws.c'; then $(CYGPATH_W) 'fputws.c'; else $(CYGPATH_W) '$(srcdir)/fputws.c'; fi` + +lib_a-fvwrite.o: fvwrite.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fvwrite.o `test -f 'fvwrite.c' || echo '$(srcdir)/'`fvwrite.c + +lib_a-fvwrite.obj: fvwrite.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fvwrite.obj `if test -f 'fvwrite.c'; then $(CYGPATH_W) 'fvwrite.c'; else $(CYGPATH_W) '$(srcdir)/fvwrite.c'; fi` + +lib_a-fwrite.o: fwrite.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fwrite.o `test -f 'fwrite.c' || echo '$(srcdir)/'`fwrite.c + +lib_a-fwrite.obj: fwrite.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-fwrite.obj `if test -f 'fwrite.c'; then $(CYGPATH_W) 'fwrite.c'; else $(CYGPATH_W) '$(srcdir)/fwrite.c'; fi` + +lib_a-puts.o: puts.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-puts.o `test -f 'puts.c' || echo '$(srcdir)/'`puts.c + +lib_a-puts.obj: puts.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-puts.obj `if test -f 'puts.c'; then $(CYGPATH_W) 'puts.c'; else $(CYGPATH_W) '$(srcdir)/puts.c'; fi` + +lib_a-vfprintf.o: vfprintf.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-vfprintf.o `test -f 'vfprintf.c' || echo '$(srcdir)/'`vfprintf.c + +lib_a-vfprintf.obj: vfprintf.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-vfprintf.obj `if test -f 'vfprintf.c'; then $(CYGPATH_W) 'vfprintf.c'; else $(CYGPATH_W) '$(srcdir)/vfprintf.c'; fi` + +lib_a-vfwprintf.o: vfwprintf.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-vfwprintf.o `test -f 'vfwprintf.c' || echo '$(srcdir)/'`vfwprintf.c + +lib_a-vfwprintf.obj: vfwprintf.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-vfwprintf.obj `if test -f 'vfwprintf.c'; then $(CYGPATH_W) 'vfwprintf.c'; else $(CYGPATH_W) '$(srcdir)/vfwprintf.c'; fi` + +lib_a-wbuf.o: wbuf.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-wbuf.o `test -f 'wbuf.c' || echo '$(srcdir)/'`wbuf.c + +lib_a-wbuf.obj: wbuf.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-wbuf.obj `if test -f 'wbuf.c'; then $(CYGPATH_W) 'wbuf.c'; else $(CYGPATH_W) '$(srcdir)/wbuf.c'; fi` + lib_a-libposix_conf.o: libposix_conf.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-libposix_conf.o `test -f 'libposix_conf.c' || echo '$(srcdir)/'`libposix_conf.c diff --git a/newlib/libc/sys/jehanne/fputs.c b/newlib/libc/sys/jehanne/fputs.c new file mode 100644 index 000000000..6ed842f18 --- /dev/null +++ b/newlib/libc/sys/jehanne/fputs.c @@ -0,0 +1,173 @@ +/* + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +/* +FUNCTION +<>, <>---write a character string in a file or stream + +INDEX + fputs +INDEX + fputs_unlocked +INDEX + _fputs_r +INDEX + _fputs_unlocked_r + +ANSI_SYNOPSIS + #include + int fputs(const char *restrict <[s]>, FILE *restrict <[fp]>); + + #define _GNU_SOURCE + #include + int fputs_unlocked(const char *restrict <[s]>, FILE *restrict <[fp]>); + + #include + int _fputs_r(struct _reent *<[ptr]>, const char *restrict <[s]>, FILE *restrict <[fp]>); + + #include + int _fputs_unlocked_r(struct _reent *<[ptr]>, const char *restrict <[s]>, FILE *restrict <[fp]>); + +TRAD_SYNOPSIS + #include + int fputs(<[s]>, <[fp]>) + char *<[s]>; + FILE *<[fp]>; + + #define _GNU_SOURCE + #include + int fputs_unlocked(<[s]>, <[fp]>) + char *<[s]>; + FILE *<[fp]>; + + #include + int _fputs_r(<[ptr]>, <[s]>, <[fp]>) + struct _reent *<[ptr]>; + char *<[s]>; + FILE *<[fp]>; + + #include + int _fputs_unlocked_r(<[ptr]>, <[s]>, <[fp]>) + struct _reent *<[ptr]>; + char *<[s]>; + FILE *<[fp]>; + +DESCRIPTION +<> writes the string at <[s]> (but without the trailing null) +to the file or stream identified by <[fp]>. + +<> is a non-thread-safe version of <>. +<> may only safely be used within a scope +protected by flockfile() (or ftrylockfile()) and funlockfile(). This +function may safely be used in a multi-threaded program if and only +if they are called while the invoking thread owns the (FILE *) +object, as is the case after a successful call to the flockfile() or +ftrylockfile() functions. If threads are disabled, then +<> is equivalent to <>. + +<<_fputs_r>> and <<_fputs_unlocked_r>> are simply reentrant versions of the +above that take an additional reentrant struct pointer argument: <[ptr]>. + +RETURNS +If successful, the result is <<0>>; otherwise, the result is <>. + +PORTABILITY +ANSI C requires <>, but does not specify that the result on +success must be <<0>>; any non-negative value is permitted. + +<> is a GNU extension. + +Supporting OS subroutines required: <>, <>, <>, +<>, <>, <>, <>. +*/ + +#include <_ansi.h> +#include +#include +#include "fvwrite.h" +#include "local.h" + +#ifdef __IMPL_UNLOCKED__ +#define _fputs_r _fputs_unlocked_r +#define fputs fputs_unlocked +#endif + +/* + * Write the given string to the given file. + */ +#pragma GCC push_option +#pragma GCC optimize("O0") + +int +_DEFUN(_fputs_r, (ptr, s, fp), + struct _reent * ptr _AND + char _CONST *__restrict s _AND + FILE *__restrict fp) +{ +#ifdef _FVWRITE_IN_STREAMIO + int result; + struct __suio uio = {0, 0, 0}; + struct __siov iov; + + iov.iov_base = s; + iov.iov_len = uio.uio_resid = strlen (s); + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + + CHECK_INIT(ptr, fp); + + _newlib_flockfile_start (fp); + ORIENT (fp, -1); + result = __sfvwrite_r (ptr, fp, &uio); + _newlib_flockfile_end (fp); + return result; +#else + _CONST char *p = s; + + CHECK_INIT(ptr, fp); + + _newlib_flockfile_start (fp); + ORIENT (fp, -1); + /* Make sure we can write. */ + if (cantwrite (ptr, fp)) + goto error; + + while (*p) + { + if (__sputc_r (ptr, *p++, fp) == EOF) + goto error; + } + _newlib_flockfile_exit (fp); + return 0; + +error: + _newlib_flockfile_end (fp); + return EOF; +#endif +} + +#pragma GCC pop_option + +#ifndef _REENT_ONLY +int +_DEFUN(fputs, (s, fp), + char _CONST *__restrict s _AND + FILE *__restrict fp) +{ + return _fputs_r (_REENT, s, fp); +} +#endif /* !_REENT_ONLY */ diff --git a/newlib/libc/sys/jehanne/fputws.c b/newlib/libc/sys/jehanne/fputws.c new file mode 100644 index 000000000..36bcd8abd --- /dev/null +++ b/newlib/libc/sys/jehanne/fputws.c @@ -0,0 +1,198 @@ +/*- + * Copyright (c) 2002-2004 Tim J. Robbins. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* +FUNCTION +<>, <>---write a wide character string in a file or stream + +INDEX + fputws +INDEX + fputws_unlocked +INDEX + _fputws_r +INDEX + _fputws_unlocked_r + +ANSI_SYNOPSIS + #include + int fputws(const wchar_t *__restrict <[ws]>, FILE *__restrict <[fp]>); + + #define _GNU_SOURCE + #include + int fputws_unlocked(const wchar_t *__restrict <[ws]>, FILE *__restrict <[fp]>); + + #include + int _fputws_r(struct _reent *<[ptr]>, const wchar_t *<[ws]>, + FILE *<[fp]>); + + #include + int _fputws_unlocked_r(struct _reent *<[ptr]>, const wchar_t *<[ws]>, + FILE *<[fp]>); + +TRAD_SYNOPSIS + #include + int fputws(<[ws]>, <[fp]>) + wchar_t *__restrict <[ws]>; + FILE *__restrict <[fp]>; + + #define _GNU_SOURCE + #include + int fputws_unlocked(<[ws]>, <[fp]>) + wchar_t *__restrict <[ws]>; + FILE *__restrict <[fp]>; + + #include + int _fputws_r(<[ptr]>, <[ws]>, <[fp]>) + struct _reent *<[ptr]>; + wchar_t *<[ws]>; + FILE *<[fp]>; + + #include + int _fputws_unlocked_r(<[ptr]>, <[ws]>, <[fp]>) + struct _reent *<[ptr]>; + wchar_t *<[ws]>; + FILE *<[fp]>; + +DESCRIPTION +<> writes the wide character string at <[ws]> (but without the +trailing null) to the file or stream identified by <[fp]>. + +<> is a non-thread-safe version of <>. +<> may only safely be used within a scope +protected by flockfile() (or ftrylockfile()) and funlockfile(). This +function may safely be used in a multi-threaded program if and only +if they are called while the invoking thread owns the (FILE *) +object, as is the case after a successful call to the flockfile() or +ftrylockfile() functions. If threads are disabled, then +<> is equivalent to <>. + +<<_fputws_r>> and <<_fputws_unlocked_r>> are simply reentrant versions of the +above that take an additional reentrant struct pointer argument: <[ptr]>. + +RETURNS +If successful, the result is a non-negative integer; otherwise, the result +is <<-1>> to indicate an error. + +PORTABILITY +<> is required by C99 and POSIX.1-2001. + +<> is a GNU extension. +*/ + +#include <_ansi.h> +#include +#include +#include +#include +#include +#include "fvwrite.h" +#include "local.h" + +#ifdef __IMPL_UNLOCKED__ +#define _fputws_r _fputws_unlocked_r +#define fputws fputws_unlocked +#endif + +#pragma GCC push_option +#pragma GCC optimize("O0") + +int +_DEFUN(_fputws_r, (ptr, ws, fp), + struct _reent *ptr _AND + const wchar_t *ws _AND + FILE *fp) +{ + size_t nbytes; + char buf[BUFSIZ]; +#ifdef _FVWRITE_IN_STREAMIO + struct __suio uio = {0, 0, 0}; + struct __siov iov; + + _newlib_flockfile_start (fp); + ORIENT (fp, 1); + if (cantwrite (ptr, fp) != 0) + goto error; + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + iov.iov_base = buf; + do + { + nbytes = _wcsrtombs_r(ptr, buf, &ws, sizeof (buf), &fp->_mbstate); + if (nbytes == (size_t) -1) + goto error; + iov.iov_len = uio.uio_resid = nbytes; + if (__sfvwrite_r(ptr, fp, &uio) != 0) + goto error; + } + while (ws != NULL); + _newlib_flockfile_exit (fp); + return (0); + +error: + _newlib_flockfile_end (fp); + return (-1); +#else + _newlib_flockfile_start (fp); + ORIENT (fp, 1); + if (cantwrite (ptr, fp) != 0) + goto error; + + do + { + size_t i = 0; + nbytes = _wcsrtombs_r (ptr, buf, &ws, sizeof (buf), &fp->_mbstate); + if (nbytes == (size_t) -1) + goto error; + while (i < nbytes) + { + if (__sputc_r (ptr, buf[i], fp) == EOF) + goto error; + i++; + } + } + while (ws != NULL); + _newlib_flockfile_exit (fp); + return (0); + +error: + _newlib_flockfile_end (fp); + return (-1); +#endif +} + +#pragma GCC pop_option + +int +_DEFUN(fputws, (ws, fp), + const wchar_t *__restrict ws _AND + FILE *__restrict fp) +{ + struct _reent *reent = _REENT; + + CHECK_INIT (reent, fp); + return _fputws_r (reent, ws, fp); +} diff --git a/newlib/libc/sys/jehanne/fvwrite.c b/newlib/libc/sys/jehanne/fvwrite.c new file mode 100644 index 000000000..49d9da75b --- /dev/null +++ b/newlib/libc/sys/jehanne/fvwrite.c @@ -0,0 +1,271 @@ +/* + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ +/* No user fns here. Pesch 15apr92. */ + +#include <_ansi.h> +#include +#include +#include +#include +#include +#include "local.h" +#include "fvwrite.h" + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define COPY(n) _CAST_VOID memmove ((_PTR) fp->_p, (_PTR) p, (size_t) (n)) + +#define GETIOV(extra_work) \ + while (len == 0) \ + { \ + extra_work; \ + p = iov->iov_base; \ + len = iov->iov_len; \ + iov++; \ + } + +/* + * Write some memory regions. Return zero on success, EOF on error. + * + * This routine is large and unsightly, but most of the ugliness due + * to the three different kinds of output buffering is handled here. + */ + +#pragma GCC push_option +#pragma GCC optimize("O0") + +int +_DEFUN(__sfvwrite_r, (ptr, fp, uio), + struct _reent *ptr _AND + register FILE *fp _AND + register struct __suio *uio) +{ + register size_t len; + register _CONST char *p = NULL; + register struct __siov *iov; + register _READ_WRITE_RETURN_TYPE w, s; + char *nl; + int nlknown, nldist; + + if ((len = uio->uio_resid) == 0) + return 0; + + /* make sure we can write */ + if (cantwrite (ptr, fp)) + return EOF; + + iov = uio->uio_iov; + len = 0; + +#ifdef __SCLE + if (fp->_flags & __SCLE) /* text mode */ + { + do + { + GETIOV (;); + while (len > 0) + { + if (putc (*p, fp) == EOF) + return EOF; + p++; + len--; + uio->uio_resid--; + } + } + while (uio->uio_resid > 0); + return 0; + } +#endif + + if (fp->_flags & __SNBF) + { + /* + * Unbuffered: Split buffer in the largest multiple of BUFSIZ < INT_MAX + * as some legacy code may expect int instead of size_t. + */ + do + { + GETIOV (;); + w = fp->_write (ptr, fp->_cookie, p, + MIN (len, INT_MAX - INT_MAX % BUFSIZ)); + if (w <= 0) + goto err; + p += w; + len -= w; + } + while ((uio->uio_resid -= w) != 0); + } + else if ((fp->_flags & __SLBF) == 0) + { + /* + * Fully buffered: fill partially full buffer, if any, + * and then flush. If there is no partial buffer, write + * one _bf._size byte chunk directly (without copying). + * + * String output is a special case: write as many bytes + * as fit, but pretend we wrote everything. This makes + * snprintf() return the number of bytes needed, rather + * than the number used, and avoids its write function + * (so that the write function can be invalid). If + * we are dealing with the asprintf routines, we will + * dynamically increase the buffer size as needed. + */ + do + { + GETIOV (;); + w = fp->_w; + if (fp->_flags & __SSTR) + { + 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) + { + ptr->_errno = 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 buffer which is no longer used and clear + __SMBF flag to avoid double free in fclose. */ + _free_r (ptr, fp->_bf._base); + fp->_flags &= ~__SMBF; + /* Ensure correct errno, even if free changed it. */ + ptr->_errno = 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; + COPY (w); /* copy MIN(fp->_w,len), */ + fp->_w -= w; + fp->_p += w; + w = len; /* but pretend copied all */ + } + else if (fp->_p > fp->_bf._base || len < fp->_bf._size) + { + /* pass through the buffer */ + w = MIN (len, w); + COPY (w); + fp->_w -= w; + fp->_p += w; + if (fp->_w == 0 && _fflush_r (ptr, fp)) + goto err; + } + else + { + /* write directly */ + w = ((int)MIN (len, INT_MAX)) / fp->_bf._size * fp->_bf._size; + w = fp->_write (ptr, fp->_cookie, p, w); + if (w <= 0) + goto err; + } + p += w; + len -= w; + } + while ((uio->uio_resid -= w) != 0); + } + else + { + /* + * Line buffered: like fully buffered, but we + * must check for newlines. Compute the distance + * to the first newline (including the newline), + * or `infinity' if there is none, then pretend + * that the amount to write is MIN(len,nldist). + */ + nlknown = 0; + nldist = 0; + do + { + GETIOV (nlknown = 0); + if (!nlknown) + { + nl = memchr ((_PTR) p, '\n', len); + nldist = nl ? nl + 1 - p : len + 1; + nlknown = 1; + } + s = MIN (len, nldist); + w = fp->_w + fp->_bf._size; + if (fp->_p > fp->_bf._base && s > w) + { + COPY (w); + /* fp->_w -= w; */ + fp->_p += w; + if (_fflush_r (ptr, fp)) + goto err; + } + else if (s >= (w = fp->_bf._size)) + { + w = fp->_write (ptr, fp->_cookie, p, w); + if (w <= 0) + goto err; + } + else + { + w = s; + COPY (w); + fp->_w -= w; + fp->_p += w; + } + if ((nldist -= w) == 0) + { + /* copied the newline: flush and forget */ + if (_fflush_r (ptr, fp)) + goto err; + nlknown = 0; + } + p += w; + len -= w; + } + while ((uio->uio_resid -= w) != 0); + } + return 0; + +err: + fp->_flags |= __SERR; + return EOF; +} + +#pragma GCC pop_option diff --git a/newlib/libc/sys/jehanne/fvwrite.h b/newlib/libc/sys/jehanne/fvwrite.h new file mode 100644 index 000000000..5c078fe68 --- /dev/null +++ b/newlib/libc/sys/jehanne/fvwrite.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 1990, 2007 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +/* %W% (Berkeley) %G% */ +#include <_ansi.h> + +/* + * I/O descriptors for __sfvwrite_r(). + */ +struct __siov { + _CONST _PTR iov_base; + size_t iov_len; +}; +struct __suio { + struct __siov *uio_iov; + int uio_iovcnt; + size_t uio_resid; +}; + + +extern int _EXFUN(__sfvwrite_r,(struct _reent *, FILE *, struct __suio *)); +extern int _EXFUN(__swsetup_r,(struct _reent *, FILE *)); diff --git a/newlib/libc/sys/jehanne/fwrite.c b/newlib/libc/sys/jehanne/fwrite.c new file mode 100644 index 000000000..f5ef01c23 --- /dev/null +++ b/newlib/libc/sys/jehanne/fwrite.c @@ -0,0 +1,218 @@ +/* + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +/* +FUNCTION +<>, <>---write array elements + +INDEX + fwrite +INDEX + fwrite_unlocked +INDEX + _fwrite_r +INDEX + _fwrite_unlocked_r + +ANSI_SYNOPSIS + #include + size_t fwrite(const void *restrict <[buf]>, size_t <[size]>, + size_t <[count]>, FILE *restrict <[fp]>); + + #define _BSD_SOURCE + #include + size_t fwrite_unlocked(const void *restrict <[buf]>, size_t <[size]>, + size_t <[count]>, FILE *restrict <[fp]>); + + #include + size_t _fwrite_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>, + size_t <[count]>, FILE *restrict <[fp]>); + + #include + size_t _fwrite_unlocked_r(struct _reent *<[ptr]>, const void *restrict <[buf]>, size_t <[size]>, + size_t <[count]>, FILE *restrict <[fp]>); + +TRAD_SYNOPSIS + #include + size_t fwrite(<[buf]>, <[size]>, <[count]>, <[fp]>) + char *<[buf]>; + size_t <[size]>; + size_t <[count]>; + FILE *<[fp]>; + + #define _BSD_SOURCE + #include + size_t fwrite_unlocked(<[buf]>, <[size]>, <[count]>, <[fp]>) + char *<[buf]>; + size_t <[size]>; + size_t <[count]>; + FILE *<[fp]>; + + #include + size_t _fwrite_r(<[ptr]>, <[buf]>, <[size]>, <[count]>, <[fp]>) + struct _reent *<[ptr]>; + char *<[buf]>; + size_t <[size]>; + size_t <[count]>; + FILE *<[fp]>; + + #include + size_t _fwrite_unlocked_r(<[ptr]>, <[buf]>, <[size]>, <[count]>, <[fp]>) + struct _reent *<[ptr]>; + char *<[buf]>; + size_t <[size]>; + size_t <[count]>; + FILE *<[fp]>; + +DESCRIPTION +<> attempts to copy, starting from the memory location +<[buf]>, <[count]> elements (each of size <[size]>) into the file or +stream identified by <[fp]>. <> may copy fewer elements than +<[count]> if an error intervenes. + +<> also advances the file position indicator (if any) for +<[fp]> by the number of @emph{characters} actually written. + +<> is a non-thread-safe version of <>. +<> may only safely be used within a scope +protected by flockfile() (or ftrylockfile()) and funlockfile(). This +function may safely be used in a multi-threaded program if and only +if they are called while the invoking thread owns the (FILE *) +object, as is the case after a successful call to the flockfile() or +ftrylockfile() functions. If threads are disabled, then +<> is equivalent to <>. + +<<_fwrite_r>> and <<_fwrite_unlocked_r>> are simply reentrant versions of the +above that take an additional reentrant structure argument: <[ptr]>. + +RETURNS +If <> succeeds in writing all the elements you specify, the +result is the same as the argument <[count]>. In any event, the +result is the number of complete elements that <> copied to +the file. + +PORTABILITY +ANSI C requires <>. + +<> is a BSD extension also provided by GNU libc. + +Supporting OS subroutines required: <>, <>, <>, +<>, <>, <>, <>. +*/ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "%W% (Berkeley) %G%"; +#endif /* LIBC_SCCS and not lint */ + +#include <_ansi.h> +#include +#include +#if 0 +#include +#endif +#include "local.h" +#if 1 +#include "fvwrite.h" +#endif + +#ifdef __IMPL_UNLOCKED__ +#define _fwrite_r _fwrite_unlocked_r +#define fwrite fwrite_unlocked +#endif + +/* + * Write `count' objects (each size `size') from memory to the given file. + * Return the number of whole objects written. + */ + +#pragma GCC push_option +#pragma GCC optimize("O0") + +size_t +_DEFUN(_fwrite_r, (ptr, buf, size, count, fp), + struct _reent * ptr _AND + _CONST _PTR __restrict buf _AND + size_t size _AND + size_t count _AND + FILE * __restrict fp) +{ + size_t n; +#ifdef _FVWRITE_IN_STREAMIO + struct __suio uio = {0, 0, 0}; + struct __siov iov; + + iov.iov_base = buf; + uio.uio_resid = iov.iov_len = n = count * size; + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + + /* + * The usual case is success (__sfvwrite_r returns 0); + * skip the divide if this happens, since divides are + * generally slow and since this occurs whenever size==0. + */ + + CHECK_INIT(ptr, fp); + + _newlib_flockfile_start (fp); + ORIENT (fp, -1); + if (__sfvwrite_r (ptr, fp, &uio) == 0) + { + _newlib_flockfile_exit (fp); + return count; + } + _newlib_flockfile_end (fp); + return (n - uio.uio_resid) / size; +#else + size_t i = 0; + _CONST char *p = buf; + n = count * size; + CHECK_INIT (ptr, fp); + + _newlib_flockfile_start (fp); + ORIENT (fp, -1); + /* Make sure we can write. */ + if (cantwrite (ptr, fp)) + goto ret; + + while (i < n) + { + if (__sputc_r (ptr, p[i], fp) == EOF) + break; + + i++; + } + +ret: + _newlib_flockfile_end (fp); + return i / size; +#endif +} + +#pragma GCC pop_option + +#ifndef _REENT_ONLY +size_t +_DEFUN(fwrite, (buf, size, count, fp), + _CONST _PTR __restrict buf _AND + size_t size _AND + size_t count _AND + FILE * fp) +{ + return _fwrite_r (_REENT, buf, size, count, fp); +} +#endif diff --git a/newlib/libc/sys/jehanne/local.h b/newlib/libc/sys/jehanne/local.h new file mode 100644 index 000000000..5f6995501 --- /dev/null +++ b/newlib/libc/sys/jehanne/local.h @@ -0,0 +1,352 @@ +/* + * Copyright (c) 1990, 2007 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + * + * %W% (UofMD/Berkeley) %G% + */ + +/* + * Information local to this implementation of stdio, + * in particular, macros and private variables. + */ + +#include <_ansi.h> +#include +#include +#include +#include +#include +#ifdef __SCLE +# include +#endif + +/* The following define determines if the per-reent stdin, stdout and stderr + streams are closed during _reclaim_reent(). The stdin, stdout and stderr + streams are initialized to use file descriptors 0, 1 and 2 respectively. In + case _STDIO_CLOSE_PER_REENT_STD_STREAMS is defined these file descriptors + will be closed via close() provided the owner of the reent structure + triggerd the on demand reent initilization, see CHECK_INIT(). */ +#if !defined(__rtems__) && !defined(__tirtos__) +#define _STDIO_CLOSE_PER_REENT_STD_STREAMS +#endif + +/* The following macros are supposed to replace calls to _flockfile/_funlockfile + and __sfp_lock_acquire/__sfp_lock_release. In case of multi-threaded + environments using pthreads, it's not sufficient to lock the stdio functions + against concurrent threads accessing the same data, the locking must also be + secured against thread cancellation. + + The below macros have to be used in pairs. The _newlib_XXX_start macro + starts with a opening curly brace, the _newlib_XXX_end macro ends with a + closing curly brace, so the start macro and the end macro mark the code + start and end of a critical section. In case the code leaves the critical + section before reaching the end of the critical section's code end, use + the appropriate _newlib_XXX_exit macro. */ + +#if !defined (__SINGLE_THREAD__) && defined (_POSIX_THREADS) \ + && !defined (__rtems__) +#define _STDIO_WITH_THREAD_CANCELLATION_SUPPORT +#endif + +#if defined(__SINGLE_THREAD__) || defined(__IMPL_UNLOCKED__) + +# define _newlib_flockfile_start(_fp) +# define _newlib_flockfile_exit(_fp) +# define _newlib_flockfile_end(_fp) +# define _newlib_sfp_lock_start() +# define _newlib_sfp_lock_exit() +# define _newlib_sfp_lock_end() + +#elif defined(_STDIO_WITH_THREAD_CANCELLATION_SUPPORT) +#include + +/* Start a stream oriented critical section: */ +# define _newlib_flockfile_start(_fp) \ + { \ + int __oldfpcancel; \ + pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &__oldfpcancel); \ + if (!(_fp->_flags2 & __SNLK)) \ + _flockfile (_fp) + +/* Exit from a stream oriented critical section prematurely: */ +# define _newlib_flockfile_exit(_fp) \ + if (!(_fp->_flags2 & __SNLK)) \ + _funlockfile (_fp); \ + pthread_setcancelstate (__oldfpcancel, &__oldfpcancel); + +/* End a stream oriented critical section: */ +# define _newlib_flockfile_end(_fp) \ + if (!(_fp->_flags2 & __SNLK)) \ + _funlockfile (_fp); \ + pthread_setcancelstate (__oldfpcancel, &__oldfpcancel); \ + } + +/* Start a stream list oriented critical section: */ +# define _newlib_sfp_lock_start() \ + { \ + int __oldsfpcancel; \ + pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &__oldsfpcancel); \ + __sfp_lock_acquire () + +/* Exit from a stream list oriented critical section prematurely: */ +# define _newlib_sfp_lock_exit() \ + __sfp_lock_release (); \ + pthread_setcancelstate (__oldsfpcancel, &__oldsfpcancel); + +/* End a stream list oriented critical section: */ +# define _newlib_sfp_lock_end() \ + __sfp_lock_release (); \ + pthread_setcancelstate (__oldsfpcancel, &__oldsfpcancel); \ + } + +#else /* !__SINGLE_THREAD__ && !__IMPL_UNLOCKED__ && !_STDIO_WITH_THREAD_CANCELLATION_SUPPORT */ + +# define _newlib_flockfile_start(_fp) \ + { \ + if (!(_fp->_flags2 & __SNLK)) \ + _flockfile (_fp) + +# define _newlib_flockfile_exit(_fp) \ + if (!(_fp->_flags2 & __SNLK)) \ + _funlockfile(_fp); \ + +# define _newlib_flockfile_end(_fp) \ + if (!(_fp->_flags2 & __SNLK)) \ + _funlockfile(_fp); \ + } + +# define _newlib_sfp_lock_start() \ + { \ + __sfp_lock_acquire () + +# define _newlib_sfp_lock_exit() \ + __sfp_lock_release (); + +# define _newlib_sfp_lock_end() \ + __sfp_lock_release (); \ + } + +#endif /* __SINGLE_THREAD__ || __IMPL_UNLOCKED__ */ + +extern wint_t _EXFUN(__fgetwc, (struct _reent *, FILE *)); +extern wint_t _EXFUN(__fputwc, (struct _reent *, wchar_t, FILE *)); +extern u_char *_EXFUN(__sccl, (char *, u_char *fmt)); +extern int _EXFUN(__svfscanf_r,(struct _reent *,FILE *, _CONST char *,va_list)); +extern int _EXFUN(__ssvfscanf_r,(struct _reent *,FILE *, _CONST char *,va_list)); +extern int _EXFUN(__svfiscanf_r,(struct _reent *,FILE *, _CONST char *,va_list)); +extern int _EXFUN(__ssvfiscanf_r,(struct _reent *,FILE *, _CONST char *,va_list)); +extern int _EXFUN(__svfwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list)); +extern int _EXFUN(__ssvfwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list)); +extern int _EXFUN(__svfiwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list)); +extern int _EXFUN(__ssvfiwscanf_r,(struct _reent *,FILE *, _CONST wchar_t *,va_list)); +int _EXFUN(_svfprintf_r,(struct _reent *, FILE *, const char *, + va_list) + _ATTRIBUTE ((__format__ (__printf__, 3, 0)))); +int _EXFUN(_svfiprintf_r,(struct _reent *, FILE *, const char *, + va_list) + _ATTRIBUTE ((__format__ (__printf__, 3, 0)))); +int _EXFUN(_svfwprintf_r,(struct _reent *, FILE *, const wchar_t *, + va_list)); +int _EXFUN(_svfiwprintf_r,(struct _reent *, FILE *, const wchar_t *, + va_list)); +extern FILE *_EXFUN(__sfp,(struct _reent *)); +extern int _EXFUN(__sflags,(struct _reent *,_CONST char*, int*)); +extern int _EXFUN(__sflush_r,(struct _reent *,FILE *)); +#ifdef _STDIO_BSD_SEMANTICS +extern int _EXFUN(__sflushw_r,(struct _reent *,FILE *)); +#endif +extern int _EXFUN(__srefill_r,(struct _reent *,FILE *)); +extern _READ_WRITE_RETURN_TYPE _EXFUN(__sread,(struct _reent *, void *, char *, + _READ_WRITE_BUFSIZE_TYPE)); +extern _READ_WRITE_RETURN_TYPE _EXFUN(__seofread,(struct _reent *, void *, + char *, + _READ_WRITE_BUFSIZE_TYPE)); +extern _READ_WRITE_RETURN_TYPE _EXFUN(__swrite,(struct _reent *, void *, + const char *, + _READ_WRITE_BUFSIZE_TYPE)); +extern _fpos_t _EXFUN(__sseek,(struct _reent *, void *, _fpos_t, int)); +extern int _EXFUN(__sclose,(struct _reent *, void *)); +extern int _EXFUN(__stextmode,(int)); +extern _VOID _EXFUN(__sinit,(struct _reent *)); +extern _VOID _EXFUN(_cleanup_r,(struct _reent *)); +extern _VOID _EXFUN(__smakebuf_r,(struct _reent *, FILE *)); +extern int _EXFUN(__swhatbuf_r,(struct _reent *, FILE *, size_t *, int *)); +extern int _EXFUN(_fwalk,(struct _reent *, int (*)(FILE *))); +extern int _EXFUN(_fwalk_reent,(struct _reent *, int (*)(struct _reent *, FILE *))); +struct _glue * _EXFUN(__sfmoreglue,(struct _reent *,int n)); +extern int _EXFUN(__submore, (struct _reent *, FILE *)); + +#ifdef __LARGE64_FILES +extern _fpos64_t _EXFUN(__sseek64,(struct _reent *, void *, _fpos64_t, int)); +extern _READ_WRITE_RETURN_TYPE _EXFUN(__swrite64,(struct _reent *, void *, + const char *, + _READ_WRITE_BUFSIZE_TYPE)); +#endif + +/* Called by the main entry point fns to ensure stdio has been initialized. */ + +#ifdef _REENT_SMALL +#define CHECK_INIT(ptr, fp) \ + do \ + { \ + struct _reent *_check_init_ptr = (ptr); \ + if ((_check_init_ptr) && !(_check_init_ptr)->__sdidinit) \ + __sinit (_check_init_ptr); \ + if ((fp) == (FILE *)&__sf_fake_stdin) \ + (fp) = _stdin_r(_check_init_ptr); \ + else if ((fp) == (FILE *)&__sf_fake_stdout) \ + (fp) = _stdout_r(_check_init_ptr); \ + else if ((fp) == (FILE *)&__sf_fake_stderr) \ + (fp) = _stderr_r(_check_init_ptr); \ + } \ + while (0) +#else /* !_REENT_SMALL */ +#define CHECK_INIT(ptr, fp) \ + do \ + { \ + struct _reent *_check_init_ptr = (ptr); \ + if ((_check_init_ptr) && !(_check_init_ptr)->__sdidinit) \ + __sinit (_check_init_ptr); \ + } \ + while (0) +#endif /* !_REENT_SMALL */ + +#define CHECK_STD_INIT(ptr) \ + do \ + { \ + struct _reent *_check_init_ptr = (ptr); \ + if ((_check_init_ptr) && !(_check_init_ptr)->__sdidinit) \ + __sinit (_check_init_ptr); \ + } \ + while (0) + +/* Return true and set errno and stream error flag iff the given FILE + cannot be written now. */ + +#define cantwrite(ptr, fp) \ + ((((fp)->_flags & __SWR) == 0 || (fp)->_bf._base == NULL) && \ + __swsetup_r(ptr, fp)) + +/* Test whether the given stdio file has an active ungetc buffer; + release such a buffer, without restoring ordinary unread data. */ + +#define HASUB(fp) ((fp)->_ub._base != NULL) +#define FREEUB(ptr, fp) { \ + if ((fp)->_ub._base != (fp)->_ubuf) \ + _free_r(ptr, (char *)(fp)->_ub._base); \ + (fp)->_ub._base = NULL; \ +} + +/* Test for an fgetline() buffer. */ + +#define HASLB(fp) ((fp)->_lb._base != NULL) +#define FREELB(ptr, fp) { _free_r(ptr,(char *)(fp)->_lb._base); \ + (fp)->_lb._base = NULL; } + +#ifdef _WIDE_ORIENT +/* + * Set the orientation for a stream. If o > 0, the stream has wide- + * orientation. If o < 0, the stream has byte-orientation. + */ +#define ORIENT(fp,ori) \ + do \ + { \ + if (!((fp)->_flags & __SORD)) \ + { \ + (fp)->_flags |= __SORD; \ + if (ori > 0) \ + (fp)->_flags2 |= __SWID; \ + else \ + (fp)->_flags2 &= ~__SWID; \ + } \ + } \ + while (0) +#else +#define ORIENT(fp,ori) +#endif + +/* WARNING: _dcvt is defined in the stdlib directory, not here! */ + +char *_EXFUN(_dcvt,(struct _reent *, char *, double, int, int, char, int)); +char *_EXFUN(_sicvt,(char *, short, char)); +char *_EXFUN(_icvt,(char *, int, char)); +char *_EXFUN(_licvt,(char *, long, char)); +#ifdef __GNUC__ +char *_EXFUN(_llicvt,(char *, long long, char)); +#endif + +#define CVT_BUF_SIZE 128 + +#define NDYNAMIC 4 /* add four more whenever necessary */ + +#ifdef __SINGLE_THREAD__ +#define __sfp_lock_acquire() +#define __sfp_lock_release() +#define __sinit_lock_acquire() +#define __sinit_lock_release() +#else +_VOID _EXFUN(__sfp_lock_acquire,(_VOID)); +_VOID _EXFUN(__sfp_lock_release,(_VOID)); +_VOID _EXFUN(__sinit_lock_acquire,(_VOID)); +_VOID _EXFUN(__sinit_lock_release,(_VOID)); +#endif + +/* Types used in positional argument support in vfprinf/vfwprintf. + The implementation is char/wchar_t dependent but the class and state + tables are only defined once in vfprintf.c. */ +typedef enum __packed { + ZERO, /* '0' */ + DIGIT, /* '1-9' */ + DOLLAR, /* '$' */ + MODFR, /* spec modifier */ + SPEC, /* format specifier */ + DOT, /* '.' */ + STAR, /* '*' */ + FLAG, /* format flag */ + OTHER, /* all other chars */ + MAX_CH_CLASS /* place-holder */ +} __CH_CLASS; + +typedef enum __packed { + START, /* start */ + SFLAG, /* seen a flag */ + WDIG, /* seen digits in width area */ + WIDTH, /* processed width */ + SMOD, /* seen spec modifier */ + SDOT, /* seen dot */ + VARW, /* have variable width specifier */ + VARP, /* have variable precision specifier */ + PREC, /* processed precision */ + VWDIG, /* have digits in variable width specification */ + VPDIG, /* have digits in variable precision specification */ + DONE, /* done */ + MAX_STATE, /* place-holder */ +} __STATE; + +typedef enum __packed { + NOOP, /* do nothing */ + NUMBER, /* build a number from digits */ + SKIPNUM, /* skip over digits */ + GETMOD, /* get and process format modifier */ + GETARG, /* get and process argument */ + GETPW, /* get variable precision or width */ + GETPWB, /* get variable precision or width and pushback fmt char */ + GETPOS, /* get positional parameter value */ + PWPOS, /* get positional parameter value for variable width or precision */ +} __ACTION; + +extern _CONST __CH_CLASS __chclass[256]; +extern _CONST __STATE __state_table[MAX_STATE][MAX_CH_CLASS]; +extern _CONST __ACTION __action_table[MAX_STATE][MAX_CH_CLASS]; diff --git a/newlib/libc/sys/jehanne/puts.c b/newlib/libc/sys/jehanne/puts.c new file mode 100644 index 000000000..1a929777c --- /dev/null +++ b/newlib/libc/sys/jehanne/puts.c @@ -0,0 +1,148 @@ +/* + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ + +/* +FUNCTION +<>---write a character string + +INDEX + puts +INDEX + _puts_r + +ANSI_SYNOPSIS + #include + int puts(const char *<[s]>); + + int _puts_r(struct _reent *<[reent]>, const char *<[s]>); + +TRAD_SYNOPSIS + #include + int puts(<[s]>) + char *<[s]>; + + int _puts_r(<[reent]>, <[s]>) + struct _reent *<[reent]>; + char *<[s]>; + +DESCRIPTION +<> writes the string at <[s]> (followed by a newline, instead of +the trailing null) to the standard output stream. + +The alternate function <<_puts_r>> is a reentrant version. The extra +argument <[reent]> is a pointer to a reentrancy structure. + +RETURNS +If successful, the result is a nonnegative integer; otherwise, the +result is <>. + +PORTABILITY +ANSI C requires <>, but does not specify that the result on +success must be <<0>>; any non-negative value is permitted. + +Supporting OS subroutines required: <>, <>, <>, +<>, <>, <>, <>. +*/ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "%W% (Berkeley) %G%"; +#endif /* LIBC_SCCS and not lint */ + +#include <_ansi.h> +#include +#include +#include +#include "fvwrite.h" +#include "local.h" + +/* + * Write the given string to stdout, appending a newline. + */ + +#pragma GCC push_option +#pragma GCC optimize("O0") + +int +_DEFUN(_puts_r, (ptr, s), + struct _reent *ptr _AND + _CONST char * s) +{ +#ifdef _FVWRITE_IN_STREAMIO + int result; + size_t c = strlen (s); + struct __suio uio = {0, 0, 0}; + struct __siov iov[2]; + FILE *fp; + + iov[0].iov_base = s; + iov[0].iov_len = c; + iov[1].iov_base = "\n"; + iov[1].iov_len = 1; + uio.uio_resid = c + 1; + uio.uio_iov = &iov[0]; + uio.uio_iovcnt = 2; + + _REENT_SMALL_CHECK_INIT (ptr); + fp = _stdout_r (ptr); + CHECK_INIT (ptr, fp); + _newlib_flockfile_start (fp); + ORIENT (fp, -1); + result = (__sfvwrite_r (ptr, fp, &uio) ? EOF : '\n'); + _newlib_flockfile_end (fp); + return result; +#else + int result = EOF; + const char *p = s; + FILE *fp; + _REENT_SMALL_CHECK_INIT (ptr); + + fp = _stdout_r (ptr); + CHECK_INIT (ptr, fp); + _newlib_flockfile_start (fp); + ORIENT (fp, -1); + /* Make sure we can write. */ + if (cantwrite (ptr, fp)) + goto err; + + while (*p) + { + if (__sputc_r (ptr, *p++, fp) == EOF) + goto err; + } + if (__sputc_r (ptr, '\n', fp) == EOF) + goto err; + + result = '\n'; + +err: + _newlib_flockfile_end (fp); + return result; +#endif +} + +#pragma GCC pop_option + +#ifndef _REENT_ONLY + +int +_DEFUN(puts, (s), + char _CONST * s) +{ + return _puts_r (_REENT, s); +} + +#endif diff --git a/newlib/libc/sys/jehanne/vfieeefp.h b/newlib/libc/sys/jehanne/vfieeefp.h new file mode 100644 index 000000000..e85cabb3c --- /dev/null +++ b/newlib/libc/sys/jehanne/vfieeefp.h @@ -0,0 +1,284 @@ +/**************************************************************** + * + * The author of this software is David M. Gay. + * + * Copyright (c) 1991 by AT&T. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose without fee is hereby granted, provided that this entire notice + * is included in all copies of any software which is or includes a copy + * or modification of this software and in all copies of the supporting + * documentation for such software. + * + * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED + * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY + * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY + * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. + * + ***************************************************************/ + +/* Please send bug reports to + David M. Gay + AT&T Bell Laboratories, Room 2C-463 + 600 Mountain Avenue + Murray Hill, NJ 07974-2070 + U.S.A. + dmg@research.att.com or research!dmg + */ + +/* This header file is a modification of mprec.h that only contains floating + point union code. */ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __IEEE_LITTLE_ENDIAN +#define IEEE_8087 +#endif + +#ifdef __IEEE_BIG_ENDIAN +#define IEEE_MC68k +#endif + +#ifdef __Z8000__ +#define Just_16 +#endif + +#ifdef Unsigned_Shifts +#define Sign_Extend(a,b) if (b < 0) a |= (__uint32_t)0xffff0000; +#else +#define Sign_Extend(a,b) /*no-op*/ +#endif + +#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1 +Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined. +#endif + +#ifdef _WANT_IO_LONG_DOUBLE +/* If we are going to examine or modify specific bits in a long double using + the lword0 or lwordx macros, then we must wrap the long double inside + a union. This is necessary to avoid undefined behavior according to + the ANSI C spec. */ + +#ifdef IEEE_8087 +#if LDBL_MANT_DIG == 24 +struct ldieee +{ + unsigned manh:23; + unsigned exp:8; + unsigned sign:1; +} __packed; +#elif LDBL_MANT_DIG == 53 +struct ldieee +{ + unsigned manl:20; + unsigned manh:32; + unsigned exp:11; + unsigned sign:1; +} __packed; +#elif LDBL_MANT_DIG == 64 +struct ldieee +{ + unsigned manl:32; + unsigned manh:32; + unsigned exp:15; + unsigned sign:1; +} __packed; +#elif LDBL_MANT_DIG > 64 +struct ldieee +{ + unsigned manl3:16; + unsigned manl2:32; + unsigned manl:32; + unsigned manh:32; + unsigned exp:15; + unsigned sign:1; +} __packed; +#endif /* LDBL_MANT_DIG */ +#else /* !IEEE_8087 */ +#if LDBL_MANT_DIG == 24 +struct ldieee +{ + unsigned sign:1; + unsigned exp:8; + unsigned manh:23; +} __packed; +#elif LDBL_MANT_DIG == 53 +struct ldieee +{ + unsigned sign:1; + unsigned exp:11; + unsigned manh:32; + unsigned manl:20; +} __packed; +#elif LDBL_MANT_DIG == 64 +struct ldieee +{ + unsigned sign:1; + unsigned exp:15; + unsigned manh:32; + unsigned manl:32; +} __packed; +#elif LDBL_MANT_DIG > 64 +struct ldieee +{ + unsigned sign:1; + unsigned exp:15; + unsigned manh:32; + unsigned manl:32; + unsigned manl2:32; + unsigned manl3:16; +} __packed; +#endif /* LDBL_MANT_DIG */ +#endif /* !IEEE_8087 */ +#endif /* _WANT_IO_LONG_DOUBLE */ + +/* If we are going to examine or modify specific bits in a double using + the word0 and/or word1 macros, then we must wrap the double inside + a union. This is necessary to avoid undefined behavior according to + the ANSI C spec. */ +union double_union +{ + double d; + __uint32_t i[2]; +}; + +#ifdef IEEE_8087 +#define word0(x) (x.i[1]) +#define word1(x) (x.i[0]) +#else +#define word0(x) (x.i[0]) +#define word1(x) (x.i[1]) +#endif + +/* #define P DBL_MANT_DIG */ +/* Ten_pmax = floor(P*log(2)/log(5)) */ +/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ +/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ +/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ + +#if defined(IEEE_8087) + defined(IEEE_MC68k) +#if defined (_DOUBLE_IS_32BITS) +#define Exp_shift 23 +#define Exp_shift1 23 +#define Exp_msk1 ((__uint32_t)0x00800000L) +#define Exp_msk11 ((__uint32_t)0x00800000L) +#define Exp_mask ((__uint32_t)0x7f800000L) +#define P 24 +#define Bias 127 +#define IEEE_Arith +#define Emin (-126) +#define Exp_1 ((__uint32_t)0x3f800000L) +#define Exp_11 ((__uint32_t)0x3f800000L) +#define Ebits 8 +#define Frac_mask ((__uint32_t)0x007fffffL) +#define Frac_mask1 ((__uint32_t)0x007fffffL) +#define Ten_pmax 10 +#define Sign_bit ((__uint32_t)0x80000000L) +#define Ten_pmax 10 +#define Bletch 2 +#define Bndry_mask ((__uint32_t)0x007fffffL) +#define Bndry_mask1 ((__uint32_t)0x007fffffL) +#define LSB 1 +#define Sign_bit ((__uint32_t)0x80000000L) +#define Log2P 1 +#define Tiny0 0 +#define Tiny1 1 +#define Quick_max 5 +#define Int_max 6 +#define Infinite(x) (word0(x) == ((__uint32_t)0x7f800000L)) +#undef word0 +#undef word1 + +#define word0(x) (x.i[0]) +#define word1(x) 0 +#else + +#define Exp_shift 20 +#define Exp_shift1 20 +#define Exp_msk1 ((__uint32_t)0x100000L) +#define Exp_msk11 ((__uint32_t)0x100000L) +#define Exp_mask ((__uint32_t)0x7ff00000L) +#define P 53 +#define Bias 1023 +#define IEEE_Arith +#define Emin (-1022) +#define Exp_1 ((__uint32_t)0x3ff00000L) +#define Exp_11 ((__uint32_t)0x3ff00000L) +#define Ebits 11 +#define Frac_mask ((__uint32_t)0xfffffL) +#define Frac_mask1 ((__uint32_t)0xfffffL) +#define Ten_pmax 22 +#define Bletch 0x10 +#define Bndry_mask ((__uint32_t)0xfffffL) +#define Bndry_mask1 ((__uint32_t)0xfffffL) +#define LSB 1 +#define Sign_bit ((__uint32_t)0x80000000L) +#define Log2P 1 +#define Tiny0 0 +#define Tiny1 1 +#define Quick_max 14 +#define Int_max 14 +#define Infinite(x) (word0(x) == ((__uint32_t)0x7ff00000L)) /* sufficient test for here */ +#endif + +#else +#undef Sudden_Underflow +#define Sudden_Underflow +#ifdef IBM +#define Exp_shift 24 +#define Exp_shift1 24 +#define Exp_msk1 ((__uint32_t)0x1000000L) +#define Exp_msk11 ((__uint32_t)0x1000000L) +#define Exp_mask ((__uint32_t)0x7f000000L) +#define P 14 +#define Bias 65 +#define Exp_1 ((__uint32_t)0x41000000L) +#define Exp_11 ((__uint32_t)0x41000000L) +#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ +#define Frac_mask ((__uint32_t)0xffffffL) +#define Frac_mask1 ((__uint32_t)0xffffffL) +#define Bletch 4 +#define Ten_pmax 22 +#define Bndry_mask ((__uint32_t)0xefffffL) +#define Bndry_mask1 ((__uint32_t)0xffffffL) +#define LSB 1 +#define Sign_bit ((__uint32_t)0x80000000L) +#define Log2P 4 +#define Tiny0 ((__uint32_t)0x100000L) +#define Tiny1 0 +#define Quick_max 14 +#define Int_max 15 +#else /* VAX */ +#define Exp_shift 23 +#define Exp_shift1 7 +#define Exp_msk1 0x80 +#define Exp_msk11 ((__uint32_t)0x800000L) +#define Exp_mask ((__uint32_t)0x7f80L) +#define P 56 +#define Bias 129 +#define Exp_1 ((__uint32_t)0x40800000L) +#define Exp_11 ((__uint32_t)0x4080L) +#define Ebits 8 +#define Frac_mask ((__uint32_t)0x7fffffL) +#define Frac_mask1 ((__uint32_t)0xffff007fL) +#define Ten_pmax 24 +#define Bletch 2 +#define Bndry_mask ((__uint32_t)0xffff007fL) +#define Bndry_mask1 ((__uint32_t)0xffff007fL) +#define LSB ((__uint32_t)0x10000L) +#define Sign_bit ((__uint32_t)0x8000L) +#define Log2P 1 +#define Tiny0 0x80 +#define Tiny1 0 +#define Quick_max 15 +#define Int_max 15 +#endif +#endif + + diff --git a/newlib/libc/sys/jehanne/vfprintf.c b/newlib/libc/sys/jehanne/vfprintf.c new file mode 100644 index 000000000..cf95ba5d8 --- /dev/null +++ b/newlib/libc/sys/jehanne/vfprintf.c @@ -0,0 +1,2359 @@ +/* + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* +FUNCTION +<>, <>, <>, <>, <>, <>---format argument list + +INDEX + vfprintf +INDEX + _vfprintf_r +INDEX + vprintf +INDEX + _vprintf_r +INDEX + vsprintf +INDEX + _vsprintf_r +INDEX + vsnprintf +INDEX + _vsnprintf_r +INDEX + vasprintf +INDEX + _vasprintf_r +INDEX + vasnprintf +INDEX + _vasnprintf_r + +ANSI_SYNOPSIS + #include + #include + int vprintf(const char *<[fmt]>, va_list <[list]>); + int vfprintf(FILE *<[fp]>, const char *<[fmt]>, va_list <[list]>); + int vsprintf(char *<[str]>, const char *<[fmt]>, va_list <[list]>); + int vsnprintf(char *<[str]>, size_t <[size]>, const char *<[fmt]>, + va_list <[list]>); + int vasprintf(char **<[strp]>, const char *<[fmt]>, va_list <[list]>); + char *vasnprintf(char *<[str]>, size_t *<[size]>, const char *<[fmt]>, + va_list <[list]>); + + int _vprintf_r(struct _reent *<[reent]>, const char *<[fmt]>, + va_list <[list]>); + int _vfprintf_r(struct _reent *<[reent]>, FILE *<[fp]>, + const char *<[fmt]>, va_list <[list]>); + int _vsprintf_r(struct _reent *<[reent]>, char *<[str]>, + const char *<[fmt]>, va_list <[list]>); + int _vasprintf_r(struct _reent *<[reent]>, char **<[str]>, + const char *<[fmt]>, va_list <[list]>); + int _vsnprintf_r(struct _reent *<[reent]>, char *<[str]>, + size_t <[size]>, const char *<[fmt]>, va_list <[list]>); + char *_vasnprintf_r(struct _reent *<[reent]>, char *<[str]>, + size_t *<[size]>, const char *<[fmt]>, va_list <[list]>); + +DESCRIPTION +<>, <>, <>, <>, <>, +and <> are (respectively) variants of <>, +<>, <>, <>, <>, and +<>. They differ only in allowing their caller to pass the +variable argument list as a <> object (initialized by +<>) rather than directly accepting a variable number of +arguments. The caller is responsible for calling <>. + +<<_vprintf_r>>, <<_vfprintf_r>>, <<_vasprintf_r>>, <<_vsprintf_r>>, +<<_vsnprintf_r>>, and <<_vasnprintf_r>> are reentrant versions of the +above. + +RETURNS +The return values are consistent with the corresponding functions. + +PORTABILITY +ANSI C requires <>, <>, <>, and +<>. The remaining functions are newlib extensions. + +Supporting OS subroutines required: <>, <>, <>, +<>, <>, <>, <>. +*/ + +#if defined(LIBC_SCCS) && !defined(lint) +/*static char *sccsid = "from: @(#)vfprintf.c 5.50 (Berkeley) 12/16/92";*/ +static char *rcsid = "$Id$"; +#endif /* LIBC_SCCS and not lint */ + +/* + * Actual printf innards. + * + * This code is large and complicated... + */ +#include + +#ifdef INTEGER_ONLY +# define VFPRINTF vfiprintf +# ifdef STRING_ONLY +# define _VFPRINTF_R _svfiprintf_r +# else +# define _VFPRINTF_R _vfiprintf_r +# endif +#else +# define VFPRINTF vfprintf +# ifdef STRING_ONLY +# define _VFPRINTF_R _svfprintf_r +# else +# define _VFPRINTF_R _vfprintf_r +# endif +# ifndef NO_FLOATING_POINT +# define FLOATING_POINT +# endif +#endif + +#define _NO_POS_ARGS +#ifdef _WANT_IO_POS_ARGS +# undef _NO_POS_ARGS +#endif + +#include <_ansi.h> +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "local.h" +#include "../stdlib/local.h" +#include "fvwrite.h" +#include "vfieeefp.h" + +/* Currently a test is made to see if long double processing is warranted. + This could be changed in the future should the _ldtoa_r code be + preferred over _dtoa_r. */ +#define _NO_LONGDBL +#if defined _WANT_IO_LONG_DOUBLE && (LDBL_MANT_DIG > DBL_MANT_DIG) +#undef _NO_LONGDBL +#endif + +#define _NO_LONGLONG +#if defined _WANT_IO_LONG_LONG \ + && (defined __GNUC__ || __STDC_VERSION__ >= 199901L) +# undef _NO_LONGLONG +#endif + +#ifdef STRING_ONLY +# ifdef _FVWRITE_IN_STREAMIO +# define __SPRINT __ssprint_r +# else +# define __SPRINT __ssputs_r +# endif +#else +# ifdef _FVWRITE_IN_STREAMIO +# define __SPRINT __sprint_r +# else +# define __SPRINT __sfputs_r +# 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 +_DEFUN(__ssputs_r, (ptr, fp, buf, len), + struct _reent *ptr _AND + FILE *fp _AND + _CONST char *buf _AND + 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) + { + ptr->_errno = 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. */ + ptr->_errno = 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 ((_PTR) fp->_p, (_PTR) buf, (size_t) (w)); + fp->_w -= w; + fp->_p += w; + + return 0; + +err: + fp->_flags |= __SERR; + return EOF; +} +#endif + +int +_DEFUN(__ssprint_r, (ptr, fp, uio), + struct _reent *ptr _AND + FILE *fp _AND + 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) + { + ptr->_errno = 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. */ + ptr->_errno = 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 ((_PTR) fp->_p, (_PTR) 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 +_DEFUN(__sfputs_r, (ptr, fp, buf, len), + struct _reent *ptr _AND + FILE *fp _AND + _CONST char *buf _AND + size_t len) +{ + register int i; + +#ifdef _WIDE_ORIENT + 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 +_DEFUN(__sprint_r, (ptr, fp, uio), + struct _reent *ptr _AND + FILE *fp _AND + register struct __suio *uio) +{ + register int err = 0; + + if (uio->uio_resid == 0) { + uio->uio_iovcnt = 0; + return (0); + } +#ifdef _WIDE_ORIENT + 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 +/* + * Helper function for `fprintf to unbuffered unix file': creates a + * temporary buffer. We only work on write-only files; this avoids + * worries about ungetc buffers and so forth. + * + * Make sure to avoid inlining. + */ +_NOINLINE_STATIC int +_DEFUN(__sbprintf, (rptr, fp, fmt, ap), + struct _reent *rptr _AND + register FILE *fp _AND + _CONST char *fmt _AND + va_list ap) +{ + int ret; + FILE fake; + unsigned char buf[BUFSIZ]; + + /* copy the important variables */ + fake._flags = fp->_flags & ~__SNBF; + fake._flags2 = fp->_flags2; + fake._file = fp->_file; + fake._cookie = fp->_cookie; + fake._write = fp->_write; + + /* set up the buffer */ + fake._bf._base = fake._p = buf; + fake._bf._size = fake._w = sizeof (buf); + fake._lbfsize = 0; /* not actually used, but Just In Case */ +#ifndef __SINGLE_THREAD__ + __lock_init_recursive (fake._lock); +#endif + + /* do the work, then copy any error status */ + ret = _VFPRINTF_R (rptr, &fake, fmt, ap); + if (ret >= 0 && _fflush_r (rptr, &fake)) + ret = EOF; + if (fake._flags & __SERR) + fp->_flags |= __SERR; + +#ifndef __SINGLE_THREAD__ + __lock_close_recursive (fake._lock); +#endif + return (ret); +} +#endif /* _UNBUF_STREAM_OPT */ +#endif /* !STRING_ONLY */ + + +#if defined (FLOATING_POINT) || defined (_WANT_IO_C99_FORMATS) +# include +#endif +#ifdef FLOATING_POINT +# include + +/* For %La, an exponent of 15 bits occupies the exponent character, a + sign, and up to 5 digits. */ +# define MAXEXPLEN 7 +# define DEFPREC 6 + +# ifdef _NO_LONGDBL + +extern char *_dtoa_r _PARAMS((struct _reent *, double, int, + int, int *, int *, char **)); + +# define _PRINTF_FLOAT_TYPE double +# define _DTOA_R _dtoa_r +# define FREXP frexp + +# else /* !_NO_LONGDBL */ + +extern char *_ldtoa_r _PARAMS((struct _reent *, _LONG_DOUBLE, int, + int, int *, int *, char **)); + +extern int _EXFUN(_ldcheck,(_LONG_DOUBLE *)); + +# define _PRINTF_FLOAT_TYPE _LONG_DOUBLE +# define _DTOA_R _ldtoa_r +/* FIXME - frexpl is not yet supported; and cvt infloops if (double)f + converts a finite value into infinity. */ +/* # define FREXP frexpl */ +# define FREXP(f,e) ((_LONG_DOUBLE) frexp ((double)f, e)) +# endif /* !_NO_LONGDBL */ + +static char *cvt(struct _reent *, _PRINTF_FLOAT_TYPE, int, int, char *, int *, + int, int *, char *); + +static int exponent(char *, int, int); + +#endif /* FLOATING_POINT */ + +/* BUF must be big enough for the maximum %#llo (assuming long long is + at most 64 bits, this would be 23 characters), the maximum + multibyte character %C, and the maximum default precision of %La + (assuming long double is at most 128 bits with 113 bits of + mantissa, this would be 29 characters). %e, %f, and %g use + reentrant storage shared with mprec. All other formats that use + buf get by with fewer characters. Making BUF slightly bigger + reduces the need for malloc in %.*a and %S, when large precision or + long strings are processed. + The bigger size of 100 bytes is used on systems which allow number + strings using the locale's grouping character. Since that's a multibyte + value, we should use a conservative value. + */ +#ifdef _WANT_IO_C99_FORMATS +#define BUF 100 +#else +#define BUF 40 +#endif +#if defined _MB_CAPABLE && MB_LEN_MAX > BUF +# undef BUF +# define BUF MB_LEN_MAX +#endif + +#ifndef _NO_LONGLONG +# define quad_t long long +# define u_quad_t unsigned long long +#else +# define quad_t long +# define u_quad_t unsigned long +#endif + +typedef quad_t * quad_ptr_t; +typedef _PTR void_ptr_t; +typedef char * char_ptr_t; +typedef long * long_ptr_t; +typedef int * int_ptr_t; +typedef short * short_ptr_t; + +#ifndef _NO_POS_ARGS +# ifdef NL_ARGMAX +# define MAX_POS_ARGS NL_ARGMAX +# else +# define MAX_POS_ARGS 32 +# endif + +union arg_val +{ + int val_int; + u_int val_u_int; + long val_long; + u_long val_u_long; + float val_float; + double val_double; + _LONG_DOUBLE val__LONG_DOUBLE; + int_ptr_t val_int_ptr_t; + short_ptr_t val_short_ptr_t; + long_ptr_t val_long_ptr_t; + char_ptr_t val_char_ptr_t; + quad_ptr_t val_quad_ptr_t; + void_ptr_t val_void_ptr_t; + quad_t val_quad_t; + u_quad_t val_u_quad_t; + wint_t val_wint_t; +}; + +static union arg_val * +_EXFUN(get_arg, (struct _reent *data, int n, char *fmt, + va_list *ap, int *numargs, union arg_val *args, + int *arg_type, char **last_fmt)); +#endif /* !_NO_POS_ARGS */ + +/* + * Macros for converting digits to letters and vice versa + */ +#define to_digit(c) ((c) - '0') +#define is_digit(c) ((unsigned)to_digit (c) <= 9) +#define to_char(n) ((n) + '0') + +/* + * Flags used during conversion. + */ +#define ALT 0x001 /* alternate form */ +#define HEXPREFIX 0x002 /* add 0x or 0X prefix */ +#define LADJUST 0x004 /* left adjustment */ +#define LONGDBL 0x008 /* long double */ +#define LONGINT 0x010 /* long integer */ +#ifndef _NO_LONGLONG +# define QUADINT 0x020 /* quad integer */ +#else /* ifdef _NO_LONGLONG, make QUADINT equivalent to LONGINT, so + that %lld behaves the same as %ld, not as %d, as expected if: + sizeof (long long) = sizeof long > sizeof int */ +# define QUADINT LONGINT +#endif +#define SHORTINT 0x040 /* short integer */ +#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ +#define FPT 0x100 /* Floating point number */ +#ifdef _WANT_IO_C99_FORMATS +# define CHARINT 0x200 /* char as integer */ +#else /* define as 0, to make SARG and UARG occupy fewer instructions */ +# define CHARINT 0 +#endif +#ifdef _WANT_IO_C99_FORMATS +# define GROUPING 0x400 /* use grouping ("'" flag) */ +#endif + +int _EXFUN(_VFPRINTF_R, (struct _reent *, FILE *, _CONST char *, va_list)); + +#ifndef STRING_ONLY +int +_DEFUN(VFPRINTF, (fp, fmt0, ap), + FILE * fp _AND + _CONST char *fmt0 _AND + va_list ap) +{ + int result; + result = _VFPRINTF_R (_REENT, fp, fmt0, ap); + return result; +} +#endif /* STRING_ONLY */ + +#pragma GCC push_option +#pragma GCC optimize("O0") + +int +_DEFUN(_VFPRINTF_R, (data, fp, fmt0, ap), + struct _reent *data _AND + FILE * fp _AND + _CONST char *fmt0 _AND + va_list ap) +{ + register char *fmt; /* format string */ + register int ch; /* character from fmt */ + register int n, m; /* handy integers (short term usage) */ + register char *cp; /* handy char pointer (short term usage) */ + register int flags; /* flags as above */ + char *fmt_anchor; /* current format spec being processed */ +#ifndef _NO_POS_ARGS + int N; /* arg number */ + int arg_index; /* index into args processed directly */ + int numargs; /* number of varargs read */ + char *saved_fmt; /* saved fmt pointer */ + union arg_val args[MAX_POS_ARGS]; + int arg_type[MAX_POS_ARGS]; + int is_pos_arg; /* is current format positional? */ + int old_is_pos_arg; /* is current format positional? */ +#endif + int ret; /* return value accumulator */ + int width; /* width from format (%8d), or 0 */ + int prec; /* precision from format (%.3d), or -1 */ + char sign; /* sign prefix (' ', '+', '-', or \0) */ +#ifdef _WANT_IO_C99_FORMATS + /* locale specific numeric grouping */ + char *thousands_sep = NULL; + size_t thsnd_len = 0; + const char *grouping = NULL; +#endif +#ifdef FLOATING_POINT + char *decimal_point = _localeconv_r (data)->decimal_point; + size_t decp_len = strlen (decimal_point); + char softsign; /* temporary negative sign for floats */ + union { int i; _PRINTF_FLOAT_TYPE fp; } _double_ = {0}; +# define _fpvalue (_double_.fp) + int expt; /* integer value of exponent */ + int expsize = 0; /* character count for expstr */ + char expstr[MAXEXPLEN]; /* buffer for exponent string */ + int lead; /* sig figs before decimal or group sep */ +#endif /* FLOATING_POINT */ +#if defined (FLOATING_POINT) || defined (_WANT_IO_C99_FORMATS) + int ndig = 0; /* actual number of digits returned by cvt */ +#endif +#if defined (FLOATING_POINT) && defined (_WANT_IO_C99_FORMATS) + int nseps; /* number of group separators with ' */ + int nrepeats; /* number of repeats of the last group */ +#endif + u_quad_t _uquad; /* integer arguments %[diouxX] */ + enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ + int dprec; /* a copy of prec if [diouxX], 0 otherwise */ + int realsz; /* field size expanded by dprec */ + int size; /* size of converted field or string */ + char *xdigs = NULL; /* digits for [xX] conversion */ +#ifdef _FVWRITE_IN_STREAMIO +#define NIOV 8 + struct __suio uio = {0, 0, 0}; /* output information: summary */ + struct __siov iov[NIOV];/* ... and individual io vectors */ + register struct __siov *iovp;/* for PRINT macro */ +#endif + char buf[BUF]; /* space for %c, %S, %[diouxX], %[aA] */ + char ox[2]; /* space for 0x hex-prefix */ +#ifdef _MB_CAPABLE + wchar_t wc; + mbstate_t state; /* mbtowc calls from library must not change state */ +#endif + char *malloc_buf = NULL;/* handy pointer for malloced buffers */ + + /* + * Choose PADSIZE to trade efficiency vs. size. If larger printf + * fields occur frequently, increase PADSIZE and make the initialisers + * below longer. + */ +#define PADSIZE 16 /* pad chunk size */ + static _CONST char blanks[PADSIZE] = + {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; + static _CONST char zeroes[PADSIZE] = + {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; + +#ifdef _MB_CAPABLE + memset (&state, '\0', sizeof (state)); +#endif + /* + * BEWARE, these `goto error' on error, and PAD uses `n'. + */ +#ifdef _FVWRITE_IN_STREAMIO +#define PRINT(ptr, len) { \ + iovp->iov_base = (ptr); \ + iovp->iov_len = (len); \ + uio.uio_resid += (len); \ + iovp++; \ + if (++uio.uio_iovcnt >= NIOV) { \ + if (__SPRINT(data, fp, &uio)) \ + goto error; \ + iovp = iov; \ + } \ +} +#define PAD(howmany, with) { \ + if ((n = (howmany)) > 0) { \ + while (n > PADSIZE) { \ + PRINT (with, PADSIZE); \ + n -= PADSIZE; \ + } \ + PRINT (with, n); \ + } \ +} +#define PRINTANDPAD(p, ep, len, with) { \ + int n = (ep) - (p); \ + if (n > (len)) \ + n = (len); \ + if (n > 0) \ + PRINT((p), n); \ + PAD((len) - (n > 0 ? n : 0), (with)); \ +} +#define FLUSH() { \ + if (uio.uio_resid && __SPRINT(data, fp, &uio)) \ + goto error; \ + uio.uio_iovcnt = 0; \ + iovp = iov; \ +} +#else +#define PRINT(ptr, len) { \ + if (__SPRINT (data, fp, (ptr), (len)) == EOF) \ + goto error; \ +} +#define PAD(howmany, with) { \ + if ((n = (howmany)) > 0) { \ + while (n > PADSIZE) { \ + PRINT (with, PADSIZE); \ + n -= PADSIZE; \ + } \ + PRINT (with, n); \ + } \ +} +#define PRINTANDPAD(p, ep, len, with) { \ + int n = (ep) - (p); \ + if (n > (len)) \ + n = (len); \ + if (n > 0) \ + PRINT((p), n); \ + PAD((len) - (n > 0 ? n : 0), (with)); \ +} +#define FLUSH() +#endif + + /* Macros to support positional arguments */ +#ifndef _NO_POS_ARGS +# define GET_ARG(n, ap, type) \ + (is_pos_arg \ + ? (n < numargs \ + ? args[n].val_##type \ + : get_arg (data, n, fmt_anchor, &ap, &numargs, args, \ + arg_type, &saved_fmt)->val_##type) \ + : (arg_index++ < numargs \ + ? args[n].val_##type \ + : (numargs < MAX_POS_ARGS \ + ? args[numargs++].val_##type = va_arg (ap, type) \ + : va_arg (ap, type)))) +#else +# define GET_ARG(n, ap, type) (va_arg (ap, type)) +#endif + + /* + * To extend shorts properly, we need both signed and unsigned + * argument extraction methods. + */ +#ifndef _NO_LONGLONG +#define SARG() \ + (flags&QUADINT ? GET_ARG (N, ap, quad_t) : \ + flags&LONGINT ? GET_ARG (N, ap, long) : \ + flags&SHORTINT ? (long)(short)GET_ARG (N, ap, int) : \ + flags&CHARINT ? (long)(signed char)GET_ARG (N, ap, int) : \ + (long)GET_ARG (N, ap, int)) +#define UARG() \ + (flags&QUADINT ? GET_ARG (N, ap, u_quad_t) : \ + flags&LONGINT ? GET_ARG (N, ap, u_long) : \ + flags&SHORTINT ? (u_long)(u_short)GET_ARG (N, ap, int) : \ + flags&CHARINT ? (u_long)(unsigned char)GET_ARG (N, ap, int) : \ + (u_long)GET_ARG (N, ap, u_int)) +#else +#define SARG() \ + (flags&LONGINT ? GET_ARG (N, ap, long) : \ + flags&SHORTINT ? (long)(short)GET_ARG (N, ap, int) : \ + flags&CHARINT ? (long)(signed char)GET_ARG (N, ap, int) : \ + (long)GET_ARG (N, ap, int)) +#define UARG() \ + (flags&LONGINT ? GET_ARG (N, ap, u_long) : \ + flags&SHORTINT ? (u_long)(u_short)GET_ARG (N, ap, int) : \ + flags&CHARINT ? (u_long)(unsigned char)GET_ARG (N, ap, int) : \ + (u_long)GET_ARG (N, ap, u_int)) +#endif + +#ifndef STRING_ONLY + /* Initialize std streams if not dealing with sprintf family. */ + CHECK_INIT (data, fp); + _newlib_flockfile_start (fp); + + ORIENT(fp, -1); + + /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ + if (cantwrite (data, fp)) { + _newlib_flockfile_exit (fp); + return (EOF); + } + +#ifdef _UNBUF_STREAM_OPT + /* optimise fprintf(stderr) (and other unbuffered Unix files) */ + if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && + fp->_file >= 0) { + _newlib_flockfile_exit (fp); + return (__sbprintf (data, fp, fmt0, ap)); + } +#endif +#else /* STRING_ONLY */ + /* Create initial buffer if we are called by asprintf family. */ + if (fp->_flags & __SMBF && !fp->_bf._base) + { + fp->_bf._base = fp->_p = _malloc_r (data, 64); + if (!fp->_p) + { + data->_errno = ENOMEM; + return EOF; + } + fp->_bf._size = 64; + } +#endif /* STRING_ONLY */ + + fmt = (char *)fmt0; +#ifdef _FVWRITE_IN_STREAMIO + uio.uio_iov = iovp = iov; + uio.uio_resid = 0; + uio.uio_iovcnt = 0; +#endif + ret = 0; +#ifndef _NO_POS_ARGS + arg_index = 0; + saved_fmt = NULL; + arg_type[0] = -1; + numargs = 0; + is_pos_arg = 0; +#endif + + /* + * Scan the format for conversions (`%' character). + */ + for (;;) { + cp = fmt; +#ifdef _MB_CAPABLE + while ((n = __MBTOWC (data, &wc, fmt, MB_CUR_MAX, + &state)) != 0) { + if (n < 0) { + /* Wave invalid chars through. */ + memset (&state, 0, sizeof state); + n = 1; + } + else if (wc == '%') + break; + fmt += n; + } +#else + while (*fmt != '\0' && *fmt != '%') + fmt += 1; +#endif + if ((m = fmt - cp) != 0) { + PRINT (cp, m); + ret += m; + } +#ifdef _MB_CAPABLE + if (n <= 0) + goto done; +#else + if (*fmt == '\0') + goto done; +#endif + fmt_anchor = fmt; + fmt++; /* skip over '%' */ + + flags = 0; + dprec = 0; + width = 0; + prec = -1; + sign = '\0'; +#ifdef FLOATING_POINT + lead = 0; +#ifdef _WANT_IO_C99_FORMATS + nseps = nrepeats = 0; +#endif +#endif +#ifndef _NO_POS_ARGS + N = arg_index; + is_pos_arg = 0; +#endif + +rflag: ch = *fmt++; +reswitch: switch (ch) { +#ifdef _WANT_IO_C99_FORMATS + case '\'': + thousands_sep = _localeconv_r (data)->thousands_sep; + thsnd_len = strlen (thousands_sep); + grouping = _localeconv_r (data)->grouping; + if (thsnd_len > 0 && grouping && *grouping) + flags |= GROUPING; + goto rflag; +#endif + case ' ': + /* + * ``If the space and + flags both appear, the space + * flag will be ignored.'' + * -- ANSI X3J11 + */ + if (!sign) + sign = ' '; + goto rflag; + case '#': + flags |= ALT; + goto rflag; + case '*': +#ifndef _NO_POS_ARGS + /* we must check for positional arg used for dynamic width */ + n = N; + old_is_pos_arg = is_pos_arg; + is_pos_arg = 0; + if (is_digit (*fmt)) { + char *old_fmt = fmt; + + n = 0; + ch = *fmt++; + do { + n = 10 * n + to_digit (ch); + ch = *fmt++; + } while (is_digit (ch)); + + if (ch == '$') { + if (n <= MAX_POS_ARGS) { + n -= 1; + is_pos_arg = 1; + } + else + goto error; + } + else { + fmt = old_fmt; + goto rflag; + } + } +#endif /* !_NO_POS_ARGS */ + + /* + * ``A negative field width argument is taken as a + * - flag followed by a positive field width.'' + * -- ANSI X3J11 + * They don't exclude field widths read from args. + */ + width = GET_ARG (n, ap, int); +#ifndef _NO_POS_ARGS + is_pos_arg = old_is_pos_arg; +#endif + if (width >= 0) + goto rflag; + width = -width; + /* FALLTHROUGH */ + case '-': + flags |= LADJUST; + goto rflag; + case '+': + sign = '+'; + goto rflag; + case '.': + if ((ch = *fmt++) == '*') { +#ifndef _NO_POS_ARGS + /* we must check for positional arg used for dynamic width */ + n = N; + old_is_pos_arg = is_pos_arg; + is_pos_arg = 0; + if (is_digit (*fmt)) { + char *old_fmt = fmt; + + n = 0; + ch = *fmt++; + do { + n = 10 * n + to_digit (ch); + ch = *fmt++; + } while (is_digit (ch)); + + if (ch == '$') { + if (n <= MAX_POS_ARGS) { + n -= 1; + is_pos_arg = 1; + } + else + goto error; + } + else { + fmt = old_fmt; + goto rflag; + } + } +#endif /* !_NO_POS_ARGS */ + prec = GET_ARG (n, ap, int); +#ifndef _NO_POS_ARGS + is_pos_arg = old_is_pos_arg; +#endif + if (prec < 0) + prec = -1; + goto rflag; + } + n = 0; + while (is_digit (ch)) { + n = 10 * n + to_digit (ch); + ch = *fmt++; + } + prec = n < 0 ? -1 : n; + goto reswitch; + case '0': + /* + * ``Note that 0 is taken as a flag, not as the + * beginning of a field width.'' + * -- ANSI X3J11 + */ + flags |= ZEROPAD; + goto rflag; + case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + n = 0; + do { + n = 10 * n + to_digit (ch); + ch = *fmt++; + } while (is_digit (ch)); +#ifndef _NO_POS_ARGS + if (ch == '$') { + if (n <= MAX_POS_ARGS) { + N = n - 1; + is_pos_arg = 1; + goto rflag; + } + else + goto error; + } +#endif /* !_NO_POS_ARGS */ + width = n; + goto reswitch; +#ifdef FLOATING_POINT + case 'L': + flags |= LONGDBL; + goto rflag; +#endif + case 'h': +#ifdef _WANT_IO_C99_FORMATS + if (*fmt == 'h') { + fmt++; + flags |= CHARINT; + } else +#endif + flags |= SHORTINT; + goto rflag; + case 'l': +#if defined _WANT_IO_C99_FORMATS || !defined _NO_LONGLONG + if (*fmt == 'l') { + fmt++; + flags |= QUADINT; + } else +#endif + flags |= LONGINT; + goto rflag; + case 'q': /* extension */ + flags |= QUADINT; + goto rflag; +#ifdef _WANT_IO_C99_FORMATS + case 'j': + if (sizeof (intmax_t) == sizeof (long)) + flags |= LONGINT; + else + flags |= QUADINT; + goto rflag; + case 'z': + if (sizeof (size_t) < sizeof (int)) + /* POSIX states size_t is 16 or more bits, as is short. */ + flags |= SHORTINT; + else if (sizeof (size_t) == sizeof (int)) + /* no flag needed */; + else if (sizeof (size_t) <= sizeof (long)) + flags |= LONGINT; + else + /* POSIX states that at least one programming + environment must support size_t no wider than + long, but that means other environments can + have size_t as wide as long long. */ + flags |= QUADINT; + goto rflag; + case 't': + if (sizeof (ptrdiff_t) < sizeof (int)) + /* POSIX states ptrdiff_t is 16 or more bits, as + is short. */ + flags |= SHORTINT; + else if (sizeof (ptrdiff_t) == sizeof (int)) + /* no flag needed */; + else if (sizeof (ptrdiff_t) <= sizeof (long)) + flags |= LONGINT; + else + /* POSIX states that at least one programming + environment must support ptrdiff_t no wider than + long, but that means other environments can + have ptrdiff_t as wide as long long. */ + flags |= QUADINT; + goto rflag; + case 'C': +#endif /* _WANT_IO_C99_FORMATS */ + case 'c': + cp = buf; +#ifdef _MB_CAPABLE + if (ch == 'C' || (flags & LONGINT)) { + mbstate_t ps; + + memset ((_PTR)&ps, '\0', sizeof (mbstate_t)); + if ((size = (int)_wcrtomb_r (data, cp, + (wchar_t)GET_ARG (N, ap, wint_t), + &ps)) == -1) { + fp->_flags |= __SERR; + goto error; + } + } + else +#endif /* _MB_CAPABLE */ + { + *cp = GET_ARG (N, ap, int); + size = 1; + } + sign = '\0'; + break; + case 'D': /* extension */ + flags |= LONGINT; + /*FALLTHROUGH*/ + case 'd': + case 'i': + _uquad = SARG (); +#ifndef _NO_LONGLONG + if ((quad_t)_uquad < 0) +#else + if ((long) _uquad < 0) +#endif + { + + _uquad = -_uquad; + sign = '-'; + } + base = DEC; + goto number; +#ifdef FLOATING_POINT +# ifdef _WANT_IO_C99_FORMATS + case 'a': + case 'A': + case 'F': +# endif + case 'e': + case 'E': + case 'f': + case 'g': + case 'G': +# ifdef _NO_LONGDBL + if (flags & LONGDBL) { + _fpvalue = (double) GET_ARG (N, ap, _LONG_DOUBLE); + } else { + _fpvalue = GET_ARG (N, ap, double); + } + + /* do this before tricky precision changes + + If the output is infinite or NaN, leading + zeros are not permitted. Otherwise, scanf + could not read what printf wrote. + */ + if (isinf (_fpvalue)) { + if (_fpvalue < 0) + sign = '-'; + if (ch <= 'G') /* 'A', 'E', 'F', or 'G' */ + cp = "INF"; + else + cp = "inf"; + size = 3; + flags &= ~ZEROPAD; + break; + } + if (isnan (_fpvalue)) { + if (ch <= 'G') /* 'A', 'E', 'F', or 'G' */ + cp = "NAN"; + else + cp = "nan"; + size = 3; + flags &= ~ZEROPAD; + break; + } + +# else /* !_NO_LONGDBL */ + + if (flags & LONGDBL) { + _fpvalue = GET_ARG (N, ap, _LONG_DOUBLE); + } else { + _fpvalue = (_LONG_DOUBLE)GET_ARG (N, ap, double); + } + + /* do this before tricky precision changes */ + expt = _ldcheck (&_fpvalue); + if (expt == 2) { + if (_fpvalue < 0) + sign = '-'; + if (ch <= 'G') /* 'A', 'E', 'F', or 'G' */ + cp = "INF"; + else + cp = "inf"; + size = 3; + flags &= ~ZEROPAD; + break; + } + if (expt == 1) { + if (ch <= 'G') /* 'A', 'E', 'F', or 'G' */ + cp = "NAN"; + else + cp = "nan"; + size = 3; + flags &= ~ZEROPAD; + break; + } +# endif /* !_NO_LONGDBL */ + +# ifdef _WANT_IO_C99_FORMATS + if (ch == 'a' || ch == 'A') { + ox[0] = '0'; + ox[1] = ch == 'a' ? 'x' : 'X'; + flags |= HEXPREFIX; + if (prec >= BUF) + { + if ((malloc_buf = + (char *)_malloc_r (data, prec + 1)) + == NULL) + { + fp->_flags |= __SERR; + goto error; + } + cp = malloc_buf; + } + else + cp = buf; + } else +# endif /* _WANT_IO_C99_FORMATS */ + if (prec == -1) { + prec = DEFPREC; + } else if ((ch == 'g' || ch == 'G') && prec == 0) { + prec = 1; + } + + flags |= FPT; + + cp = cvt (data, _fpvalue, prec, flags, &softsign, + &expt, ch, &ndig, cp); + + if (ch == 'g' || ch == 'G') { + if (expt <= -4 || expt > prec) + ch -= 2; /* 'e' or 'E' */ + else + ch = 'g'; + } +# ifdef _WANT_IO_C99_FORMATS + else if (ch == 'F') + ch = 'f'; +# endif + if (ch <= 'e') { /* 'a', 'A', 'e', or 'E' fmt */ + --expt; + expsize = exponent (expstr, expt, ch); + size = expsize + ndig; + if (ndig > 1 || flags & ALT) + size += decp_len; +# ifdef _WANT_IO_C99_FORMATS + flags &= ~GROUPING; +# endif + } else { + if (ch == 'f') { /* f fmt */ + if (expt > 0) { + size = expt; + if (prec || flags & ALT) + size += prec + decp_len; + } else /* "0.X" */ + size = (prec || flags & ALT) + ? prec + 1 + decp_len + : 1; + } else if (expt >= ndig) { /* fixed g fmt */ + size = expt; + if (flags & ALT) + size += decp_len; + } else { + size = ndig + decp_len; + if (expt <= 0) + size += 1 - expt; + } +# ifdef _WANT_IO_C99_FORMATS + if ((flags & GROUPING) && expt > 0) { + /* space for thousands' grouping */ + nseps = nrepeats = 0; + lead = expt; + while (*grouping != CHAR_MAX) { + if (lead <= *grouping) + break; + lead -= *grouping; + if (grouping[1]) { + nseps++; + grouping++; + } else + nrepeats++; + } + size += (nseps + nrepeats) * thsnd_len; + } else +# endif + lead = expt; + } + + if (softsign) + sign = '-'; + break; +#endif /* FLOATING_POINT */ +#ifdef _GLIBC_EXTENSION + case 'm': /* extension */ + { + int dummy; + cp = _strerror_r (data, data->_errno, 1, &dummy); + } + flags &= ~LONGINT; + goto string; +#endif + case 'n': +#ifndef _NO_LONGLONG + if (flags & QUADINT) + *GET_ARG (N, ap, quad_ptr_t) = ret; + else +#endif + if (flags & LONGINT) + *GET_ARG (N, ap, long_ptr_t) = ret; + else if (flags & SHORTINT) + *GET_ARG (N, ap, short_ptr_t) = ret; +#ifdef _WANT_IO_C99_FORMATS + else if (flags & CHARINT) + *GET_ARG (N, ap, char_ptr_t) = ret; +#endif + else + *GET_ARG (N, ap, int_ptr_t) = ret; + continue; /* no output */ + case 'O': /* extension */ + flags |= LONGINT; + /*FALLTHROUGH*/ + case 'o': + _uquad = UARG (); + base = OCT; +#ifdef _WANT_IO_C99_FORMATS + flags &= ~GROUPING; +#endif + goto nosign; + case 'p': + /* + * ``The argument shall be a pointer to void. The + * value of the pointer is converted to a sequence + * of printable characters, in an implementation- + * defined manner.'' + * -- ANSI X3J11 + */ + /* NOSTRICT */ + _uquad = (uintptr_t) GET_ARG (N, ap, void_ptr_t); + base = HEX; + xdigs = "0123456789abcdef"; + flags |= HEXPREFIX; + ox[0] = '0'; + ox[1] = ch = 'x'; + goto nosign; + case 's': +#ifdef _WANT_IO_C99_FORMATS + case 'S': +#endif + cp = GET_ARG (N, ap, char_ptr_t); +#ifdef _GLIBC_EXTENSION +string: +#endif + sign = '\0'; +#ifndef __OPTIMIZE_SIZE__ + /* Behavior is undefined if the user passed a + NULL string when precision is not 0. + However, if we are not optimizing for size, + we might as well mirror glibc behavior. */ + if (cp == NULL) { + cp = "(null)"; + size = ((unsigned) prec > 6U) ? 6 : prec; + } + else +#endif /* __OPTIMIZE_SIZE__ */ +#ifdef _MB_CAPABLE + if (ch == 'S' || (flags & LONGINT)) { + mbstate_t ps; + _CONST wchar_t *wcp; + + wcp = (_CONST wchar_t *)cp; + size = m = 0; + memset ((_PTR)&ps, '\0', sizeof (mbstate_t)); + + /* Count number of bytes needed for multibyte + string that will be produced from widechar + string. */ + if (prec >= 0) { + while (1) { + if (wcp[m] == L'\0') + break; + if ((n = (int)_wcrtomb_r (data, + buf, wcp[m], &ps)) == -1) { + fp->_flags |= __SERR; + goto error; + } + if (n + size > prec) + break; + m += 1; + size += n; + if (size == prec) + break; + } + } + else { + if ((size = (int)_wcsrtombs_r (data, + NULL, &wcp, 0, &ps)) == -1) { + fp->_flags |= __SERR; + goto error; + } + wcp = (_CONST wchar_t *)cp; + } + + if (size == 0) + break; + + if (size >= BUF) { + if ((malloc_buf = + (char *)_malloc_r (data, size + 1)) + == NULL) { + fp->_flags |= __SERR; + goto error; + } + cp = malloc_buf; + } else + cp = buf; + + /* Convert widechar string to multibyte string. */ + memset ((_PTR)&ps, '\0', sizeof (mbstate_t)); + if (_wcsrtombs_r (data, cp, &wcp, size, &ps) + != size) { + fp->_flags |= __SERR; + goto error; + } + cp[size] = '\0'; + } + else +#endif /* _MB_CAPABLE */ + if (prec >= 0) { + /* + * can't use strlen; can only look for the + * NUL in the first `prec' characters, and + * strlen () will go further. + */ + char *p = memchr (cp, 0, prec); + + if (p != NULL) + size = p - cp; + else + size = prec; + } else + size = strlen (cp); + + break; + case 'U': /* extension */ + flags |= LONGINT; + /*FALLTHROUGH*/ + case 'u': + _uquad = UARG (); + base = DEC; + goto nosign; + case 'X': + xdigs = "0123456789ABCDEF"; + goto hex; + case 'x': + xdigs = "0123456789abcdef"; +hex: _uquad = UARG (); + base = HEX; + /* leading 0x/X only if non-zero */ + if (flags & ALT && _uquad != 0) { + ox[0] = '0'; + ox[1] = ch; + flags |= HEXPREFIX; + } + +#ifdef _WANT_IO_C99_FORMATS + flags &= ~GROUPING; +#endif + /* unsigned conversions */ +nosign: sign = '\0'; + /* + * ``... diouXx conversions ... if a precision is + * specified, the 0 flag will be ignored.'' + * -- ANSI X3J11 + */ +number: if ((dprec = prec) >= 0) + flags &= ~ZEROPAD; + + /* + * ``The result of converting a zero value with an + * explicit precision of zero is no characters.'' + * -- ANSI X3J11 + */ + cp = buf + BUF; + if (_uquad != 0 || prec != 0) { + /* + * Unsigned mod is hard, and unsigned mod + * by a constant is easier than that by + * a variable; hence this switch. + */ + switch (base) { + case OCT: + do { + *--cp = to_char (_uquad & 7); + _uquad >>= 3; + } while (_uquad); + /* handle octal leading 0 */ + if (flags & ALT && *cp != '0') + *--cp = '0'; + break; + + case DEC: + /* many numbers are 1 digit */ + if (_uquad < 10) { + *--cp = to_char(_uquad); + break; + } +#ifdef _WANT_IO_C99_FORMATS + ndig = 0; +#endif + do { + *--cp = to_char (_uquad % 10); +#ifdef _WANT_IO_C99_FORMATS + ndig++; + /* If (*grouping == CHAR_MAX) then no + more grouping */ + if ((flags & GROUPING) + && ndig == *grouping + && *grouping != CHAR_MAX + && _uquad > 9) { + cp -= thsnd_len; + strncpy (cp, thousands_sep, + thsnd_len); + ndig = 0; + /* If (grouping[1] == '\0') then we + have to use *grouping character + (last grouping rule) for all + next cases. */ + if (grouping[1] != '\0') + grouping++; + } +#endif + _uquad /= 10; + } while (_uquad != 0); + break; + + case HEX: + do { + *--cp = xdigs[_uquad & 15]; + _uquad >>= 4; + } while (_uquad); + break; + + default: + cp = "bug in vfprintf: bad base"; + size = strlen (cp); + goto skipsize; + } + } + /* + * ...result is to be converted to an 'alternate form'. + * For o conversion, it increases the precision to force + * the first digit of the result to be a zero." + * -- ANSI X3J11 + * + * To demonstrate this case, compile and run: + * printf ("%#.0o",0); + */ + else if (base == OCT && (flags & ALT)) + *--cp = '0'; + + size = buf + BUF - cp; + skipsize: + break; + default: /* "%?" prints ?, unless ? is NUL */ + if (ch == '\0') + goto done; + /* pretend it was %c with argument ch */ + cp = buf; + *cp = ch; + size = 1; + sign = '\0'; + break; + } + + /* + * All reasonable formats wind up here. At this point, `cp' + * points to a string which (if not flags&LADJUST) should be + * padded out to `width' places. If flags&ZEROPAD, it should + * first be prefixed by any sign or other prefix; otherwise, + * it should be blank padded before the prefix is emitted. + * After any left-hand padding and prefixing, emit zeroes + * required by a decimal [diouxX] precision, then print the + * string proper, then emit zeroes required by any leftover + * floating precision; finally, if LADJUST, pad with blanks. + * If flags&FPT, ch must be in [aAeEfg]. + * + * Compute actual size, so we know how much to pad. + * size excludes decimal prec; realsz includes it. + */ + realsz = dprec > size ? dprec : size; + if (sign) + realsz++; + if (flags & HEXPREFIX) + realsz+= 2; + + /* right-adjusting blank padding */ + if ((flags & (LADJUST|ZEROPAD)) == 0) + PAD (width - realsz, blanks); + + /* prefix */ + if (sign) + PRINT (&sign, 1); + if (flags & HEXPREFIX) + PRINT (ox, 2); + + /* right-adjusting zero padding */ + if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) + PAD (width - realsz, zeroes); + + /* leading zeroes from decimal precision */ + PAD (dprec - size, zeroes); + + /* the string or number proper */ +#ifdef FLOATING_POINT + if ((flags & FPT) == 0) { + PRINT (cp, size); + } else { /* glue together f_p fragments */ + if (ch >= 'f') { /* 'f' or 'g' */ + if (_fpvalue == 0) { + /* kludge for __dtoa irregularity */ + PRINT ("0", 1); + if (expt < ndig || flags & ALT) { + PRINT (decimal_point, decp_len); + PAD (ndig - 1, zeroes); + } + } else if (expt <= 0) { + PRINT ("0", 1); + if (expt || ndig || flags & ALT) { + PRINT (decimal_point, decp_len); + PAD (-expt, zeroes); + PRINT (cp, ndig); + } + } else { + char *convbuf = cp; + PRINTANDPAD(cp, convbuf + ndig, + lead, zeroes); + cp += lead; +#ifdef _WANT_IO_C99_FORMATS + if (flags & GROUPING) { + while (nseps > 0 || nrepeats > 0) { + if (nrepeats > 0) + nrepeats--; + else { + grouping--; + nseps--; + } + PRINT(thousands_sep, thsnd_len); + PRINTANDPAD (cp, convbuf + ndig, + *grouping, zeroes); + cp += *grouping; + } + if (cp > convbuf + ndig) + cp = convbuf + ndig; + } +#endif + if (expt < ndig || flags & ALT) + PRINT (decimal_point, decp_len); + PRINTANDPAD (cp, convbuf + ndig, + ndig - expt, zeroes); + } + } else { /* 'a', 'A', 'e', or 'E' */ + if (ndig > 1 || flags & ALT) { + PRINT (cp, 1); + cp++; + PRINT (decimal_point, decp_len); + if (_fpvalue) { + PRINT (cp, ndig - 1); + } else /* 0.[0..] */ + /* __dtoa irregularity */ + PAD (ndig - 1, zeroes); + } else /* XeYYY */ + PRINT (cp, 1); + PRINT (expstr, expsize); + } + } +#else /* !FLOATING_POINT */ + PRINT (cp, size); +#endif + /* left-adjusting padding (always blank) */ + if (flags & LADJUST) + PAD (width - realsz, blanks); + + /* finally, adjust ret */ + ret += width > realsz ? width : realsz; + + FLUSH (); /* copy out the I/O vectors */ + + if (malloc_buf != NULL) { + _free_r (data, malloc_buf); + malloc_buf = NULL; + } + } +done: + FLUSH (); +error: + if (malloc_buf != NULL) + _free_r (data, malloc_buf); +#ifndef STRING_ONLY + _newlib_flockfile_end (fp); +#endif + return (__sferror (fp) ? EOF : ret); + /* NOTREACHED */ +} + +#pragma GCC pop_option + +#ifdef FLOATING_POINT + +/* Using reentrant DATA, convert finite VALUE into a string of digits + with no decimal point, using NDIGITS precision and FLAGS as guides + to whether trailing zeros must be included. Set *SIGN to nonzero + if VALUE was negative. Set *DECPT to the exponent plus one. Set + *LENGTH to the length of the returned string. CH must be one of + [aAeEfFgG]; if it is [aA], then the return string lives in BUF, + otherwise the return value shares the mprec reentrant storage. */ +static char * +cvt(struct _reent *data, _PRINTF_FLOAT_TYPE value, int ndigits, int flags, + char *sign, int *decpt, int ch, int *length, char *buf) +{ + int mode, dsgn; + char *digits, *bp, *rve; +# ifdef _NO_LONGDBL + union double_union tmp; + + tmp.d = value; + if (word0 (tmp) & Sign_bit) { /* this will check for < 0 and -0.0 */ + value = -value; + *sign = '-'; + } else + *sign = '\000'; +# else /* !_NO_LONGDBL */ + union + { + struct ldieee ieee; + _LONG_DOUBLE val; + } ld; + + ld.val = value; + if (ld.ieee.sign) { /* this will check for < 0 and -0.0 */ + value = -value; + *sign = '-'; + } else + *sign = '\000'; +# endif /* !_NO_LONGDBL */ + +# ifdef _WANT_IO_C99_FORMATS + if (ch == 'a' || ch == 'A') { + /* This code assumes FLT_RADIX is a power of 2. The initial + division ensures the digit before the decimal will be less + than FLT_RADIX (unless it is rounded later). There is no + loss of precision in these calculations. */ + value = FREXP (value, decpt) / 8; + if (!value) + *decpt = 1; + digits = ch == 'a' ? "0123456789abcdef" : "0123456789ABCDEF"; + bp = buf; + do { + value *= 16; + mode = (int) value; + value -= mode; + *bp++ = digits[mode]; + } while (ndigits-- && value); + if (value > 0.5 || (value == 0.5 && mode & 1)) { + /* round to even */ + rve = bp; + while (*--rve == digits[0xf]) { + *rve = '0'; + } + *rve = *rve == '9' ? digits[0xa] : *rve + 1; + } else { + while (ndigits-- >= 0) { + *bp++ = '0'; + } + } + *length = bp - buf; + return buf; + } +# endif /* _WANT_IO_C99_FORMATS */ + if (ch == 'f' || ch == 'F') { + mode = 3; /* ndigits after the decimal point */ + } else { + /* To obtain ndigits after the decimal point for the 'e' + * and 'E' formats, round to ndigits + 1 significant + * figures. + */ + if (ch == 'e' || ch == 'E') { + ndigits++; + } + mode = 2; /* ndigits significant digits */ + } + + digits = _DTOA_R (data, value, mode, ndigits, decpt, &dsgn, &rve); + + if ((ch != 'g' && ch != 'G') || flags & ALT) { /* Print trailing zeros */ + bp = digits + ndigits; + if (ch == 'f' || ch == 'F') { + if (*digits == '0' && value) + *decpt = -ndigits + 1; + bp += *decpt; + } + if (value == 0) /* kludge for __dtoa irregularity */ + rve = bp; + while (rve < bp) + *rve++ = '0'; + } + *length = rve - digits; + return (digits); +} + +static int +exponent(char *p0, int exp, int fmtch) +{ + register char *p, *t; + char expbuf[MAXEXPLEN]; +# ifdef _WANT_IO_C99_FORMATS + int isa = fmtch == 'a' || fmtch == 'A'; +# else +# define isa 0 +# endif + + p = p0; + *p++ = isa ? 'p' - 'a' + fmtch : fmtch; + if (exp < 0) { + exp = -exp; + *p++ = '-'; + } + else + *p++ = '+'; + t = expbuf + MAXEXPLEN; + if (exp > 9) { + do { + *--t = to_char (exp % 10); + } while ((exp /= 10) > 9); + *--t = to_char (exp); + for (; t < expbuf + MAXEXPLEN; *p++ = *t++); + } + else { + if (!isa) + *p++ = '0'; + *p++ = to_char (exp); + } + return (p - p0); +} +#endif /* FLOATING_POINT */ + + +#ifndef _NO_POS_ARGS + +/* Positional argument support. + Written by Jeff Johnston + + Copyright (c) 2002 Red Hat Incorporated. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + The name of Red Hat Incorporated may not be used to endorse + or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL RED HAT INCORPORATED BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +/* The below constant state tables are shared between all versions of + vfprintf and vfwprintf. They must only be defined once, which we do in + the STRING_ONLY/INTEGER_ONLY versions here. */ +#if defined (STRING_ONLY) && defined(INTEGER_ONLY) + +_CONST __CH_CLASS __chclass[256] = { + /* 00-07 */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* 08-0f */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* 10-17 */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* 18-1f */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* 20-27 */ FLAG, OTHER, OTHER, FLAG, DOLLAR, OTHER, OTHER, FLAG, + /* 28-2f */ OTHER, OTHER, STAR, FLAG, OTHER, FLAG, DOT, OTHER, + /* 30-37 */ ZERO, DIGIT, DIGIT, DIGIT, DIGIT, DIGIT, DIGIT, DIGIT, + /* 38-3f */ DIGIT, DIGIT, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* 40-47 */ OTHER, SPEC, OTHER, SPEC, SPEC, SPEC, SPEC, SPEC, + /* 48-4f */ OTHER, OTHER, OTHER, OTHER, MODFR, OTHER, OTHER, SPEC, + /* 50-57 */ OTHER, OTHER, OTHER, SPEC, OTHER, SPEC, OTHER, OTHER, + /* 58-5f */ SPEC, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* 60-67 */ OTHER, SPEC, OTHER, SPEC, SPEC, SPEC, SPEC, SPEC, + /* 68-6f */ MODFR, SPEC, MODFR, OTHER, MODFR, OTHER, SPEC, SPEC, + /* 70-77 */ SPEC, MODFR, OTHER, SPEC, MODFR, SPEC, OTHER, OTHER, + /* 78-7f */ SPEC, OTHER, MODFR, OTHER, OTHER, OTHER, OTHER, OTHER, + /* 80-87 */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* 88-8f */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* 90-97 */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* 98-9f */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* a0-a7 */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* a8-af */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* b0-b7 */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* b8-bf */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* c0-c7 */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* c8-cf */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* d0-d7 */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* d8-df */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* e0-e7 */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* e8-ef */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* f0-f7 */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, + /* f8-ff */ OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, OTHER, +}; + +_CONST __STATE __state_table[MAX_STATE][MAX_CH_CLASS] = { + /* '0' '1-9' '$' MODFR SPEC '.' '*' FLAG OTHER */ + /* START */ { SFLAG, WDIG, DONE, SMOD, DONE, SDOT, VARW, SFLAG, DONE }, + /* SFLAG */ { SFLAG, WDIG, DONE, SMOD, DONE, SDOT, VARW, SFLAG, DONE }, + /* WDIG */ { DONE, DONE, WIDTH, SMOD, DONE, SDOT, DONE, DONE, DONE }, + /* WIDTH */ { DONE, DONE, DONE, SMOD, DONE, SDOT, DONE, DONE, DONE }, + /* SMOD */ { DONE, DONE, DONE, DONE, DONE, DONE, DONE, DONE, DONE }, + /* SDOT */ { SDOT, PREC, DONE, SMOD, DONE, DONE, VARP, DONE, DONE }, + /* VARW */ { DONE, VWDIG, DONE, SMOD, DONE, SDOT, DONE, DONE, DONE }, + /* VARP */ { DONE, VPDIG, DONE, SMOD, DONE, DONE, DONE, DONE, DONE }, + /* PREC */ { DONE, DONE, DONE, SMOD, DONE, DONE, DONE, DONE, DONE }, + /* VWDIG */ { DONE, DONE, WIDTH, DONE, DONE, DONE, DONE, DONE, DONE }, + /* VPDIG */ { DONE, DONE, PREC, DONE, DONE, DONE, DONE, DONE, DONE }, +}; + +_CONST __ACTION __action_table[MAX_STATE][MAX_CH_CLASS] = { + /* '0' '1-9' '$' MODFR SPEC '.' '*' FLAG OTHER */ + /* START */ { NOOP, NUMBER, NOOP, GETMOD, GETARG, NOOP, NOOP, NOOP, NOOP }, + /* SFLAG */ { NOOP, NUMBER, NOOP, GETMOD, GETARG, NOOP, NOOP, NOOP, NOOP }, + /* WDIG */ { NOOP, NOOP, GETPOS, GETMOD, GETARG, NOOP, NOOP, NOOP, NOOP }, + /* WIDTH */ { NOOP, NOOP, NOOP, GETMOD, GETARG, NOOP, NOOP, NOOP, NOOP }, + /* SMOD */ { NOOP, NOOP, NOOP, NOOP, GETARG, NOOP, NOOP, NOOP, NOOP }, + /* SDOT */ { NOOP, SKIPNUM, NOOP, GETMOD, GETARG, NOOP, NOOP, NOOP, NOOP }, + /* VARW */ { NOOP, NUMBER, NOOP, GETPW, GETPWB, GETPW, NOOP, NOOP, NOOP }, + /* VARP */ { NOOP, NUMBER, NOOP, GETPW, GETPWB, NOOP, NOOP, NOOP, NOOP }, + /* PREC */ { NOOP, NOOP, NOOP, GETMOD, GETARG, NOOP, NOOP, NOOP, NOOP }, + /* VWDIG */ { NOOP, NOOP, PWPOS, NOOP, NOOP, NOOP, NOOP, NOOP, NOOP }, + /* VPDIG */ { NOOP, NOOP, PWPOS, NOOP, NOOP, NOOP, NOOP, NOOP, NOOP }, +}; + +#endif /* STRING_ONLY && INTEGER_ONLY */ + +/* function to get positional parameter N where n = N - 1 */ +static union arg_val * +_DEFUN(get_arg, (data, n, fmt, ap, numargs_p, args, arg_type, last_fmt), + struct _reent *data _AND + int n _AND + char *fmt _AND + va_list *ap _AND + int *numargs_p _AND + union arg_val *args _AND + int *arg_type _AND + char **last_fmt) +{ + int ch; + int number, flags; + int spec_type; + int numargs = *numargs_p; + __CH_CLASS chtype; + __STATE state, next_state; + __ACTION action; + int pos, last_arg; + int max_pos_arg = n; + /* Only need types that can be reached via vararg promotions. */ + enum types { INT, LONG_INT, QUAD_INT, CHAR_PTR, DOUBLE, LONG_DOUBLE, WIDE_CHAR }; +# ifdef _MB_CAPABLE + wchar_t wc; + mbstate_t wc_state; + int nbytes; +# endif + + /* if this isn't the first call, pick up where we left off last time */ + if (*last_fmt != NULL) + fmt = *last_fmt; + +# ifdef _MB_CAPABLE + memset (&wc_state, '\0', sizeof (wc_state)); +# endif + + /* we need to process either to end of fmt string or until we have actually + read the desired parameter from the vararg list. */ + while (*fmt && n >= numargs) + { +# ifdef _MB_CAPABLE + while ((nbytes = __MBTOWC (data, &wc, fmt, MB_CUR_MAX, &wc_state)) > 0) + { + fmt += nbytes; + if (wc == '%') + break; + } + + if (nbytes <= 0) + break; +# else + while (*fmt != '\0' && *fmt != '%') + fmt += 1; + + if (*fmt == '\0') + break; +# endif /* ! _MB_CAPABLE */ + state = START; + flags = 0; + pos = -1; + number = 0; + spec_type = INT; + + /* Use state/action table to process format specifiers. We ignore invalid + formats and we are only interested in information that tells us how to + read the vararg list. */ + while (state != DONE) + { + ch = *fmt++; + chtype = __chclass[ch]; + next_state = __state_table[state][chtype]; + action = __action_table[state][chtype]; + state = next_state; + + switch (action) + { + case GETMOD: /* we have format modifier */ + switch (ch) + { + case 'h': + /* No flag needed, since short and char promote to int. */ + break; + case 'L': + flags |= LONGDBL; + break; + case 'q': + flags |= QUADINT; + break; +# ifdef _WANT_IO_C99_FORMATS + case 'j': + if (sizeof (intmax_t) == sizeof (long)) + flags |= LONGINT; + else + flags |= QUADINT; + break; + case 'z': + if (sizeof (size_t) <= sizeof (int)) + /* no flag needed */; + else if (sizeof (size_t) <= sizeof (long)) + flags |= LONGINT; + else + /* POSIX states that at least one programming + environment must support size_t no wider than + long, but that means other environments can + have size_t as wide as long long. */ + flags |= QUADINT; + break; + case 't': + if (sizeof (ptrdiff_t) <= sizeof (int)) + /* no flag needed */; + else if (sizeof (ptrdiff_t) <= sizeof (long)) + flags |= LONGINT; + else + /* POSIX states that at least one programming + environment must support ptrdiff_t no wider than + long, but that means other environments can + have ptrdiff_t as wide as long long. */ + flags |= QUADINT; + break; +# endif /* _WANT_IO_C99_FORMATS */ + case 'l': + default: +# if defined _WANT_IO_C99_FORMATS || !defined _NO_LONGLONG + if (*fmt == 'l') + { + flags |= QUADINT; + ++fmt; + } + else +# endif + flags |= LONGINT; + break; + } + break; + case GETARG: /* we have format specifier */ + { + numargs &= (MAX_POS_ARGS - 1); + /* process the specifier and translate it to a type to fetch from varargs */ + switch (ch) + { + case 'd': + case 'i': + case 'o': + case 'x': + case 'X': + case 'u': + if (flags & LONGINT) + spec_type = LONG_INT; +# ifndef _NO_LONGLONG + else if (flags & QUADINT) + spec_type = QUAD_INT; +# endif + else + spec_type = INT; + break; + case 'D': + case 'U': + case 'O': + spec_type = LONG_INT; + break; +# ifdef _WANT_IO_C99_FORMATS + case 'a': + case 'A': + case 'F': +# endif + case 'f': + case 'g': + case 'G': + case 'E': + case 'e': +# ifndef _NO_LONGDBL + if (flags & LONGDBL) + spec_type = LONG_DOUBLE; + else +# endif + spec_type = DOUBLE; + break; + case 's': +# ifdef _WANT_IO_C99_FORMATS + case 'S': +# endif + case 'p': + case 'n': + spec_type = CHAR_PTR; + break; + case 'c': +# ifdef _WANT_IO_C99_FORMATS + if (flags & LONGINT) + spec_type = WIDE_CHAR; + else +# endif + spec_type = INT; + break; +# ifdef _WANT_IO_C99_FORMATS + case 'C': + spec_type = WIDE_CHAR; + break; +# endif + } + + /* if we have a positional parameter, just store the type, otherwise + fetch the parameter from the vararg list */ + if (pos != -1) + arg_type[pos] = spec_type; + else + { + switch (spec_type) + { + case LONG_INT: + args[numargs++].val_long = va_arg (*ap, long); + break; + case QUAD_INT: + args[numargs++].val_quad_t = va_arg (*ap, quad_t); + break; + case WIDE_CHAR: + args[numargs++].val_wint_t = va_arg (*ap, wint_t); + break; + case INT: + args[numargs++].val_int = va_arg (*ap, int); + break; + case CHAR_PTR: + args[numargs++].val_char_ptr_t = va_arg (*ap, char *); + break; + case DOUBLE: + args[numargs++].val_double = va_arg (*ap, double); + break; + case LONG_DOUBLE: + args[numargs++].val__LONG_DOUBLE = va_arg (*ap, _LONG_DOUBLE); + break; + } + } + } + break; + case GETPOS: /* we have positional specifier */ + if (arg_type[0] == -1) + memset (arg_type, 0, sizeof (int) * MAX_POS_ARGS); + pos = number - 1; + max_pos_arg = (max_pos_arg > pos ? max_pos_arg : pos); + break; + case PWPOS: /* we have positional specifier for width or precision */ + if (arg_type[0] == -1) + memset (arg_type, 0, sizeof (int) * MAX_POS_ARGS); + number -= 1; + arg_type[number] = INT; + max_pos_arg = (max_pos_arg > number ? max_pos_arg : number); + break; + case GETPWB: /* we require format pushback */ + --fmt; + /* fallthrough */ + case GETPW: /* we have a variable precision or width to acquire */ + args[numargs++].val_int = va_arg (*ap, int); + break; + case NUMBER: /* we have a number to process */ + number = (ch - '0'); + while ((ch = *fmt) != '\0' && is_digit (ch)) + { + number = number * 10 + (ch - '0'); + ++fmt; + } + break; + case SKIPNUM: /* we have a number to skip */ + while ((ch = *fmt) != '\0' && is_digit (ch)) + ++fmt; + break; + case NOOP: + default: + break; /* do nothing */ + } + } + } + + /* process all arguments up to at least the one we are looking for and if we + have seen the end of the string, then process up to the max argument needed */ + if (*fmt == '\0') + last_arg = max_pos_arg; + else + last_arg = n; + + while (numargs <= last_arg) + { + switch (arg_type[numargs]) + { + case LONG_INT: + args[numargs++].val_long = va_arg (*ap, long); + break; + case QUAD_INT: + args[numargs++].val_quad_t = va_arg (*ap, quad_t); + break; + case CHAR_PTR: + args[numargs++].val_char_ptr_t = va_arg (*ap, char *); + break; + case DOUBLE: + args[numargs++].val_double = va_arg (*ap, double); + break; + case LONG_DOUBLE: + args[numargs++].val__LONG_DOUBLE = va_arg (*ap, _LONG_DOUBLE); + break; + case WIDE_CHAR: + args[numargs++].val_wint_t = va_arg (*ap, wint_t); + break; + case INT: + default: + args[numargs++].val_int = va_arg (*ap, int); + break; + } + } + + /* alter the global numargs value and keep a reference to the last bit of the fmt + string we processed here because the caller will continue processing where we started */ + *numargs_p = numargs; + *last_fmt = fmt; + return &args[n]; +} +#endif /* !_NO_POS_ARGS */ diff --git a/newlib/libc/sys/jehanne/vfwprintf.c b/newlib/libc/sys/jehanne/vfwprintf.c new file mode 100644 index 000000000..7e138fd34 --- /dev/null +++ b/newlib/libc/sys/jehanne/vfwprintf.c @@ -0,0 +1,2024 @@ +/* + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Chris Torek. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* +FUNCTION +<>, <>, <>---wide character format argument list + +INDEX + vfwprintf +INDEX + _vfwprintf_r +INDEX + vwprintf +INDEX + _vwprintf_r +INDEX + vswprintf +INDEX + _vswprintf_r + +ANSI_SYNOPSIS + #include + #include + #include + int vwprintf(const wchar_t *__restrict <[fmt]>, va_list <[list]>); + int vfwprintf(FILE *__restrict <[fp]>, + const wchar_t *__restrict <[fmt]>, va_list <[list]>); + int vswprintf(wchar_t * __restrict <[str]>, size_t <[size]>, + const wchar_t *__ restrict <[fmt]>, va_list <[list]>); + + int _vwprintf_r(struct _reent *<[reent]>, const wchar_t *<[fmt]>, + va_list <[list]>); + int _vfwprintf_r(struct _reent *<[reent]>, FILE *<[fp]>, + const wchar_t *<[fmt]>, va_list <[list]>); + int _vswprintf_r(struct _reent *<[reent]>, wchar_t *<[str]>, + size_t <[size]>, const wchar_t *<[fmt]>, va_list <[list]>); + +DESCRIPTION +<>, <> and <> are (respectively) variants +of <>, <> and <>. They differ only in allowing +their caller to pass the variable argument list as a <> object +(initialized by <>) rather than directly accepting a variable +number of arguments. The caller is responsible for calling <>. + +<<_vwprintf_r>>, <<_vfwprintf_r>> and <<_vswprintf_r>> are reentrant +versions of the above. + +RETURNS +The return values are consistent with the corresponding functions. + +PORTABILITY +POSIX-1.2008 with extensions; C99 (compliant except for POSIX extensions). + +Supporting OS subroutines required: <>, <>, <>, +<>, <>, <>, <>. + +SEEALSO +<>, <> and <>. +*/ + +/* + * Actual wprintf innards. + * + * This code is large and complicated... + */ +#include + +#ifdef INTEGER_ONLY +# define VFWPRINTF vfiwprintf +# ifdef STRING_ONLY +# define _VFWPRINTF_R _svfiwprintf_r +# else +# define _VFWPRINTF_R _vfiwprintf_r +# endif +#else +# define VFWPRINTF vfwprintf +# ifdef STRING_ONLY +# define _VFWPRINTF_R _svfwprintf_r +# else +# define _VFWPRINTF_R _vfwprintf_r +# endif +# ifndef NO_FLOATING_POINT +# define FLOATING_POINT +# endif +#endif + +#define _NO_POS_ARGS +#ifdef _WANT_IO_POS_ARGS +# undef _NO_POS_ARGS +#endif + +#include <_ansi.h> +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "local.h" +#include "fvwrite.h" +#include "vfieeefp.h" +#ifdef __HAVE_LOCALE_INFO_EXTENDED__ +#include "../locale/setlocale.h" +#endif + +/* Currently a test is made to see if long double processing is warranted. + This could be changed in the future should the _ldtoa_r code be + preferred over _dtoa_r. */ +#define _NO_LONGDBL +#if defined _WANT_IO_LONG_DOUBLE && (LDBL_MANT_DIG > DBL_MANT_DIG) +#undef _NO_LONGDBL +#endif + +#define _NO_LONGLONG +#if defined _WANT_IO_LONG_LONG \ + && (defined __GNUC__ || __STDC_VERSION__ >= 199901L) +# undef _NO_LONGLONG +#endif + +int _EXFUN(_VFWPRINTF_R, (struct _reent *, FILE *, _CONST wchar_t *, va_list)); +/* Defined in vfprintf.c. */ +#ifdef _FVWRITE_IN_STREAMIO +# ifdef STRING_ONLY +# define __SPRINT __ssprint_r +# else +# define __SPRINT __sprint_r +# endif +int _EXFUN(__SPRINT, (struct _reent *, FILE *, register struct __suio *)); +#else +# ifdef STRING_ONLY +# define __SPRINT __ssputs_r +# else +# define __SPRINT __sfputs_r +# endif +int _EXFUN(__SPRINT, (struct _reent *, FILE *, _CONST char *, size_t)); +#endif +#ifndef STRING_ONLY +#ifdef _UNBUF_STREAM_OPT +/* + * Helper function for `fprintf to unbuffered unix file': creates a + * temporary buffer. We only work on write-only files; this avoids + * worries about ungetc buffers and so forth. + */ +static int +_DEFUN(__sbwprintf, (rptr, fp, fmt, ap), + struct _reent *rptr _AND + register FILE *fp _AND + _CONST wchar_t *fmt _AND + va_list ap) +{ + int ret; + FILE fake; + unsigned char buf[BUFSIZ]; + + /* copy the important variables */ + fake._flags = fp->_flags & ~__SNBF; + fake._flags2 = fp->_flags2; + fake._file = fp->_file; + fake._cookie = fp->_cookie; + fake._write = fp->_write; + + /* set up the buffer */ + fake._bf._base = fake._p = buf; + fake._bf._size = fake._w = sizeof (buf); + fake._lbfsize = 0; /* not actually used, but Just In Case */ +#ifndef __SINGLE_THREAD__ + __lock_init_recursive (fake._lock); +#endif + + /* do the work, then copy any error status */ + ret = _VFWPRINTF_R (rptr, &fake, fmt, ap); + if (ret >= 0 && _fflush_r (rptr, &fake)) + ret = EOF; + if (fake._flags & __SERR) + fp->_flags |= __SERR; + +#ifndef __SINGLE_THREAD__ + __lock_close_recursive (fake._lock); +#endif + return (ret); +} +#endif /* _UNBUF_STREAM_OPT */ +#endif /* !STRING_ONLY */ + + +#if defined (FLOATING_POINT) || defined (_WANT_IO_C99_FORMATS) +# include +#endif +#ifdef FLOATING_POINT +# include + +/* For %La, an exponent of 15 bits occupies the exponent character, a + sign, and up to 5 digits. */ +# define MAXEXPLEN 7 +# define DEFPREC 6 + +# ifdef _NO_LONGDBL + +extern char *_dtoa_r _PARAMS((struct _reent *, double, int, + int, int *, int *, char **)); + +# define _PRINTF_FLOAT_TYPE double +# define _DTOA_R _dtoa_r +# define FREXP frexp + +# else /* !_NO_LONGDBL */ + +extern char *_ldtoa_r _PARAMS((struct _reent *, _LONG_DOUBLE, int, + int, int *, int *, char **)); + +extern int _EXFUN(_ldcheck,(_LONG_DOUBLE *)); + +# define _PRINTF_FLOAT_TYPE _LONG_DOUBLE +# define _DTOA_R _ldtoa_r +/* FIXME - frexpl is not yet supported; and cvt infloops if (double)f + converts a finite value into infinity. */ +/* # define FREXP frexpl */ +# define FREXP(f,e) ((_LONG_DOUBLE) frexp ((double)f, e)) +# endif /* !_NO_LONGDBL */ + +static wchar_t *wcvt(struct _reent *, _PRINTF_FLOAT_TYPE, int, int, wchar_t *, + int *, int, int *, wchar_t *, int); + +static int wexponent(wchar_t *, int, int); + +#endif /* FLOATING_POINT */ + +/* BUF must be big enough for the maximum %#llo (assuming long long is + at most 64 bits, this would be 23 characters), the maximum + multibyte character %C, and the maximum default precision of %La + (assuming long double is at most 128 bits with 113 bits of + mantissa, this would be 29 characters). %e, %f, and %g use + reentrant storage shared with mprec. All other formats that use + buf get by with fewer characters. Making BUF slightly bigger + reduces the need for malloc in %.*a and %ls/%S, when large precision or + long strings are processed. + The bigger size of 100 bytes is used on systems which allow number + strings using the locale's grouping character. Since that's a multibyte + value, we should use a conservative value. + */ +#ifdef _WANT_IO_C99_FORMATS +#define BUF 100 +#else +#define BUF 40 +#endif +#if defined _MB_CAPABLE && MB_LEN_MAX > BUF +# undef BUF +# define BUF MB_LEN_MAX +#endif + +#ifndef _NO_LONGLONG +# define quad_t long long +# define u_quad_t unsigned long long +#else +# define quad_t long +# define u_quad_t unsigned long +#endif + +typedef quad_t * quad_ptr_t; +typedef _PTR void_ptr_t; +typedef char * char_ptr_t; +typedef wchar_t* wchar_ptr_t; +typedef long * long_ptr_t; +typedef int * int_ptr_t; +typedef short * short_ptr_t; + +#ifndef _NO_POS_ARGS +# ifdef NL_ARGMAX +# define MAX_POS_ARGS NL_ARGMAX +# else +# define MAX_POS_ARGS 32 +# endif + +union arg_val +{ + int val_int; + u_int val_u_int; + long val_long; + u_long val_u_long; + float val_float; + double val_double; + _LONG_DOUBLE val__LONG_DOUBLE; + int_ptr_t val_int_ptr_t; + short_ptr_t val_short_ptr_t; + long_ptr_t val_long_ptr_t; + char_ptr_t val_char_ptr_t; + wchar_ptr_t val_wchar_ptr_t; + quad_ptr_t val_quad_ptr_t; + void_ptr_t val_void_ptr_t; + quad_t val_quad_t; + u_quad_t val_u_quad_t; + wint_t val_wint_t; +}; + +static union arg_val * +_EXFUN(get_arg, (struct _reent *data, int n, wchar_t *fmt, + va_list *ap, int *numargs, union arg_val *args, + int *arg_type, wchar_t **last_fmt)); +#endif /* !_NO_POS_ARGS */ + +/* + * Macros for converting digits to letters and vice versa + */ +#define to_digit(c) ((c) - L'0') +#define is_digit(c) ((unsigned)to_digit (c) <= 9) +#define to_char(n) ((n) + L'0') + +/* + * Flags used during conversion. + */ +#define ALT 0x001 /* alternate form */ +#define HEXPREFIX 0x002 /* add 0x or 0X prefix */ +#define LADJUST 0x004 /* left adjustment */ +#define LONGDBL 0x008 /* long double */ +#define LONGINT 0x010 /* long integer */ +#ifndef _NO_LONGLONG +# define QUADINT 0x020 /* quad integer */ +#else /* ifdef _NO_LONGLONG, make QUADINT equivalent to LONGINT, so + that %lld behaves the same as %ld, not as %d, as expected if: + sizeof (long long) = sizeof long > sizeof int */ +# define QUADINT LONGINT +#endif +#define SHORTINT 0x040 /* short integer */ +#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ +#define FPT 0x100 /* Floating point number */ +#ifdef _WANT_IO_C99_FORMATS +# define CHARINT 0x200 /* char as integer */ +#else /* define as 0, to make SARG and UARG occupy fewer instructions */ +# define CHARINT 0 +#endif +#ifdef _WANT_IO_C99_FORMATS +# define GROUPING 0x400 /* use grouping ("'" flag) */ +#endif + +#ifndef STRING_ONLY +int +_DEFUN(VFWPRINTF, (fp, fmt0, ap), + FILE *__restrict fp _AND + _CONST wchar_t *__restrict fmt0 _AND + va_list ap) +{ + int result; + result = _VFWPRINTF_R (_REENT, fp, fmt0, ap); + return result; +} +#endif /* STRING_ONLY */ + +#pragma GCC push_option +#pragma GCC optimize("O0") + +int +_DEFUN(_VFWPRINTF_R, (data, fp, fmt0, ap), + struct _reent *data _AND + FILE * fp _AND + _CONST wchar_t *fmt0 _AND + va_list ap) +{ + register wchar_t *fmt; /* format string */ + register wint_t ch; /* character from fmt */ + register int n, m; /* handy integers (short term usage) */ + register wchar_t *cp; /* handy char pointer (short term usage) */ + register int flags; /* flags as above */ + wchar_t *fmt_anchor; /* current format spec being processed */ +#ifndef _NO_POS_ARGS + int N; /* arg number */ + int arg_index; /* index into args processed directly */ + int numargs; /* number of varargs read */ + wchar_t *saved_fmt; /* saved fmt pointer */ + union arg_val args[MAX_POS_ARGS]; + int arg_type[MAX_POS_ARGS]; + int is_pos_arg; /* is current format positional? */ + int old_is_pos_arg; /* is current format positional? */ +#endif + int ret; /* return value accumulator */ + int width; /* width from format (%8d), or 0 */ + int prec; /* precision from format (%.3d), or -1 */ + wchar_t sign; /* sign prefix (' ', '+', '-', or \0) */ +#ifdef _WANT_IO_C99_FORMATS + /* locale specific numeric grouping */ + wchar_t thousands_sep = L'\0'; + const char *grouping = NULL; +#endif +#if defined (_MB_CAPABLE) && !defined (__HAVE_LOCALE_INFO_EXTENDED__) \ + && (defined (FLOATING_POINT) || defined (_WANT_IO_C99_FORMATS)) + mbstate_t state; /* mbtowc calls from library must not change state */ +#endif +#ifdef FLOATING_POINT + wchar_t decimal_point; + wchar_t softsign; /* temporary negative sign for floats */ + union { int i; _PRINTF_FLOAT_TYPE fp; } _double_ = {0}; +# define _fpvalue (_double_.fp) + int expt; /* integer value of exponent */ + int expsize = 0; /* character count for expstr */ + wchar_t expstr[MAXEXPLEN]; /* buffer for exponent string */ + int lead; /* sig figs before decimal or group sep */ +#endif /* FLOATING_POINT */ +#if defined (FLOATING_POINT) || defined (_WANT_IO_C99_FORMATS) + int ndig = 0; /* actual number of digits returned by cvt */ +#endif +#if defined (FLOATING_POINT) && defined (_WANT_IO_C99_FORMATS) + int nseps; /* number of group separators with ' */ + int nrepeats; /* number of repeats of the last group */ +#endif + u_quad_t _uquad; /* integer arguments %[diouxX] */ + enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ + int dprec; /* a copy of prec if [diouxX], 0 otherwise */ + int realsz; /* field size expanded by dprec */ + int size = 0; /* size of converted field or string */ + wchar_t *xdigs = NULL; /* digits for [xX] conversion */ +#ifdef _FVWRITE_IN_STREAMIO +#define NIOV 8 + struct __suio uio = {0, 0, 0}; /* output information: summary */ + struct __siov iov[NIOV];/* ... and individual io vectors */ + register struct __siov *iovp;/* for PRINT macro */ +#endif + wchar_t buf[BUF]; /* space for %c, %ls/%S, %[diouxX], %[aA] */ + wchar_t ox[2]; /* space for 0x hex-prefix */ + wchar_t *malloc_buf = NULL;/* handy pointer for malloced buffers */ + + /* + * Choose PADSIZE to trade efficiency vs. size. If larger printf + * fields occur frequently, increase PADSIZE and make the initialisers + * below longer. + */ +#define PADSIZE 16 /* pad chunk size */ + static _CONST wchar_t blanks[PADSIZE] = + {L' ',L' ',L' ',L' ',L' ',L' ',L' ',L' ', + L' ',L' ',L' ',L' ',L' ',L' ',L' ',L' '}; + static _CONST wchar_t zeroes[PADSIZE] = + {L'0',L'0',L'0',L'0',L'0',L'0',L'0',L'0', + L'0',L'0',L'0',L'0',L'0',L'0',L'0',L'0'}; + +#ifdef FLOATING_POINT +#ifdef _MB_CAPABLE +#ifdef __HAVE_LOCALE_INFO_EXTENDED__ + decimal_point = *__get_current_numeric_locale ()->wdecimal_point; +#else + { + size_t nconv; + + memset (&state, '\0', sizeof (state)); + nconv = _mbrtowc_r (data, &decimal_point, + _localeconv_r (data)->decimal_point, + MB_CUR_MAX, &state); + if (nconv == (size_t) -1 || nconv == (size_t) -2) + decimal_point = L'.'; + } +#endif +#else + decimal_point = (wchar_t) *_localeconv_r (data)->decimal_point; +#endif +#endif + /* + * BEWARE, these `goto error' on error, and PAD uses `n'. + */ +#ifdef _FVWRITE_IN_STREAMIO +#define PRINT(ptr, len) { \ + iovp->iov_base = (char *) (ptr); \ + iovp->iov_len = (len) * sizeof (wchar_t); \ + uio.uio_resid += (len) * sizeof (wchar_t); \ + iovp++; \ + if (++uio.uio_iovcnt >= NIOV) { \ + if (__SPRINT(data, fp, &uio)) \ + goto error; \ + iovp = iov; \ + } \ +} +#define PAD(howmany, with) { \ + if ((n = (howmany)) > 0) { \ + while (n > PADSIZE) { \ + PRINT (with, PADSIZE); \ + n -= PADSIZE; \ + } \ + PRINT (with, n); \ + } \ +} +#define PRINTANDPAD(p, ep, len, with) { \ + int n = (ep) - (p); \ + if (n > (len)) \ + n = (len); \ + if (n > 0) \ + PRINT((p), n); \ + PAD((len) - (n > 0 ? n : 0), (with)); \ +} +#define FLUSH() { \ + if (uio.uio_resid && __SPRINT(data, fp, &uio)) \ + goto error; \ + uio.uio_iovcnt = 0; \ + iovp = iov; \ +} +#else +#define PRINT(ptr, len) { \ + if (__SPRINT (data, fp, (_CONST char *)(ptr), (len) * sizeof (wchar_t)) == EOF) \ + goto error; \ +} +#define PAD(howmany, with) { \ + if ((n = (howmany)) > 0) { \ + while (n > PADSIZE) { \ + PRINT (with, PADSIZE); \ + n -= PADSIZE; \ + } \ + PRINT (with, n); \ + } \ +} +#define PRINTANDPAD(p, ep, len, with) { \ + int n = (ep) - (p); \ + if (n > (len)) \ + n = (len); \ + if (n > 0) \ + PRINT((p), n); \ + PAD((len) - (n > 0 ? n : 0), (with)); \ +} +#define FLUSH() +#endif + + /* Macros to support positional arguments */ +#ifndef _NO_POS_ARGS +# define GET_ARG(n, ap, type) \ + (is_pos_arg \ + ? (n < numargs \ + ? args[n].val_##type \ + : get_arg (data, n, fmt_anchor, &ap, &numargs, args, \ + arg_type, &saved_fmt)->val_##type) \ + : (arg_index++ < numargs \ + ? args[n].val_##type \ + : (numargs < MAX_POS_ARGS \ + ? args[numargs++].val_##type = va_arg (ap, type) \ + : va_arg (ap, type)))) +#else +# define GET_ARG(n, ap, type) (va_arg (ap, type)) +#endif + + /* + * To extend shorts properly, we need both signed and unsigned + * argument extraction methods. + */ +#ifndef _NO_LONGLONG +#define SARG() \ + (flags&QUADINT ? GET_ARG (N, ap, quad_t) : \ + flags&LONGINT ? GET_ARG (N, ap, long) : \ + flags&SHORTINT ? (long)(short)GET_ARG (N, ap, int) : \ + flags&CHARINT ? (long)(signed char)GET_ARG (N, ap, int) : \ + (long)GET_ARG (N, ap, int)) +#define UARG() \ + (flags&QUADINT ? GET_ARG (N, ap, u_quad_t) : \ + flags&LONGINT ? GET_ARG (N, ap, u_long) : \ + flags&SHORTINT ? (u_long)(u_short)GET_ARG (N, ap, int) : \ + flags&CHARINT ? (u_long)(unsigned char)GET_ARG (N, ap, int) : \ + (u_long)GET_ARG (N, ap, u_int)) +#else +#define SARG() \ + (flags&LONGINT ? GET_ARG (N, ap, long) : \ + flags&SHORTINT ? (long)(short)GET_ARG (N, ap, int) : \ + flags&CHARINT ? (long)(signed char)GET_ARG (N, ap, int) : \ + (long)GET_ARG (N, ap, int)) +#define UARG() \ + (flags&LONGINT ? GET_ARG (N, ap, u_long) : \ + flags&SHORTINT ? (u_long)(u_short)GET_ARG (N, ap, int) : \ + flags&CHARINT ? (u_long)(unsigned char)GET_ARG (N, ap, int) : \ + (u_long)GET_ARG (N, ap, u_int)) +#endif + +#ifndef STRING_ONLY + /* Initialize std streams if not dealing with sprintf family. */ + CHECK_INIT (data, fp); + _newlib_flockfile_start (fp); + + ORIENT(fp, 1); + + /* sorry, fwprintf(read_only_file, "") returns EOF, not 0 */ + if (cantwrite (data, fp)) { + _newlib_flockfile_exit (fp); + return (EOF); + } + +#ifdef _UNBUF_STREAM_OPT + /* optimise fwprintf(stderr) (and other unbuffered Unix files) */ + if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && + fp->_file >= 0) { + _newlib_flockfile_exit (fp); + return (__sbwprintf (data, fp, fmt0, ap)); + } +#endif +#else /* STRING_ONLY */ + /* Create initial buffer if we are called by asprintf family. */ + if (fp->_flags & __SMBF && !fp->_bf._base) + { + fp->_bf._base = fp->_p = _malloc_r (data, 64); + if (!fp->_p) + { + data->_errno = ENOMEM; + return EOF; + } + fp->_bf._size = 64; + } +#endif /* STRING_ONLY */ + + fmt = (wchar_t *)fmt0; +#ifdef _FVWRITE_IN_STREAMIO + uio.uio_iov = iovp = iov; + uio.uio_resid = 0; + uio.uio_iovcnt = 0; +#endif + ret = 0; +#ifndef _NO_POS_ARGS + arg_index = 0; + saved_fmt = NULL; + arg_type[0] = -1; + numargs = 0; + is_pos_arg = 0; +#endif + + /* + * Scan the format for conversions (`%' character). + */ + for (;;) { + cp = fmt; + while (*fmt != L'\0' && *fmt != L'%') + ++fmt; + if ((m = fmt - cp) != 0) { + PRINT (cp, m); + ret += m; + } + if (*fmt == L'\0') + goto done; + fmt_anchor = fmt; + fmt++; /* skip over '%' */ + + flags = 0; + dprec = 0; + width = 0; + prec = -1; + sign = L'\0'; +#ifdef FLOATING_POINT + lead = 0; +#ifdef _WANT_IO_C99_FORMATS + nseps = nrepeats = 0; +#endif +#endif +#ifndef _NO_POS_ARGS + N = arg_index; + is_pos_arg = 0; +#endif + +rflag: ch = *fmt++; +reswitch: switch (ch) { +#ifdef _WANT_IO_C99_FORMATS + case L'\'': +#ifdef _MB_CAPABLE +#ifdef __HAVE_LOCALE_INFO_EXTENDED__ + thousands_sep = *__get_current_numeric_locale ()->wthousands_sep; +#else + { + size_t nconv; + + memset (&state, '\0', sizeof (state)); + nconv = _mbrtowc_r (data, &thousands_sep, + _localeconv_r (data)->thousands_sep, + MB_CUR_MAX, &state); + if (nconv == (size_t) -1 || nconv == (size_t) -2) + thousands_sep = L'\0'; + } +#endif +#else + thousands_sep = (wchar_t) *_localeconv_r(data)->thousands_sep; +#endif + grouping = _localeconv_r (data)->grouping; + if (thousands_sep && grouping && *grouping) + flags |= GROUPING; + goto rflag; +#endif + case L' ': + /* + * ``If the space and + flags both appear, the space + * flag will be ignored.'' + * -- ANSI X3J11 + */ + if (!sign) + sign = L' '; + goto rflag; + case L'#': + flags |= ALT; + goto rflag; + case L'*': +#ifndef _NO_POS_ARGS + /* we must check for positional arg used for dynamic width */ + n = N; + old_is_pos_arg = is_pos_arg; + is_pos_arg = 0; + if (is_digit (*fmt)) { + wchar_t *old_fmt = fmt; + + n = 0; + ch = *fmt++; + do { + n = 10 * n + to_digit (ch); + ch = *fmt++; + } while (is_digit (ch)); + + if (ch == L'$') { + if (n <= MAX_POS_ARGS) { + n -= 1; + is_pos_arg = 1; + } + else + goto error; + } + else { + fmt = old_fmt; + goto rflag; + } + } +#endif /* !_NO_POS_ARGS */ + + /* + * ``A negative field width argument is taken as a + * - flag followed by a positive field width.'' + * -- ANSI X3J11 + * They don't exclude field widths read from args. + */ + width = GET_ARG (n, ap, int); +#ifndef _NO_POS_ARGS + is_pos_arg = old_is_pos_arg; +#endif + if (width >= 0) + goto rflag; + width = -width; + /* FALLTHROUGH */ + case L'-': + flags |= LADJUST; + goto rflag; + case L'+': + sign = L'+'; + goto rflag; + case L'.': + if ((ch = *fmt++) == L'*') { +#ifndef _NO_POS_ARGS + /* we must check for positional arg used for dynamic width */ + n = N; + old_is_pos_arg = is_pos_arg; + is_pos_arg = 0; + if (is_digit (*fmt)) { + wchar_t *old_fmt = fmt; + + n = 0; + ch = *fmt++; + do { + n = 10 * n + to_digit (ch); + ch = *fmt++; + } while (is_digit (ch)); + + if (ch == L'$') { + if (n <= MAX_POS_ARGS) { + n -= 1; + is_pos_arg = 1; + } + else + goto error; + } + else { + fmt = old_fmt; + goto rflag; + } + } +#endif /* !_NO_POS_ARGS */ + prec = GET_ARG (n, ap, int); +#ifndef _NO_POS_ARGS + is_pos_arg = old_is_pos_arg; +#endif + if (prec < 0) + prec = -1; + goto rflag; + } + n = 0; + while (is_digit (ch)) { + n = 10 * n + to_digit (ch); + ch = *fmt++; + } + prec = n < 0 ? -1 : n; + goto reswitch; + case L'0': + /* + * ``Note that 0 is taken as a flag, not as the + * beginning of a field width.'' + * -- ANSI X3J11 + */ + flags |= ZEROPAD; + goto rflag; + case L'1': case L'2': case L'3': case L'4': + case L'5': case L'6': case L'7': case L'8': case L'9': + n = 0; + do { + n = 10 * n + to_digit (ch); + ch = *fmt++; + } while (is_digit (ch)); +#ifndef _NO_POS_ARGS + if (ch == L'$') { + if (n <= MAX_POS_ARGS) { + N = n - 1; + is_pos_arg = 1; + goto rflag; + } + else + goto error; + } +#endif /* !_NO_POS_ARGS */ + width = n; + goto reswitch; +#ifdef FLOATING_POINT + case L'L': + flags |= LONGDBL; + goto rflag; +#endif + case L'h': +#ifdef _WANT_IO_C99_FORMATS + if (*fmt == L'h') { + fmt++; + flags |= CHARINT; + } else +#endif + flags |= SHORTINT; + goto rflag; + case L'l': +#if defined _WANT_IO_C99_FORMATS || !defined _NO_LONGLONG + if (*fmt == L'l') { + fmt++; + flags |= QUADINT; + } else +#endif + flags |= LONGINT; + goto rflag; + case L'q': /* GNU extension */ + flags |= QUADINT; + goto rflag; +#ifdef _WANT_IO_C99_FORMATS + case L'j': + if (sizeof (intmax_t) == sizeof (long)) + flags |= LONGINT; + else + flags |= QUADINT; + goto rflag; + case L'z': + if (sizeof (size_t) < sizeof (int)) + /* POSIX states size_t is 16 or more bits, as is short. */ + flags |= SHORTINT; + else if (sizeof (size_t) == sizeof (int)) + /* no flag needed */; + else if (sizeof (size_t) <= sizeof (long)) + flags |= LONGINT; + else + /* POSIX states that at least one programming + environment must support size_t no wider than + long, but that means other environments can + have size_t as wide as long long. */ + flags |= QUADINT; + goto rflag; + case L't': + if (sizeof (ptrdiff_t) < sizeof (int)) + /* POSIX states ptrdiff_t is 16 or more bits, as + is short. */ + flags |= SHORTINT; + else if (sizeof (ptrdiff_t) == sizeof (int)) + /* no flag needed */; + else if (sizeof (ptrdiff_t) <= sizeof (long)) + flags |= LONGINT; + else + /* POSIX states that at least one programming + environment must support ptrdiff_t no wider than + long, but that means other environments can + have ptrdiff_t as wide as long long. */ + flags |= QUADINT; + goto rflag; + case L'C': /* POSIX extension */ +#endif /* _WANT_IO_C99_FORMATS */ + case L'c': + cp = buf; + if (ch == L'c' && !(flags & LONGINT)) { + wint_t wc = btowc ((int) GET_ARG (N, ap, int)); + if (wc == WEOF) { + fp->_flags |= __SERR; + goto error; + } + cp[0] = (wchar_t) wc; + } + else + { + cp[0] = GET_ARG (N, ap, int); + } + cp[1] = L'\0'; + size = 1; + sign = L'\0'; + break; + case L'd': + case L'i': + _uquad = SARG (); +#ifndef _NO_LONGLONG + if ((quad_t)_uquad < 0) +#else + if ((long) _uquad < 0) +#endif + { + + _uquad = -_uquad; + sign = L'-'; + } + base = DEC; + goto number; +#ifdef FLOATING_POINT +# ifdef _WANT_IO_C99_FORMATS + case L'a': + case L'A': + case L'F': +# endif + case L'e': + case L'E': + case L'f': + case L'g': + case L'G': +# ifdef _NO_LONGDBL + if (flags & LONGDBL) { + _fpvalue = (double) GET_ARG (N, ap, _LONG_DOUBLE); + } else { + _fpvalue = GET_ARG (N, ap, double); + } + + /* do this before tricky precision changes + + If the output is infinite or NaN, leading + zeros are not permitted. Otherwise, scanf + could not read what printf wrote. + */ + if (isinf (_fpvalue)) { + if (_fpvalue < 0) + sign = '-'; + if (ch <= L'G') /* 'A', 'E', 'F', or 'G' */ + cp = L"INF"; + else + cp = L"inf"; + size = 3; + flags &= ~ZEROPAD; + break; + } + if (isnan (_fpvalue)) { + if (ch <= L'G') /* 'A', 'E', 'F', or 'G' */ + cp = L"NAN"; + else + cp = L"nan"; + size = 3; + flags &= ~ZEROPAD; + break; + } + +# else /* !_NO_LONGDBL */ + + if (flags & LONGDBL) { + _fpvalue = GET_ARG (N, ap, _LONG_DOUBLE); + } else { + _fpvalue = (_LONG_DOUBLE)GET_ARG (N, ap, double); + } + + /* do this before tricky precision changes */ + expt = _ldcheck (&_fpvalue); + if (expt == 2) { + if (_fpvalue < 0) + sign = L'-'; + if (ch <= L'G') /* 'A', 'E', 'F', or 'G' */ + cp = L"INF"; + else + cp = L"inf"; + size = 3; + flags &= ~ZEROPAD; + break; + } + if (expt == 1) { + if (ch <= L'G') /* 'A', 'E', 'F', or 'G' */ + cp = L"NAN"; + else + cp = L"nan"; + size = 3; + flags &= ~ZEROPAD; + break; + } +# endif /* !_NO_LONGDBL */ + + cp = buf; +# ifdef _WANT_IO_C99_FORMATS + if (ch == L'a' || ch == L'A') { + ox[0] = L'0'; + ox[1] = ch == L'a' ? L'x' : L'X'; + flags |= HEXPREFIX; + if (prec >= BUF) + { + if ((malloc_buf = + (wchar_t *)_malloc_r (data, (prec + 1) * sizeof (wchar_t))) + == NULL) + { + fp->_flags |= __SERR; + goto error; + } + cp = malloc_buf; + } + } else +# endif /* _WANT_IO_C99_FORMATS */ + if (prec == -1) { + prec = DEFPREC; + } else if ((ch == L'g' || ch == L'G') && prec == 0) { + prec = 1; + } + + flags |= FPT; + + cp = wcvt (data, _fpvalue, prec, flags, &softsign, + &expt, ch, &ndig, cp, BUF); + + /* If buf is not large enough for the converted wchar_t + sequence, call wcvt again with a malloced new buffer. + This should happen fairly rarely. + */ + if (cp == buf && ndig > BUF && malloc_buf == NULL) { + if ((malloc_buf = + (wchar_t *)_malloc_r (data, ndig * sizeof (wchar_t))) + == NULL) + { + fp->_flags |= __SERR; + goto error; + } + cp = wcvt (data, _fpvalue, prec, flags, &softsign, + &expt, ch, &ndig, malloc_buf, ndig); + } + + if (ch == L'g' || ch == L'G') { + if (expt <= -4 || expt > prec) + ch -= 2; /* 'e' or 'E' */ + else + ch = L'g'; + } +# ifdef _WANT_IO_C99_FORMATS + else if (ch == L'F') + ch = L'f'; +# endif + if (ch <= L'e') { /* 'a', 'A', 'e', or 'E' fmt */ + --expt; + expsize = wexponent (expstr, expt, ch); + size = expsize + ndig; + if (ndig > 1 || flags & ALT) + ++size; +# ifdef _WANT_IO_C99_FORMATS + flags &= ~GROUPING; +# endif + } else { + if (ch == L'f') { /* f fmt */ + if (expt > 0) { + size = expt; + if (prec || flags & ALT) + size += prec + 1; + } else /* "0.X" */ + size = (prec || flags & ALT) + ? prec + 2 + : 1; + } else if (expt >= ndig) { /* fixed g fmt */ + size = expt; + if (flags & ALT) + ++size; + } else + size = ndig + (expt > 0 ? + 1 : 2 - expt); +# ifdef _WANT_IO_C99_FORMATS + if ((flags & GROUPING) && expt > 0) { + /* space for thousands' grouping */ + nseps = nrepeats = 0; + lead = expt; + while (*grouping != CHAR_MAX) { + if (lead <= *grouping) + break; + lead -= *grouping; + if (grouping[1]) { + nseps++; + grouping++; + } else + nrepeats++; + } + size += nseps + nrepeats; + } else +# endif + lead = expt; + } + if (softsign) + sign = L'-'; + break; +#endif /* FLOATING_POINT */ +#ifdef _GLIBC_EXTENSION + case L'm': /* GNU extension */ + { + int dummy; + cp = (wchar_t *) _strerror_r (data, data->_errno, 1, &dummy); + } + flags &= ~LONGINT; + goto string; +#endif + case L'n': +#ifndef _NO_LONGLONG + if (flags & QUADINT) + *GET_ARG (N, ap, quad_ptr_t) = ret; + else +#endif + if (flags & LONGINT) + *GET_ARG (N, ap, long_ptr_t) = ret; + else if (flags & SHORTINT) + *GET_ARG (N, ap, short_ptr_t) = ret; +#ifdef _WANT_IO_C99_FORMATS + else if (flags & CHARINT) + *GET_ARG (N, ap, char_ptr_t) = ret; +#endif + else + *GET_ARG (N, ap, int_ptr_t) = ret; + continue; /* no output */ + case L'o': + _uquad = UARG (); + base = OCT; +#ifdef _WANT_IO_C99_FORMATS + flags &= ~GROUPING; +#endif + goto nosign; + case L'p': + /* + * ``The argument shall be a pointer to void. The + * value of the pointer is converted to a sequence + * of printable characters, in an implementation- + * defined manner.'' + * -- ANSI X3J11 + */ + /* NOSTRICT */ + _uquad = (uintptr_t) GET_ARG (N, ap, void_ptr_t); + base = HEX; + xdigs = L"0123456789abcdef"; + flags |= HEXPREFIX; + ox[0] = L'0'; + ox[1] = ch = L'x'; + goto nosign; + case L's': +#ifdef _WANT_IO_C99_FORMATS + case L'S': /* POSIX extension */ +#endif + cp = GET_ARG (N, ap, wchar_ptr_t); +#ifdef _GLIBC_EXTENSION +string: +#endif + sign = '\0'; +#ifndef __OPTIMIZE_SIZE__ + /* Behavior is undefined if the user passed a + NULL string when precision is not 0. + However, if we are not optimizing for size, + we might as well mirror glibc behavior. */ + if (cp == NULL) { + cp = L"(null)"; + size = ((unsigned) prec > 6U) ? 6 : prec; + } + else +#endif /* __OPTIMIZE_SIZE__ */ +#ifdef _MB_CAPABLE + if (ch != L'S' && !(flags & LONGINT)) { + char *arg = (char *) cp; + size_t insize = 0, nchars = 0, nconv = 0; + mbstate_t ps; + wchar_t *p; + + if (prec >= 0) { + char *p = arg; + memset ((_PTR)&ps, '\0', sizeof (mbstate_t)); + while (nchars < (size_t)prec) { + nconv = mbrlen (p, MB_CUR_MAX, &ps); + if (nconv == 0 || nconv == (size_t)-1 || + nconv == (size_t)-2) + break; + p += nconv; + ++nchars; + insize += nconv; + } + if (nconv == (size_t) -1 || nconv == (size_t) -2) { + fp->_flags |= __SERR; + goto error; + } + } else + insize = strlen(arg); + if (insize >= BUF) { + if ((malloc_buf = (wchar_t *) _malloc_r (data, (insize + 1) * sizeof (wchar_t))) + == NULL) { + fp->_flags |= __SERR; + goto error; + } + cp = malloc_buf; + } else + cp = buf; + memset ((_PTR)&ps, '\0', sizeof (mbstate_t)); + p = cp; + while (insize != 0) { + nconv = _mbrtowc_r (data, p, arg, insize, &ps); + if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2) + break; + ++p; + arg += nconv; + insize -= nconv; + } + if (nconv == (size_t) -1 || nconv == (size_t) -2) { + fp->_flags |= __SERR; + goto error; + } + *p = L'\0'; + size = p - cp; + } +#else + if (ch != L'S' && !(flags & LONGINT)) { + char *arg = (char *) cp; + size_t insize = 0; + + if (prec >= 0) { + char *p = memchr (arg, '\0', prec); + insize = p ? p - arg : prec; + } else + insize = strlen (arg); + if (insize >= BUF) { + if ((malloc_buf = (wchar_t *) _malloc_r (data, (insize + 1) * sizeof (wchar_t))) + == NULL) { + fp->_flags |= __SERR; + goto error; + } + cp = malloc_buf; + } else + cp = buf; + for (size = 0; size < insize; ++size) + cp[size] = arg[size]; + cp[size] = L'\0'; + } +#endif /* _MB_CAPABLE */ + else if (prec >= 0) { + /* + * can't use wcslen; can only look for the + * NUL in the first `prec' characters, and + * strlen () will go further. + */ + wchar_t *p = wmemchr (cp, L'\0', prec); + + if (p != NULL) { + size = p - cp; + if (size > prec) + size = prec; + } else + size = prec; + } else + size = wcslen (cp); + + break; + case L'u': + _uquad = UARG (); + base = DEC; + goto nosign; + case L'X': + xdigs = L"0123456789ABCDEF"; + goto hex; + case L'x': + xdigs = L"0123456789abcdef"; +hex: _uquad = UARG (); + base = HEX; + /* leading 0x/X only if non-zero */ + if (flags & ALT && _uquad != 0) { + ox[0] = L'0'; + ox[1] = ch; + flags |= HEXPREFIX; + } + +#ifdef _WANT_IO_C99_FORMATS + flags &= ~GROUPING; +#endif + /* unsigned conversions */ +nosign: sign = L'\0'; + /* + * ``... diouXx conversions ... if a precision is + * specified, the 0 flag will be ignored.'' + * -- ANSI X3J11 + */ +number: if ((dprec = prec) >= 0) + flags &= ~ZEROPAD; + + /* + * ``The result of converting a zero value with an + * explicit precision of zero is no characters.'' + * -- ANSI X3J11 + */ + cp = buf + BUF; + if (_uquad != 0 || prec != 0) { + /* + * Unsigned mod is hard, and unsigned mod + * by a constant is easier than that by + * a variable; hence this switch. + */ + switch (base) { + case OCT: + do { + *--cp = to_char (_uquad & 7); + _uquad >>= 3; + } while (_uquad); + /* handle octal leading 0 */ + if (flags & ALT && *cp != L'0') + *--cp = L'0'; + break; + + case DEC: + /* many numbers are 1 digit */ + if (_uquad < 10) { + *--cp = to_char(_uquad); + break; + } +#ifdef _WANT_IO_C99_FORMATS + ndig = 0; +#endif + do { + *--cp = to_char (_uquad % 10); +#ifdef _WANT_IO_C99_FORMATS + ndig++; + /* If (*grouping == CHAR_MAX) then no + more grouping */ + if ((flags & GROUPING) + && ndig == *grouping + && *grouping != CHAR_MAX + && _uquad > 9) { + *--cp = thousands_sep; + ndig = 0; + /* If (grouping[1] == '\0') then we + have to use *grouping character + (last grouping rule) for all + next cases. */ + if (grouping[1] != '\0') + grouping++; + } +#endif + _uquad /= 10; + } while (_uquad != 0); + break; + + case HEX: + do { + *--cp = xdigs[_uquad & 15]; + _uquad >>= 4; + } while (_uquad); + break; + + default: + cp = L"bug in vfprintf: bad base"; + size = wcslen (cp); + goto skipsize; + } + } + /* + * ...result is to be converted to an 'alternate form'. + * For o conversion, it increases the precision to force + * the first digit of the result to be a zero." + * -- ANSI X3J11 + * + * To demonstrate this case, compile and run: + * printf ("%#.0o",0); + */ + else if (base == OCT && (flags & ALT)) + *--cp = L'0'; + + size = buf + BUF - cp; + skipsize: + break; + default: /* "%?" prints ?, unless ? is NUL */ + if (ch == L'\0') + goto done; + /* pretend it was %c with argument ch */ + cp = buf; + *cp = ch; + size = 1; + sign = L'\0'; + break; + } + + /* + * All reasonable formats wind up here. At this point, `cp' + * points to a string which (if not flags&LADJUST) should be + * padded out to `width' places. If flags&ZEROPAD, it should + * first be prefixed by any sign or other prefix; otherwise, + * it should be blank padded before the prefix is emitted. + * After any left-hand padding and prefixing, emit zeroes + * required by a decimal [diouxX] precision, then print the + * string proper, then emit zeroes required by any leftover + * floating precision; finally, if LADJUST, pad with blanks. + * If flags&FPT, ch must be in [aAeEfg]. + * + * Compute actual size, so we know how much to pad. + * size excludes decimal prec; realsz includes it. + */ + realsz = dprec > size ? dprec : size; + if (sign) + realsz++; + if (flags & HEXPREFIX) + realsz+= 2; + + /* right-adjusting blank padding */ + if ((flags & (LADJUST|ZEROPAD)) == 0) + PAD (width - realsz, blanks); + + /* prefix */ + if (sign) + PRINT (&sign, 1); + if (flags & HEXPREFIX) + PRINT (ox, 2); + + /* right-adjusting zero padding */ + if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) + PAD (width - realsz, zeroes); + + /* leading zeroes from decimal precision */ + PAD (dprec - size, zeroes); + + /* the string or number proper */ +#ifdef FLOATING_POINT + if ((flags & FPT) == 0) { + PRINT (cp, size); + } else { /* glue together f_p fragments */ + if (ch >= L'f') { /* 'f' or 'g' */ + if (_fpvalue == 0) { + /* kludge for __dtoa irregularity */ + PRINT (L"0", 1); + if (expt < ndig || flags & ALT) { + PRINT (&decimal_point, 1); + PAD (ndig - 1, zeroes); + } + } else if (expt <= 0) { + PRINT (L"0", 1); + if (expt || ndig || flags & ALT) { + PRINT (&decimal_point, 1); + PAD (-expt, zeroes); + PRINT (cp, ndig); + } + } else { + wchar_t *convbuf = cp; + PRINTANDPAD(cp, convbuf + ndig, + lead, zeroes); + cp += lead; +#ifdef _WANT_IO_C99_FORMATS + if (flags & GROUPING) { + while (nseps > 0 || nrepeats > 0) { + if (nrepeats > 0) + nrepeats--; + else { + grouping--; + nseps--; + } + PRINT (&thousands_sep, 1); + PRINTANDPAD (cp, convbuf + ndig, + *grouping, zeroes); + cp += *grouping; + } + if (cp > convbuf + ndig) + cp = convbuf + ndig; + } +#endif + if (expt < ndig || flags & ALT) + PRINT (&decimal_point, 1); + PRINTANDPAD (cp, convbuf + ndig, + ndig - expt, zeroes); + } + + } else { /* 'a', 'A', 'e', or 'E' */ + if (ndig > 1 || flags & ALT) { + PRINT (cp, 1); + cp++; + PRINT (&decimal_point, 1); + if (_fpvalue) { + PRINT (cp, ndig - 1); + } else /* 0.[0..] */ + /* __dtoa irregularity */ + PAD (ndig - 1, zeroes); + } else /* XeYYY */ + PRINT (cp, 1); + PRINT (expstr, expsize); + } + } +#else /* !FLOATING_POINT */ + PRINT (cp, size); +#endif + /* left-adjusting padding (always blank) */ + if (flags & LADJUST) + PAD (width - realsz, blanks); + + /* finally, adjust ret */ + ret += width > realsz ? width : realsz; + + FLUSH (); /* copy out the I/O vectors */ + + if (malloc_buf != NULL) { + _free_r (data, malloc_buf); + malloc_buf = NULL; + } + } +done: + FLUSH (); +error: + if (malloc_buf != NULL) + _free_r (data, malloc_buf); +#ifndef STRING_ONLY + _newlib_flockfile_end (fp); +#endif + return (__sferror (fp) ? EOF : ret); + /* NOTREACHED */ +} + +#pragma GCC pop_option + +#ifdef FLOATING_POINT + +/* Using reentrant DATA, convert finite VALUE into a string of digits + with no decimal point, using NDIGITS precision and FLAGS as guides + to whether trailing zeros must be included. Set *SIGN to nonzero + if VALUE was negative. Set *DECPT to the exponent plus one. Set + *LENGTH to the length of the returned string. CH must be one of + [aAeEfFgG]; different from vfprintf.c:cvt(), the return string + lives in BUF regardless of CH. LEN is the length of BUF, except + when CH is [aA], in which case LEN is not in use. If BUF is not + large enough for the converted string, only the first LEN number + of characters will be returned in BUF, but *LENGTH will be set to + the full length of the string before the truncation. */ +static wchar_t * +wcvt(struct _reent *data, _PRINTF_FLOAT_TYPE value, int ndigits, int flags, + wchar_t *sign, int *decpt, int ch, int *length, wchar_t *buf, int len) +{ + int mode, dsgn; +# ifdef _NO_LONGDBL + union double_union tmp; + + tmp.d = value; + if (word0 (tmp) & Sign_bit) { /* this will check for < 0 and -0.0 */ + value = -value; + *sign = L'-'; + } else + *sign = L'\0'; +# else /* !_NO_LONGDBL */ + union + { + struct ldieee ieee; + _LONG_DOUBLE val; + } ld; + + ld.val = value; + if (ld.ieee.sign) { /* this will check for < 0 and -0.0 */ + value = -value; + *sign = L'-'; + } else + *sign = L'\0'; +# endif /* !_NO_LONGDBL */ + +# ifdef _WANT_IO_C99_FORMATS + if (ch == L'a' || ch == L'A') { + wchar_t *digits, *bp, *rve; + /* This code assumes FLT_RADIX is a power of 2. The initial + division ensures the digit before the decimal will be less + than FLT_RADIX (unless it is rounded later). There is no + loss of precision in these calculations. */ + value = FREXP (value, decpt) / 8; + if (!value) + *decpt = 1; + digits = ch == L'a' ? L"0123456789abcdef" : L"0123456789ABCDEF"; + bp = buf; + do { + value *= 16; + mode = (int) value; + value -= mode; + *bp++ = digits[mode]; + } while (ndigits-- && value); + if (value > 0.5 || (value == 0.5 && mode & 1)) { + /* round to even */ + rve = bp; + while (*--rve == digits[0xf]) { + *rve = L'0'; + } + *rve = *rve == L'9' ? digits[0xa] : *rve + 1; + } else { + while (ndigits-- >= 0) { + *bp++ = L'0'; + } + } + *length = bp - buf; + return buf; + } +# endif /* _WANT_IO_C99_FORMATS */ + if (ch == L'f' || ch == L'F') { + mode = 3; /* ndigits after the decimal point */ + } else { + /* To obtain ndigits after the decimal point for the 'e' + * and 'E' formats, round to ndigits + 1 significant + * figures. + */ + if (ch == L'e' || ch == L'E') { + ndigits++; + } + mode = 2; /* ndigits significant digits */ + } + + { + char *digits, *bp, *rve; +#ifndef _MB_CAPABLE + int i; +#endif + + digits = _DTOA_R (data, value, mode, ndigits, decpt, &dsgn, &rve); + + if ((ch != L'g' && ch != L'G') || flags & ALT) { /* Print trailing zeros */ + bp = digits + ndigits; + if (ch == L'f' || ch == L'F') { + if (*digits == L'0' && value) + *decpt = -ndigits + 1; + bp += *decpt; + } + if (value == 0) /* kludge for __dtoa irregularity */ + rve = bp; + while (rve < bp) + *rve++ = '0'; + } + + *length = rve - digits; /* full length of the string */ +#ifdef _MB_CAPABLE + _mbsnrtowcs_r (data, buf, (const char **) &digits, *length, + len, NULL); +#else + for (i = 0; i < *length && i < len; ++i) + buf[i] = (wchar_t) digits[i]; +#endif + return buf; + } +} + +static int +wexponent(wchar_t *p0, int exp, int fmtch) +{ + register wchar_t *p, *t; + wchar_t expbuf[MAXEXPLEN]; +# ifdef _WANT_IO_C99_FORMATS + int isa = fmtch == L'a' || fmtch == L'A'; +# else +# define isa 0 +# endif + + p = p0; + *p++ = isa ? L'p' - L'a' + fmtch : fmtch; + if (exp < 0) { + exp = -exp; + *p++ = L'-'; + } + else + *p++ = L'+'; + t = expbuf + MAXEXPLEN; + if (exp > 9) { + do { + *--t = to_char (exp % 10); + } while ((exp /= 10) > 9); + *--t = to_char (exp); + for (; t < expbuf + MAXEXPLEN; *p++ = *t++); + } + else { + if (!isa) + *p++ = L'0'; + *p++ = to_char (exp); + } + return (p - p0); +} +#endif /* FLOATING_POINT */ + + +#ifndef _NO_POS_ARGS + +/* Positional argument support. + Written by Jeff Johnston + + Copyright (c) 2002 Red Hat Incorporated. + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + The name of Red Hat Incorporated may not be used to endorse + or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL RED HAT INCORPORATED BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +/* function to get positional parameter N where n = N - 1 */ +static union arg_val * +_DEFUN(get_arg, (data, n, fmt, ap, numargs_p, args, arg_type, last_fmt), + struct _reent *data _AND + int n _AND + wchar_t *fmt _AND + va_list *ap _AND + int *numargs_p _AND + union arg_val *args _AND + int *arg_type _AND + wchar_t **last_fmt) +{ + wchar_t ch; + int number, flags; + int spec_type; + int numargs = *numargs_p; + __CH_CLASS chtype; + __STATE state, next_state; + __ACTION action; + int pos, last_arg; + int max_pos_arg = n; + /* Only need types that can be reached via vararg promotions. */ + enum types { INT, LONG_INT, QUAD_INT, CHAR_PTR, DOUBLE, LONG_DOUBLE, WIDE_CHAR }; + + /* if this isn't the first call, pick up where we left off last time */ + if (*last_fmt != NULL) + fmt = *last_fmt; + + /* we need to process either to end of fmt string or until we have actually + read the desired parameter from the vararg list. */ + while (*fmt && n >= numargs) + { + while (*fmt != L'\0' && *fmt != L'%') + fmt += 1; + + if (*fmt == L'\0') + break; + state = START; + flags = 0; + pos = -1; + number = 0; + spec_type = INT; + + /* Use state/action table to process format specifiers. We ignore invalid + formats and we are only interested in information that tells us how to + read the vararg list. */ + while (state != DONE) + { + ch = *fmt++; + chtype = ch < (wchar_t) 256 ? __chclass[ch] : OTHER; + next_state = __state_table[state][chtype]; + action = __action_table[state][chtype]; + state = next_state; + + switch (action) + { + case GETMOD: /* we have format modifier */ + switch (ch) + { + case L'h': + /* No flag needed, since short and char promote to int. */ + break; + case L'L': + flags |= LONGDBL; + break; + case L'q': + flags |= QUADINT; + break; +# ifdef _WANT_IO_C99_FORMATS + case L'j': + if (sizeof (intmax_t) == sizeof (long)) + flags |= LONGINT; + else + flags |= QUADINT; + break; + case L'z': + if (sizeof (size_t) <= sizeof (int)) + /* no flag needed */; + else if (sizeof (size_t) <= sizeof (long)) + flags |= LONGINT; + else + /* POSIX states that at least one programming + environment must support size_t no wider than + long, but that means other environments can + have size_t as wide as long long. */ + flags |= QUADINT; + break; + case L't': + if (sizeof (ptrdiff_t) <= sizeof (int)) + /* no flag needed */; + else if (sizeof (ptrdiff_t) <= sizeof (long)) + flags |= LONGINT; + else + /* POSIX states that at least one programming + environment must support ptrdiff_t no wider than + long, but that means other environments can + have ptrdiff_t as wide as long long. */ + flags |= QUADINT; + break; +# endif /* _WANT_IO_C99_FORMATS */ + case L'l': + default: +# if defined _WANT_IO_C99_FORMATS || !defined _NO_LONGLONG + if (*fmt == L'l') + { + flags |= QUADINT; + ++fmt; + } + else +# endif + flags |= LONGINT; + break; + } + break; + case GETARG: /* we have format specifier */ + { + numargs &= (MAX_POS_ARGS - 1); + /* process the specifier and translate it to a type to fetch from varargs */ + switch (ch) + { + case L'd': + case L'i': + case L'o': + case L'x': + case L'X': + case L'u': + if (flags & LONGINT) + spec_type = LONG_INT; +# ifndef _NO_LONGLONG + else if (flags & QUADINT) + spec_type = QUAD_INT; +# endif + else + spec_type = INT; + break; +# ifdef _WANT_IO_C99_FORMATS + case L'a': + case L'A': + case L'F': +# endif + case L'f': + case L'g': + case L'G': + case L'E': + case L'e': +# ifndef _NO_LONGDBL + if (flags & LONGDBL) + spec_type = LONG_DOUBLE; + else +# endif + spec_type = DOUBLE; + break; + case L's': +# ifdef _WANT_IO_C99_FORMATS + case L'S': /* POSIX extension */ +# endif + case L'p': + case L'n': + spec_type = CHAR_PTR; + break; + case L'c': +# ifdef _WANT_IO_C99_FORMATS + if (flags & LONGINT) + spec_type = WIDE_CHAR; + else +# endif + spec_type = INT; + break; +# ifdef _WANT_IO_C99_FORMATS + case L'C': /* POSIX extension */ + spec_type = WIDE_CHAR; + break; +# endif + } + + /* if we have a positional parameter, just store the type, otherwise + fetch the parameter from the vararg list */ + if (pos != -1) + arg_type[pos] = spec_type; + else + { + switch (spec_type) + { + case LONG_INT: + args[numargs++].val_long = va_arg (*ap, long); + break; + case QUAD_INT: + args[numargs++].val_quad_t = va_arg (*ap, quad_t); + break; + case WIDE_CHAR: + args[numargs++].val_wint_t = va_arg (*ap, wint_t); + break; + case INT: + args[numargs++].val_int = va_arg (*ap, int); + break; + case CHAR_PTR: + args[numargs++].val_wchar_ptr_t = va_arg (*ap, wchar_t *); + break; + case DOUBLE: + args[numargs++].val_double = va_arg (*ap, double); + break; + case LONG_DOUBLE: + args[numargs++].val__LONG_DOUBLE = va_arg (*ap, _LONG_DOUBLE); + break; + } + } + } + break; + case GETPOS: /* we have positional specifier */ + if (arg_type[0] == -1) + memset (arg_type, 0, sizeof (int) * MAX_POS_ARGS); + pos = number - 1; + max_pos_arg = (max_pos_arg > pos ? max_pos_arg : pos); + break; + case PWPOS: /* we have positional specifier for width or precision */ + if (arg_type[0] == -1) + memset (arg_type, 0, sizeof (int) * MAX_POS_ARGS); + number -= 1; + arg_type[number] = INT; + max_pos_arg = (max_pos_arg > number ? max_pos_arg : number); + break; + case GETPWB: /* we require format pushback */ + --fmt; + /* fallthrough */ + case GETPW: /* we have a variable precision or width to acquire */ + args[numargs++].val_int = va_arg (*ap, int); + break; + case NUMBER: /* we have a number to process */ + number = (ch - '0'); + while ((ch = *fmt) != '\0' && is_digit (ch)) + { + number = number * 10 + (ch - '0'); + ++fmt; + } + break; + case SKIPNUM: /* we have a number to skip */ + while ((ch = *fmt) != '\0' && is_digit (ch)) + ++fmt; + break; + case NOOP: + default: + break; /* do nothing */ + } + } + } + + /* process all arguments up to at least the one we are looking for and if we + have seen the end of the string, then process up to the max argument needed */ + if (*fmt == '\0') + last_arg = max_pos_arg; + else + last_arg = n; + + while (numargs <= last_arg) + { + switch (arg_type[numargs]) + { + case LONG_INT: + args[numargs++].val_long = va_arg (*ap, long); + break; + case QUAD_INT: + args[numargs++].val_quad_t = va_arg (*ap, quad_t); + break; + case CHAR_PTR: + args[numargs++].val_wchar_ptr_t = va_arg (*ap, wchar_t *); + break; + case DOUBLE: + args[numargs++].val_double = va_arg (*ap, double); + break; + case LONG_DOUBLE: + args[numargs++].val__LONG_DOUBLE = va_arg (*ap, _LONG_DOUBLE); + break; + case WIDE_CHAR: + args[numargs++].val_wint_t = va_arg (*ap, wint_t); + break; + case INT: + default: + args[numargs++].val_int = va_arg (*ap, int); + break; + } + } + + /* alter the global numargs value and keep a reference to the last bit of the fmt + string we processed here because the caller will continue processing where we started */ + *numargs_p = numargs; + *last_fmt = fmt; + return &args[n]; +} +#endif /* !_NO_POS_ARGS */ diff --git a/newlib/libc/sys/jehanne/wbuf.c b/newlib/libc/sys/jehanne/wbuf.c new file mode 100644 index 000000000..c3ad09700 --- /dev/null +++ b/newlib/libc/sys/jehanne/wbuf.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms are permitted + * provided that the above copyright notice and this paragraph are + * duplicated in all such forms and that any documentation, + * advertising materials, and other materials related to such + * distribution and use acknowledge that the software was developed + * by the University of California, Berkeley. The name of the + * University may not be used to endorse or promote products derived + * from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. + */ +/* No user fns here. Pesch 15apr92. */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char sccsid[] = "%W% (Berkeley) %G%"; +#endif /* LIBC_SCCS and not lint */ + +#include <_ansi.h> +#include +#include +#include "local.h" +#include "fvwrite.h" + +/* + * Write the given character into the (probably full) buffer for + * the given file. Flush the buffer out if it is or becomes full, + * or if c=='\n' and the file is line buffered. + */ + +#pragma GCC push_option +#pragma GCC optimize("O0") + +int +_DEFUN(__swbuf_r, (ptr, c, fp), + struct _reent *ptr _AND + register int c _AND + register FILE *fp) +{ + register int n; + + /* Ensure stdio has been initialized. */ + + CHECK_INIT (ptr, fp); + + /* + * In case we cannot write, or longjmp takes us out early, + * make sure _w is 0 (if fully- or un-buffered) or -_bf._size + * (if line buffered) so that we will get called again. + * If we did not do this, a sufficient number of putc() + * calls might wrap _w from negative to positive. + */ + + fp->_w = fp->_lbfsize; + if (cantwrite (ptr, fp)) + return EOF; + c = (unsigned char) c; + + ORIENT (fp, -1); + + /* + * If it is completely full, flush it out. Then, in any case, + * stuff c into the buffer. If this causes the buffer to fill + * completely, or if c is '\n' and the file is line buffered, + * flush it (perhaps a second time). The second flush will always + * happen on unbuffered streams, where _bf._size==1; fflush() + * guarantees that putc() will always call wbuf() by setting _w + * to 0, so we need not do anything else. + */ + + n = fp->_p - fp->_bf._base; + if (n >= fp->_bf._size) + { + if (_fflush_r (ptr, fp)) + return EOF; + n = 0; + } + fp->_w--; + *fp->_p++ = c; + if (++n == fp->_bf._size || (fp->_flags & __SLBF && c == '\n')) + if (_fflush_r (ptr, fp)) + return EOF; + return c; +} + +#pragma GCC pop_option + +/* This function isn't any longer declared in stdio.h, but it's + required for backward compatibility with applications built against + earlier dynamically built newlib libraries. */ +int +_DEFUN(__swbuf, (c, fp), + register int c _AND + register FILE *fp) +{ + return __swbuf_r (_REENT, c, fp); +}