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

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

index 8e82912063b7dac1eb92bd8aad3bb7b1c040b10a..60ed44502154fa52e6ff1860057c3ebe7b6e196a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-06-22  Bruno Haible  <bruno@clisp.org>
+
+       vzsnprintf: New module.
+       * lib/stdio.in.h (vzsnprintf): New declaration, based on
+       lib/vsnprintf.c.
+       * lib/vzsnprintf.c: New file, based on lib/vsnprintf.c.
+       * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
+       GNULIB_VZSNPRINTF.
+       * modules/stdio (Makefile.am): Substitute GNULIB_VZSNPRINTF.
+       * modules/vzsnprintf: New file.
+
 2024-06-20  Bruno Haible  <bruno@clisp.org>
 
        test-framework-sh: Fix side effect on dfa tests (regression 2024-06-11).
index 1c0c9661bfeb61312642b457716f8532453ba7b1..d8d27a56c53abc0b499604fdcbbc7bec55ffcffc 100644 (file)
@@ -1769,6 +1769,25 @@ _GL_CXXALIASWARN (vscanf);
 # endif
 #endif
 
+#if @GNULIB_VZSNPRINTF@
+/* Prints formatted output to string STR.  Similar to sprintf, but the
+   additional parameter SIZE limits how much is written into STR.
+   STR may be NULL, in which case nothing will be written.
+   Returns the string length of the formatted string (which may be larger
+   than SIZE).  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 (vzsnprintf, ptrdiff_t,
+                  (char *restrict str, size_t size,
+                   const char *restrict format, va_list args)
+                  _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (3, 0)
+                  _GL_ARG_NONNULL ((3)));
+_GL_CXXALIAS_SYS (vzsnprintf, ptrdiff_t,
+                  (char *restrict str, size_t size,
+                   const char *restrict format, va_list args));
+#endif
+
 #if @GNULIB_VSNPRINTF@
 # if @REPLACE_VSNPRINTF@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
diff --git a/lib/vzsnprintf.c b/lib/vzsnprintf.c
new file mode 100644 (file)
index 0000000..96e240b
--- /dev/null
@@ -0,0 +1,65 @@
+/* Formatted output to strings.
+   Copyright (C) 2004, 2006-2024 Free Software Foundation, Inc.
+   Written by Simon Josefsson and Yoann Vandoorselaere <yoann@prelude-ids.org>.
+
+   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 2.1 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 <string.h>
+
+#include "vasnprintf.h"
+
+ptrdiff_t
+vzsnprintf (char *str, size_t size, const char *format, va_list args)
+{
+  char *output;
+  size_t len;
+  size_t lenbuf = size;
+
+  output = vasnprintf (str, &lenbuf, format, args);
+  len = lenbuf;
+
+  if (!output)
+    return -1;
+
+  if (output != str)
+    {
+      if (size)
+        {
+          size_t pruned_len = (len < size ? len : size - 1);
+          memcpy (str, output, pruned_len);
+          str[pruned_len] = '\0';
+        }
+
+      free (output);
+    }
+
+  if (len > PTRDIFF_MAX)
+    {
+      errno = ENOMEM;
+      return -1;
+    }
+
+  return len;
+}
index 8eb5816ad7e916aa05ed3a58c8fb41ddeaa4e3dd..9c631f172f7b0d08384fe05c1300e0fcb4608b28 100644 (file)
@@ -1,5 +1,5 @@
 # stdio_h.m4
-# serial 63
+# serial 64
 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,
@@ -186,6 +186,7 @@ AC_DEFUN([gl_STDIO_H_REQUIRE_DEFAULTS],
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VPRINTF_POSIX])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VSNPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VSPRINTF_POSIX])
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VZSNPRINTF])
     dnl Support Microsoft deprecated alias function names by default.
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MDA_FCLOSEALL], [1])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MDA_FDOPEN], [1])
index 45ae9824ed69d690257de4eba59d110639f3ad58..63ea9a908e34d3b2ff761efa04d9b885646f0a35 100644 (file)
@@ -121,6 +121,7 @@ stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H)
              -e 's/@''GNULIB_VPRINTF_POSIX''@/$(GNULIB_VPRINTF_POSIX)/g' \
              -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_MDA_FCLOSEALL''@/$(GNULIB_MDA_FCLOSEALL)/g' \
              -e 's/@''GNULIB_MDA_FDOPEN''@/$(GNULIB_MDA_FDOPEN)/g' \
              -e 's/@''GNULIB_MDA_FILENO''@/$(GNULIB_MDA_FILENO)/g' \
diff --git a/modules/vzsnprintf b/modules/vzsnprintf
new file mode 100644 (file)
index 0000000..853d882
--- /dev/null
@@ -0,0 +1,27 @@
+Description:
+vzsnprintf() function: print formatted output from an stdarg argument list
+to a fixed length string (without INT_MAX limitation)
+
+Files:
+lib/vzsnprintf.c
+
+Depends-on:
+stdio
+vasnprintf
+errno
+stdint
+
+configure.ac:
+gl_STDIO_MODULE_INDICATOR([vzsnprintf])
+
+Makefile.am:
+lib_SOURCES += vzsnprintf.c
+
+Include:
+<stdio.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all