From d2b0c77abf5b72cbf78ec4e4e404c851950b0fef Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 9 Mar 2007 02:40:14 +0000 Subject: [PATCH] New module 'vfprintf-posix'. --- ChangeLog | 11 ++++++++ lib/stdio_.h | 14 +++++++++ lib/vfprintf.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ m4/stdio_h.m4 | 2 ++ m4/vfprintf-posix.m4 | 52 ++++++++++++++++++++++++++++++++++ modules/stdio | 2 ++ modules/vfprintf-posix | 32 +++++++++++++++++++++ 7 files changed, 190 insertions(+) create mode 100644 lib/vfprintf.c create mode 100644 m4/vfprintf-posix.m4 create mode 100644 modules/vfprintf-posix diff --git a/ChangeLog b/ChangeLog index 42050171f..2d45e6959 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2007-03-08 Bruno Haible + * modules/vfprintf-posix: New file. + * lib/vfprintf.c: New file. + * m4/vfprintf-posix.m4: New file. + * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Set also GNULIB_VFPRINTF_POSIX, + REPLACE_VFPRINTF. + * lib/stdio_.h (vfprintf): New declaration. + * modules/stdio (Makefile.am): Substitute also GNULIB_VFPRINTF_POSIX, + REPLACE_VFPRINTF. + +2007-03-08 Bruno Haible + * lib/stdio_.h: Treat __need___FILE like __need_FILE. 2007-03-08 Bruno Haible diff --git a/lib/stdio_.h b/lib/stdio_.h index ceba8a14f..0ed942e50 100644 --- a/lib/stdio_.h +++ b/lib/stdio_.h @@ -40,6 +40,20 @@ extern "C" { #endif +#if @GNULIB_VFPRINTF_POSIX@ +# if @REPLACE_VFPRINTF@ +# define vfprintf rpl_vfprintf +extern int vfprintf (FILE *fp, const char *format, va_list args); +# endif +#elif defined GNULIB_POSIXCHECK +# undef vfprintf +# define vfprintf(s,f,a) \ + (GL_LINK_WARNING ("vfprintf is not always POSIX compliant - " \ + "use gnulib module vfprintf-posix for portable " \ + "POSIX compliance"), \ + vfprintf (s, f, a)) +#endif + #if @GNULIB_SNPRINTF@ # if @REPLACE_SNPRINTF@ # define snprintf rpl_snprintf diff --git a/lib/vfprintf.c b/lib/vfprintf.c new file mode 100644 index 000000000..b73642adb --- /dev/null +++ b/lib/vfprintf.c @@ -0,0 +1,77 @@ +/* Formatted output to a stream. + Copyright (C) 2004, 2006-2007 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +/* Specification. */ +#include + +#include +#include +#include +#include + +#include "fseterr.h" +#include "vasnprintf.h" + +/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */ +#ifndef EOVERFLOW +# define EOVERFLOW E2BIG +#endif + +/* Print formatted output to the stream FP. + Return string length of formatted string. On error, return a negative + value. */ +int +vfprintf (FILE *fp, const char *format, va_list args) +{ + char buf[2000]; + char *output; + size_t len; + size_t lenbuf = sizeof (buf); + + output = vasnprintf (buf, &lenbuf, format, args); + len = lenbuf; + + if (!output) + { + fseterr (fp); + return -1; + } + + if (fwrite (output, 1, len, fp) < len) + { + if (output != buf) + { + int saved_errno = errno; + free (output); + errno = saved_errno; + } + return -1; + } + + if (len > INT_MAX) + { + errno = EOVERFLOW; + fseterr (fp); + return -1; + } + + return len; +} diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4 index 14e7d7508..503cd3494 100644 --- a/m4/stdio_h.m4 +++ b/m4/stdio_h.m4 @@ -23,9 +23,11 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS], [ GNULIB_SNPRINTF=0; AC_SUBST([GNULIB_SNPRINTF]) GNULIB_SPRINTF_POSIX=0; AC_SUBST([GNULIB_SPRINTF_POSIX]) + GNULIB_VFPRINTF_POSIX=0; AC_SUBST([GNULIB_VFPRINTF_POSIX]) GNULIB_VSNPRINTF=0; AC_SUBST([GNULIB_VSNPRINTF]) GNULIB_VSPRINTF_POSIX=0; AC_SUBST([GNULIB_VSPRINTF_POSIX]) dnl Assume proper GNU behavior unless another module says otherwise. + REPLACE_VFPRINTF=0; AC_SUBST([REPLACE_VFPRINTF]) REPLACE_SNPRINTF=0; AC_SUBST([REPLACE_SNPRINTF]) HAVE_DECL_SNPRINTF=1; AC_SUBST([HAVE_DECL_SNPRINTF]) REPLACE_VSNPRINTF=0; AC_SUBST([REPLACE_VSNPRINTF]) diff --git a/m4/vfprintf-posix.m4 b/m4/vfprintf-posix.m4 new file mode 100644 index 000000000..11cbfda42 --- /dev/null +++ b/m4/vfprintf-posix.m4 @@ -0,0 +1,52 @@ +# vfprintf-posix.m4 serial 1 +dnl Copyright (C) 2007 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_VFPRINTF_POSIX], +[ + AC_REQUIRE([gl_EOVERFLOW]) + AC_REQUIRE([gl_PRINTF_SIZES_C99]) + AC_REQUIRE([gl_PRINTF_DIRECTIVE_A]) + AC_REQUIRE([gl_PRINTF_DIRECTIVE_N]) + AC_REQUIRE([gl_PRINTF_POSITIONS]) + gl_cv_func_vfprintf_posix=no + case "$gl_cv_func_printf_sizes_c99" in + *yes) + case "$gl_cv_func_printf_directive_a" in + *yes) + case "$gl_cv_func_printf_directive_n" in + *yes) + case "$gl_cv_func_printf_positions" in + *yes) + # vfprintf exists and is already POSIX compliant. + gl_cv_func_vfprintf_posix=yes + ;; + esac + ;; + esac + ;; + esac + ;; + esac + if test $gl_cv_func_vfprintf_posix = no; then + if ! expr "$gl_cv_func_printf_directive_a" : ".*yes" > /dev/null; then + AC_DEFINE([NEED_PRINTF_DIRECTIVE_A], 1, + [Define if the vasnprintf implementation needs special code for + the 'a' and 'A' directives.]) + fi + gl_REPLACE_VASNPRINTF + gl_REPLACE_VFPRINTF + fi +]) + +AC_DEFUN([gl_REPLACE_VFPRINTF], +[ + AC_REQUIRE([gl_STDIO_H_DEFAULTS]) + AC_LIBOBJ([vfprintf]) + REPLACE_VFPRINTF=1 + gl_PREREQ_VFPRINTF +]) + +AC_DEFUN([gl_PREREQ_VFPRINTF], [:]) diff --git a/modules/stdio b/modules/stdio index 3a8bdf4fe..d4b02d466 100644 --- a/modules/stdio +++ b/modules/stdio @@ -23,8 +23,10 @@ stdio.h: stdio_.h sed -e 's|@''ABSOLUTE_STDIO_H''@|$(ABSOLUTE_STDIO_H)|g' \ -e 's|@''GNULIB_SNPRINTF''@|$(GNULIB_SNPRINTF)|g' \ -e 's|@''GNULIB_SPRINTF_POSIX''@|$(GNULIB_SPRINTF_POSIX)|g' \ + -e 's|@''GNULIB_VFPRINTF_POSIX''@|$(GNULIB_VFPRINTF_POSIX)|g' \ -e 's|@''GNULIB_VSNPRINTF''@|$(GNULIB_VSNPRINTF)|g' \ -e 's|@''GNULIB_VSPRINTF_POSIX''@|$(GNULIB_VSPRINTF_POSIX)|g' \ + -e 's|@''REPLACE_VFPRINTF''@|$(REPLACE_VFPRINTF)|g' \ -e 's|@''REPLACE_SNPRINTF''@|$(REPLACE_SNPRINTF)|g' \ -e 's|@''HAVE_DECL_SNPRINTF''@|$(HAVE_DECL_SNPRINTF)|g' \ -e 's|@''REPLACE_VSNPRINTF''@|$(REPLACE_VSNPRINTF)|g' \ diff --git a/modules/vfprintf-posix b/modules/vfprintf-posix new file mode 100644 index 000000000..1c0adb206 --- /dev/null +++ b/modules/vfprintf-posix @@ -0,0 +1,32 @@ +Description: +POSIX compatible vfprintf() function: print formatted output to a stream + +Files: +lib/vfprintf.c +m4/vfprintf-posix.m4 +m4/printf.m4 + +Depends-on: +stdio +fseterr +vasnprintf +isnan-nolibm +isnanl-nolibm +printf-frexp +printf-frexpl + +configure.ac: +gl_FUNC_VFPRINTF_POSIX +gl_STDIO_MODULE_INDICATOR([vfprintf-posix]) + +Makefile.am: + +Include: + + +License: +LGPL + +Maintainer: +Bruno Haible + -- 2.11.0