]> Savannah Git Hosting - gnulib.git/commitdiff
vfzprintf: New module.
authorBruno Haible <bruno@clisp.org>
Sun, 30 Jun 2024 16:31:56 +0000 (18:31 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 30 Jun 2024 16:31:56 +0000 (18:31 +0200)
* lib/stdio.in.h (vfzprintf): New declaration.
* lib/vfzprintf.c: New file, based on lib/vfprintf.c and
lib/vdzprintf.c.
* m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
GNULIB_VFZPRINTF.
* modules/stdio (Makefile.am): Substitute GNULIB_VFZPRINTF.
* modules/vfzprintf: New file.

ChangeLog
lib/stdio.in.h
lib/vfzprintf.c [new file with mode: 0644]
m4/stdio_h.m4
modules/stdio
modules/vfzprintf [new file with mode: 0644]

index a1c373225481f8c669d0c25c5bceb8eb09a5e83a..bb0e4aa50da84ba997f1618a190dcc4026dd781f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-06-30  Bruno Haible  <bruno@clisp.org>
+
+       vfzprintf: New module.
+       * lib/stdio.in.h (vfzprintf): New declaration.
+       * lib/vfzprintf.c: New file, based on lib/vfprintf.c and
+       lib/vdzprintf.c.
+       * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
+       GNULIB_VFZPRINTF.
+       * modules/stdio (Makefile.am): Substitute GNULIB_VFZPRINTF.
+       * modules/vfzprintf: New file.
+
 2024-06-30  Bruno Haible  <bruno@clisp.org>
 
        dzprintf-gnu: Add tests.
index acbe14cc419d87de87b217d629f2b3f13fc0a85d..e4a09979199fd74e486948a68b90f79d64a20263 100644 (file)
@@ -1775,6 +1775,24 @@ _GL_WARN_ON_USE (vdprintf, "vdprintf is unportable - "
 # endif
 #endif
 
+#if @GNULIB_VFZPRINTF@
+/* Prints formatted output to stream FP.
+   Returns the number of bytes written to the stream.  Upon failure,
+   returns -1 with the stream's error indicator set.
+   Failure cause EOVERFLOW can only occur when a width > INT_MAX is used.
+   Therefore, if the format string is valid and does not use %ls/%lc
+   directives nor widths, the only possible failure causes are ENOMEM
+   and the possible failure causes from fwrite().  */
+_GL_FUNCDECL_SYS (vfzprintf, off64_t,
+                  (FILE *restrict fp,
+                   const char *restrict format, va_list args)
+                  _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 0)
+                  _GL_ARG_NONNULL ((1, 2)));
+_GL_CXXALIAS_SYS (vfzprintf, off64_t,
+                  (FILE *restrict fp,
+                   const char *restrict format, va_list args));
+#endif
+
 #if @GNULIB_VFPRINTF_POSIX@ || @GNULIB_VFPRINTF@
 # if (@GNULIB_VFPRINTF_POSIX@ && @REPLACE_VFPRINTF@) \
      || (@GNULIB_VFPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && (@GNULIB_STDIO_H_NONBLOCKING@ || @GNULIB_STDIO_H_SIGPIPE@))
diff --git a/lib/vfzprintf.c b/lib/vfzprintf.c
new file mode 100644 (file)
index 0000000..94e71a2
--- /dev/null
@@ -0,0 +1,72 @@
+/* Formatted output to a stream.
+   Copyright (C) 2004, 2006-2024 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation, either version 3 of the
+   License, or (at your option) any later version.
+
+   This file 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 Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Specification.  */
+#include <stdio.h>
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "fseterr.h"
+#include "intprops.h"
+#include "vasnprintf.h"
+
+off64_t
+vfzprintf (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 (len > TYPE_MAXIMUM (off64_t))
+    {
+      /* We could write the (huge) output, but then could not return len, as it
+         would be negative.  Treat this case as if vasnprintf had already
+         encountered an out-of-memory situation.  */
+      if (output != buf)
+        free (output);
+      fseterr (fp);
+      errno = ENOMEM;
+      return -1;
+    }
+
+  if (fwrite (output, 1, len, fp) < len)
+    {
+      if (output != buf)
+        free (output);
+      return -1;
+    }
+
+  if (output != buf)
+    free (output);
+
+  return len;
+}
index 126e395fae9a84c773ecb48812e9a1b1965b83fa..090d5cd11086d01f6262c4fc8d1f4508235bd5db 100644 (file)
@@ -1,5 +1,5 @@
 # stdio_h.m4
-# serial 71
+# serial 72
 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -188,6 +188,7 @@ AC_DEFUN([gl_STDIO_H_REQUIRE_DEFAULTS],
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VDZPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VFPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VFPRINTF_POSIX])
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VFZPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VPRINTF_POSIX])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VSNPRINTF])
index 22008a2b902cde521c97f1706eea708337b074d1..ec8cf240d63794bc5ed8edb43a5563c44177bef4 100644 (file)
@@ -121,6 +121,7 @@ stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H)
              -e 's/@''GNULIB_VDZPRINTF''@/$(GNULIB_VDZPRINTF)/g' \
              -e 's/@''GNULIB_VFPRINTF''@/$(GNULIB_VFPRINTF)/g' \
              -e 's/@''GNULIB_VFPRINTF_POSIX''@/$(GNULIB_VFPRINTF_POSIX)/g' \
+             -e 's/@''GNULIB_VFZPRINTF''@/$(GNULIB_VFZPRINTF)/g' \
              -e 's/@''GNULIB_VFSCANF''@/$(GNULIB_VFSCANF)/g' \
              -e 's/@''GNULIB_VSCANF''@/$(GNULIB_VSCANF)/g' \
              -e 's/@''GNULIB_VPRINTF''@/$(GNULIB_VPRINTF)/g' \
diff --git a/modules/vfzprintf b/modules/vfzprintf
new file mode 100644 (file)
index 0000000..45bafdb
--- /dev/null
@@ -0,0 +1,29 @@
+Description:
+vfzprintf() function: print formatted output (without INT_MAX limitation)
+to a stream
+
+Files:
+lib/vfzprintf.c
+
+Depends-on:
+stdio
+vasnprintf
+intprops
+free-posix
+fseterr
+errno
+
+configure.ac:
+gl_STDIO_MODULE_INDICATOR([vfzprintf])
+
+Makefile.am:
+lib_SOURCES += vfzprintf.c
+
+Include:
+<stdio.h>
+
+License:
+LGPL
+
+Maintainer:
+all