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

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

index 28833003fb93454748585f16c48ee45929ba718b..458561b9fed09d483b7f2d9bd881f5f923ddfb5d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-06-22  Bruno Haible  <bruno@clisp.org>
+
+       zsnprintf: New module.
+       * lib/stdio.in.h (zsnprintf): New declaration, based on
+       lib/snprintf.c.
+       * lib/zsnprintf.c: New file, based on lib/snprintf.c.
+       * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
+       GNULIB_ZSNPRINTF.
+       * modules/stdio (Makefile.am): Substitute GNULIB_ZSNPRINTF.
+       * modules/zsnprintf: New file.
+
 2024-06-22  Bruno Haible  <bruno@clisp.org>
 
        vsnprintf: Use vzsnprintf.
index 8d6cd21fd57292cf2bacdb97988831bdacdd0770..4a942a92ed74a43ed2618bbfb219ebac65baa1af 100644 (file)
@@ -1433,6 +1433,25 @@ _GL_CXXALIASWARN (scanf);
 # endif
 #endif
 
+#if @GNULIB_ZSNPRINTF@
+/* 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 (zsnprintf, ptrdiff_t,
+                  (char *restrict str, size_t size,
+                   const char *restrict format, ...)
+                  _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (3, 4)
+                  _GL_ARG_NONNULL ((3)));
+_GL_CXXALIAS_SYS (zsnprintf, ptrdiff_t,
+                  (char *restrict str, size_t size,
+                   const char *restrict format, ...));
+#endif
+
 #if @GNULIB_SNPRINTF@
 # if @REPLACE_SNPRINTF@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
diff --git a/lib/zsnprintf.c b/lib/zsnprintf.c
new file mode 100644 (file)
index 0000000..894631d
--- /dev/null
@@ -0,0 +1,66 @@
+/* Formatted output to strings.
+   Copyright (C) 2004, 2006-2024 Free Software Foundation, Inc.
+   Written by Simon Josefsson and Paul Eggert.
+
+   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/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <stdio.h>
+
+#include <errno.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "vasnprintf.h"
+
+ptrdiff_t
+zsnprintf (char *str, size_t size, const char *format, ...)
+{
+  char *output;
+  size_t len;
+  size_t lenbuf = size;
+  va_list args;
+
+  va_start (args, format);
+  output = vasnprintf (str, &lenbuf, format, args);
+  len = lenbuf;
+  va_end (args);
+
+  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 9c631f172f7b0d08384fe05c1300e0fcb4608b28..28d1c96c7438be130b701303f43f3139e9127326 100644 (file)
@@ -1,5 +1,5 @@
 # stdio_h.m4
-# serial 64
+# serial 65
 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_ZSNPRINTF])
     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 63ea9a908e34d3b2ff761efa04d9b885646f0a35..9e676681ddc2b8be2e184502d412e9b16de05173 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_ZSNPRINTF''@/$(GNULIB_ZSNPRINTF)/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/zsnprintf b/modules/zsnprintf
new file mode 100644 (file)
index 0000000..40fd329
--- /dev/null
@@ -0,0 +1,27 @@
+Description:
+zsnprintf() function: print formatted output to a fixed length string
+(without INT_MAX limitation)
+
+Files:
+lib/zsnprintf.c
+
+Depends-on:
+stdio
+vasnprintf
+errno
+stdint
+
+configure.ac:
+gl_STDIO_MODULE_INDICATOR([zsnprintf])
+
+Makefile.am:
+lib_SOURCES += zsnprintf.c
+
+Include:
+<stdio.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all