]> Savannah Git Hosting - gnulib.git/commitdiff
fzprintf: New module.
authorBruno Haible <bruno@clisp.org>
Sun, 30 Jun 2024 17:40:35 +0000 (19:40 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 30 Jun 2024 17:40:35 +0000 (19:40 +0200)
* lib/stdio.in.h (fzprintf): New declaration.
* lib/fzprintf.c: New file, based on lib/vfzprintf.c.
* m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
GNULIB_FZPRINTF.
* modules/stdio (Makefile.am): Substitute GNULIB_FZPRINTF.
* modules/fzprintf: New file.

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

index 51af63d1b8ea67ab87fd25ac957040c521561b46..bcbc7bc7c59040f538791862b8c499b4ceedb008 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2024-06-30  Bruno Haible  <bruno@clisp.org>
+
+       fzprintf: New module.
+       * lib/stdio.in.h (fzprintf): New declaration.
+       * lib/fzprintf.c: New file, based on lib/vfzprintf.c.
+       * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
+       GNULIB_FZPRINTF.
+       * modules/stdio (Makefile.am): Substitute GNULIB_FZPRINTF.
+       * modules/fzprintf: New file.
+
 2024-06-30  Bruno Haible  <bruno@clisp.org>
 
        vfprintf-posix: Use vfzprintf.
diff --git a/lib/fzprintf.c b/lib/fzprintf.c
new file mode 100644 (file)
index 0000000..b8df994
--- /dev/null
@@ -0,0 +1,75 @@
+/* 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
+fzprintf (FILE *fp, const char *format, ...)
+{
+  va_list args;
+  char buf[2000];
+  char *output;
+  size_t len;
+  size_t lenbuf = sizeof (buf);
+
+  va_start (args, format);
+  output = vasnprintf (buf, &lenbuf, format, args);
+  len = lenbuf;
+  va_end (args);
+
+  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 8802e878327072cb18cc14a13392855808cfe0d7..4ce51a743c92fac31c9a10edd3208581eb973fa0 100644 (file)
@@ -566,6 +566,22 @@ _GL_WARN_ON_USE (fopen, "fopen on native Windows platforms is not POSIX complian
 # endif
 #endif
 
+#if @GNULIB_FZPRINTF@
+/* 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 (fzprintf, off64_t,
+                  (FILE *restrict fp, const char *restrict format, ...)
+                  _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 3)
+                  _GL_ARG_NONNULL ((1, 2)));
+_GL_CXXALIAS_SYS (fzprintf, off64_t,
+                  (FILE *restrict fp, const char *restrict format, ...));
+#endif
+
 #if @GNULIB_FPRINTF_POSIX@ || @GNULIB_FPRINTF@
 # if (@GNULIB_FPRINTF_POSIX@ && @REPLACE_FPRINTF@) \
      || (@GNULIB_FPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && (@GNULIB_STDIO_H_NONBLOCKING@ || @GNULIB_STDIO_H_SIGPIPE@))
index 090d5cd11086d01f6262c4fc8d1f4508235bd5db..542280100a5a42a38f2c4b1e96f61228ce1fd512 100644 (file)
@@ -1,5 +1,5 @@
 # stdio_h.m4
-# serial 72
+# serial 73
 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,
@@ -154,6 +154,7 @@ AC_DEFUN([gl_STDIO_H_REQUIRE_DEFAULTS],
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_FTELL])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_FTELLO])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_FWRITE])
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_FZPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETC])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETCHAR])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETDELIM])
diff --git a/modules/fzprintf b/modules/fzprintf
new file mode 100644 (file)
index 0000000..b822f26
--- /dev/null
@@ -0,0 +1,29 @@
+Description:
+fzprintf() function: print formatted output (without INT_MAX limitation)
+to a stream
+
+Files:
+lib/fzprintf.c
+
+Depends-on:
+stdio
+vasnprintf
+intprops
+free-posix
+fseterr
+errno
+
+configure.ac:
+gl_STDIO_MODULE_INDICATOR([fzprintf])
+
+Makefile.am:
+lib_SOURCES += fzprintf.c
+
+Include:
+<stdio.h>
+
+License:
+LGPL
+
+Maintainer:
+all
index ec8cf240d63794bc5ed8edb43a5563c44177bef4..a366b51a85d141d6536357c2d87ab55c4b9cfe10 100644 (file)
@@ -89,6 +89,7 @@ stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H)
              -e 's/@''GNULIB_FTELL''@/$(GNULIB_FTELL)/g' \
              -e 's/@''GNULIB_FTELLO''@/$(GNULIB_FTELLO)/g' \
              -e 's/@''GNULIB_FWRITE''@/$(GNULIB_FWRITE)/g' \
+             -e 's/@''GNULIB_FZPRINTF''@/$(GNULIB_FZPRINTF)/g' \
              -e 's/@''GNULIB_GETC''@/$(GNULIB_GETC)/g' \
              -e 's/@''GNULIB_GETCHAR''@/$(GNULIB_GETCHAR)/g' \
              -e 's/@''GNULIB_GETDELIM''@/$(GNULIB_GETDELIM)/g' \