]> Savannah Git Hosting - gnulib.git/commitdiff
vzsprintf: New module.
authorBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 10:16:00 +0000 (12:16 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 10:16:00 +0000 (12:16 +0200)
* lib/stdio.in.h (vzsprintf): New declaration, based on
lib/vsprintf.c.
* lib/vzsprintf.c: New file, based on lib/vsprintf.c.
* m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
GNULIB_VZSPRINTF.
* modules/stdio (Makefile.am): Substitute GNULIB_VZSPRINTF.
* modules/vzsprintf: New file.

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

index 68677a44ad81b41703d95af2a527591426876d43..470192143c5a9d0a97c5c1d0d75b1586f6229685 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-06-22  Bruno Haible  <bruno@clisp.org>
+
+       vzsprintf: New module.
+       * lib/stdio.in.h (vzsprintf): New declaration, based on
+       lib/vsprintf.c.
+       * lib/vzsprintf.c: New file, based on lib/vsprintf.c.
+       * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
+       GNULIB_VZSPRINTF.
+       * modules/stdio (Makefile.am): Substitute GNULIB_VZSPRINTF.
+       * modules/vzsprintf: New file.
+
 2024-06-22  Bruno Haible  <bruno@clisp.org>
 
        snprintf: Use vzsnprintf.
index d0479c10ae3758d650c7d6536fe9bd2d84ff834f..2d7e20e3d1dafa371f7bf3006cf4b875319160db 100644 (file)
@@ -1854,6 +1854,23 @@ _GL_WARN_ON_USE (vsnprintf, "vsnprintf is unportable - "
 # endif
 #endif
 
+#if @GNULIB_VZSPRINTF@
+/* Prints formatted output to string STR.
+   Returns the string length of the formatted string.  Upon failure,
+   returns -1 with errno set.
+   Failure code 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 code is ENOMEM.  */
+_GL_FUNCDECL_SYS (vzsprintf, ptrdiff_t,
+                  (char *restrict str,
+                   const char *restrict format, va_list args)
+                  _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 0)
+                  _GL_ARG_NONNULL ((1, 2)));
+_GL_CXXALIAS_SYS (vzsprintf, ptrdiff_t,
+                  (char *restrict str,
+                   const char *restrict format, va_list args));
+#endif
+
 #if @GNULIB_VSPRINTF_POSIX@
 # if @REPLACE_VSPRINTF@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
diff --git a/lib/vzsprintf.c b/lib/vzsprintf.c
new file mode 100644 (file)
index 0000000..5495430
--- /dev/null
@@ -0,0 +1,64 @@
+/* Formatted output to strings.
+   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 <stdint.h>
+#include <stdlib.h>
+
+#include "vasnprintf.h"
+
+ptrdiff_t
+vzsprintf (char *str, const char *format, va_list args)
+{
+  char *output;
+  size_t len;
+  size_t lenbuf;
+
+  /* Set lenbuf = min (SIZE_MAX, - (uintptr_t) str - 1).  */
+  lenbuf = SIZE_MAX;
+  if (lenbuf >= ~ (uintptr_t) str)
+    lenbuf = ~ (uintptr_t) str;
+
+  output = vasnprintf (str, &lenbuf, format, args);
+  len = lenbuf;
+
+  if (!output)
+    return -1;
+
+  if (output != str)
+    {
+      /* len is near SIZE_MAX.  */
+      free (output);
+      errno = ENOMEM;
+      return -1;
+    }
+
+  if (len > PTRDIFF_MAX)
+    {
+      errno = ENOMEM;
+      return -1;
+    }
+
+  return len;
+}
index 28d1c96c7438be130b701303f43f3139e9127326..f549ca48107770e9f09915af21c4992993ed9423 100644 (file)
@@ -1,5 +1,5 @@
 # stdio_h.m4
-# serial 65
+# serial 66
 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,
@@ -187,6 +187,7 @@ AC_DEFUN([gl_STDIO_H_REQUIRE_DEFAULTS],
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VSNPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VSPRINTF_POSIX])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VZSNPRINTF])
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VZSPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ZSNPRINTF])
     dnl Support Microsoft deprecated alias function names by default.
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MDA_FCLOSEALL], [1])
index 9e676681ddc2b8be2e184502d412e9b16de05173..4e41ae13e8e7228fb1e206e4eae44fad3f16df4e 100644 (file)
@@ -122,6 +122,7 @@ stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H)
              -e 's/@''GNULIB_VSNPRINTF''@/$(GNULIB_VSNPRINTF)/g' \
              -e 's/@''GNULIB_VSPRINTF_POSIX''@/$(GNULIB_VSPRINTF_POSIX)/g' \
              -e 's/@''GNULIB_VZSNPRINTF''@/$(GNULIB_VZSNPRINTF)/g' \
+             -e 's/@''GNULIB_VZSPRINTF''@/$(GNULIB_VZSPRINTF)/g' \
              -e 's/@''GNULIB_ZSNPRINTF''@/$(GNULIB_ZSNPRINTF)/g' \
              -e 's/@''GNULIB_MDA_FCLOSEALL''@/$(GNULIB_MDA_FCLOSEALL)/g' \
              -e 's/@''GNULIB_MDA_FDOPEN''@/$(GNULIB_MDA_FDOPEN)/g' \
diff --git a/modules/vzsprintf b/modules/vzsprintf
new file mode 100644 (file)
index 0000000..6564c02
--- /dev/null
@@ -0,0 +1,27 @@
+Description:
+vzsprintf() function: print formatted output from an stdarg argument list
+to a string (without INT_MAX limitation)
+
+Files:
+lib/vzsprintf.c
+
+Depends-on:
+stdio
+vasnprintf
+errno
+stdint
+
+configure.ac:
+gl_STDIO_MODULE_INDICATOR([vzsprintf])
+
+Makefile.am:
+lib_SOURCES += vzsprintf.c
+
+Include:
+<stdio.h>
+
+License:
+LGPL
+
+Maintainer:
+all