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

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

index c823d86fbc270fccb80d3becedb15216409671a9..96dc1dff2c558ad7579b2b0965243223a1ebc898 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-06-22  Bruno Haible  <bruno@clisp.org>
+
+       zsprintf: New module.
+       * lib/stdio.in.h (zsprintf): New declaration, based on
+       lib/sprintf.c.
+       * lib/zsprintf.c: New file, based on lib/sprintf.c.
+       * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize
+       GNULIB_ZSPRINTF.
+       * modules/stdio (Makefile.am): Substitute GNULIB_ZSPRINTF.
+       * modules/zsprintf: New file.
+
 2024-06-22  Bruno Haible  <bruno@clisp.org>
 
        vsprintf-posix: Use vzsprintf.
index 858ab477367e489c3807d7f79e1d85194703c803..17c662f45abe907ea6ccfc2525a9e4ef0ecc7962 100644 (file)
@@ -1494,6 +1494,23 @@ _GL_WARN_ON_USE (snprintf, "snprintf is unportable - "
 # endif
 #endif
 
+#if @GNULIB_ZSPRINTF@
+/* 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 (zsprintf, ptrdiff_t,
+                  (char *restrict str,
+                   const char *restrict format, ...)
+                  _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 3)
+                  _GL_ARG_NONNULL ((1, 2)));
+_GL_CXXALIAS_SYS (zsprintf, ptrdiff_t,
+                  (char *restrict str,
+                   const char *restrict format, ...));
+#endif
+
 /* Some people would argue that all sprintf uses should be warned about
    (for example, OpenBSD issues a link warning for it),
    since it can cause security holes due to buffer overruns.
diff --git a/lib/zsprintf.c b/lib/zsprintf.c
new file mode 100644 (file)
index 0000000..f004ed7
--- /dev/null
@@ -0,0 +1,67 @@
+/* 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
+zsprintf (char *str, const char *format, ...)
+{
+  char *output;
+  size_t len;
+  size_t lenbuf;
+  va_list args;
+
+  /* Set lenbuf = min (SIZE_MAX, - (uintptr_t) str - 1).  */
+  lenbuf = SIZE_MAX;
+  if (lenbuf >= ~ (uintptr_t) str)
+    lenbuf = ~ (uintptr_t) str;
+
+  va_start (args, format);
+  output = vasnprintf (str, &lenbuf, format, args);
+  len = lenbuf;
+  va_end (args);
+
+  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 f549ca48107770e9f09915af21c4992993ed9423..69d4b88b4beb7e51f973dea69b5f463d1f4a8e38 100644 (file)
@@ -1,5 +1,5 @@
 # stdio_h.m4
-# serial 66
+# serial 67
 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,
@@ -189,6 +189,7 @@ AC_DEFUN([gl_STDIO_H_REQUIRE_DEFAULTS],
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VZSNPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VZSPRINTF])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ZSNPRINTF])
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ZSPRINTF])
     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 4e41ae13e8e7228fb1e206e4eae44fad3f16df4e..29ed8e0bfab2b7c3a248d9bf9e9be25ae8294bf1 100644 (file)
@@ -124,6 +124,7 @@ stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H)
              -e 's/@''GNULIB_VZSNPRINTF''@/$(GNULIB_VZSNPRINTF)/g' \
              -e 's/@''GNULIB_VZSPRINTF''@/$(GNULIB_VZSPRINTF)/g' \
              -e 's/@''GNULIB_ZSNPRINTF''@/$(GNULIB_ZSNPRINTF)/g' \
+             -e 's/@''GNULIB_ZSPRINTF''@/$(GNULIB_ZSPRINTF)/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/zsprintf b/modules/zsprintf
new file mode 100644 (file)
index 0000000..7da8687
--- /dev/null
@@ -0,0 +1,27 @@
+Description:
+zsprintf() function: print formatted output to a string (without INT_MAX
+limitation)
+
+Files:
+lib/zsprintf.c
+
+Depends-on:
+stdio
+vasnprintf
+errno
+stdint
+
+configure.ac:
+gl_STDIO_MODULE_INDICATOR([zsprintf])
+
+Makefile.am:
+lib_SOURCES += zsprintf.c
+
+Include:
+<stdio.h>
+
+License:
+LGPL
+
+Maintainer:
+all