From 685bc46ea352d770e54f02050312df99fd21c9d8 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 9 Mar 2007 02:59:39 +0000 Subject: [PATCH] New module 'fprintf-posix'. --- ChangeLog | 11 +++++++ lib/fprintf.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/stdio_.h | 14 +++++++++ m4/fprintf-posix.m4 | 52 +++++++++++++++++++++++++++++++++ m4/stdio_h.m4 | 2 ++ modules/fprintf-posix | 32 +++++++++++++++++++++ modules/stdio | 2 ++ 7 files changed, 193 insertions(+) create mode 100644 lib/fprintf.c create mode 100644 m4/fprintf-posix.m4 create mode 100644 modules/fprintf-posix diff --git a/ChangeLog b/ChangeLog index b9ee3ff8b..8ed30e0e7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2007-03-08 Bruno Haible + * modules/fprintf-posix: New file. + * lib/fprintf.c: New file. + * m4/fprintf-posix.m4: New file. + * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Set also GNULIB_FPRINTF_POSIX, + REPLACE_FPRINTF. + * lib/stdio_.h (fprintf): New declaration. + * modules/stdio (Makefile.am): Substitute also GNULIB_FPRINTF_POSIX, + REPLACE_FPRINTF. + +2007-03-08 Bruno Haible + * modules/vfprintf-posix-tests: New file. * tests/test-vfprintf-posix.sh: New file. * tests/test-vfprintf-posix.c: New file. diff --git a/lib/fprintf.c b/lib/fprintf.c new file mode 100644 index 000000000..2eee2757a --- /dev/null +++ b/lib/fprintf.c @@ -0,0 +1,80 @@ +/* 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 +fprintf (FILE *fp, const char *format, ...) +{ + char buf[2000]; + char *output; + size_t len; + size_t lenbuf = sizeof (buf); + va_list args; + + va_start (args, format); + output = vasnprintf (buf, &lenbuf, format, args); + len = lenbuf; + va_end (args); + + 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/lib/stdio_.h b/lib/stdio_.h index 0ed942e50..7be355aac 100644 --- a/lib/stdio_.h +++ b/lib/stdio_.h @@ -40,6 +40,20 @@ extern "C" { #endif +#if @GNULIB_FPRINTF_POSIX@ +# if @REPLACE_FPRINTF@ +# define fprintf rpl_fprintf +extern int fprintf (FILE *fp, const char *format, ...); +# endif +#elif defined GNULIB_POSIXCHECK +# undef fprintf +# define fprintf \ + (GL_LINK_WARNING ("fprintf is not always POSIX compliant - " \ + "use gnulib module fprintf-posix for portable " \ + "POSIX compliance"), \ + fprintf) +#endif + #if @GNULIB_VFPRINTF_POSIX@ # if @REPLACE_VFPRINTF@ # define vfprintf rpl_vfprintf diff --git a/m4/fprintf-posix.m4 b/m4/fprintf-posix.m4 new file mode 100644 index 000000000..60e519d60 --- /dev/null +++ b/m4/fprintf-posix.m4 @@ -0,0 +1,52 @@ +# fprintf-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_FPRINTF_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_fprintf_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) + # fprintf exists and is already POSIX compliant. + gl_cv_func_fprintf_posix=yes + ;; + esac + ;; + esac + ;; + esac + ;; + esac + if test $gl_cv_func_fprintf_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_FPRINTF + fi +]) + +AC_DEFUN([gl_REPLACE_FPRINTF], +[ + AC_REQUIRE([gl_STDIO_H_DEFAULTS]) + AC_LIBOBJ([fprintf]) + REPLACE_FPRINTF=1 + gl_PREREQ_FPRINTF +]) + +AC_DEFUN([gl_PREREQ_FPRINTF], [:]) diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4 index 503cd3494..e50d964d6 100644 --- a/m4/stdio_h.m4 +++ b/m4/stdio_h.m4 @@ -21,12 +21,14 @@ AC_DEFUN([gl_STDIO_MODULE_INDICATOR], AC_DEFUN([gl_STDIO_H_DEFAULTS], [ + GNULIB_FPRINTF_POSIX=0; AC_SUBST([GNULIB_FPRINTF_POSIX]) 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_FPRINTF=0; AC_SUBST([REPLACE_FPRINTF]) REPLACE_VFPRINTF=0; AC_SUBST([REPLACE_VFPRINTF]) REPLACE_SNPRINTF=0; AC_SUBST([REPLACE_SNPRINTF]) HAVE_DECL_SNPRINTF=1; AC_SUBST([HAVE_DECL_SNPRINTF]) diff --git a/modules/fprintf-posix b/modules/fprintf-posix new file mode 100644 index 000000000..f79a8b428 --- /dev/null +++ b/modules/fprintf-posix @@ -0,0 +1,32 @@ +Description: +POSIX compatible fprintf() function: print formatted output to a stream + +Files: +lib/fprintf.c +m4/fprintf-posix.m4 +m4/printf.m4 + +Depends-on: +stdio +fseterr +vasnprintf +isnan-nolibm +isnanl-nolibm +printf-frexp +printf-frexpl + +configure.ac: +gl_FUNC_FPRINTF_POSIX +gl_STDIO_MODULE_INDICATOR([fprintf-posix]) + +Makefile.am: + +Include: + + +License: +LGPL + +Maintainer: +Bruno Haible + diff --git a/modules/stdio b/modules/stdio index d4b02d466..12508797b 100644 --- a/modules/stdio +++ b/modules/stdio @@ -21,11 +21,13 @@ stdio.h: stdio_.h rm -f $@-t $@ { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */' && \ sed -e 's|@''ABSOLUTE_STDIO_H''@|$(ABSOLUTE_STDIO_H)|g' \ + -e 's|@''GNULIB_FPRINTF_POSIX''@|$(GNULIB_FPRINTF_POSIX)|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_FPRINTF''@|$(REPLACE_FPRINTF)|g' \ -e 's|@''REPLACE_VFPRINTF''@|$(REPLACE_VFPRINTF)|g' \ -e 's|@''REPLACE_SNPRINTF''@|$(REPLACE_SNPRINTF)|g' \ -e 's|@''HAVE_DECL_SNPRINTF''@|$(HAVE_DECL_SNPRINTF)|g' \ -- 2.11.0