And here's the full POSIX compliant fprintf() module. 2007-03-08 Bruno Haible <[EMAIL PROTECTED]>
* 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. ============================ modules/fprintf-posix ============================= 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: <stdio.h> License: LGPL Maintainer: Bruno Haible ============================== lib/fprintf.c =================================== /* 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 <config.h> #endif /* Specification. */ #include <stdio.h> #include <errno.h> #include <limits.h> #include <stdarg.h> #include <stdlib.h> #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; } ============================ m4/fprintf-posix.m4 =============================== # 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], [:]) ================================================================================ *** m4/stdio_h.m4 9 Mar 2007 02:40:14 -0000 1.6 --- m4/stdio_h.m4 9 Mar 2007 02:56:17 -0000 *************** *** 21,32 **** --- 21,34 ---- 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]) *** lib/stdio_.h 9 Mar 2007 02:40:14 -0000 1.8 --- lib/stdio_.h 9 Mar 2007 02:56:17 -0000 *************** *** 40,45 **** --- 40,59 ---- #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 *** modules/stdio 9 Mar 2007 02:40:14 -0000 1.6 --- modules/stdio 9 Mar 2007 02:56:17 -0000 *************** *** 21,31 **** --- 21,33 ---- rm -f [EMAIL PROTECTED] $@ { 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' \