From: Bruno Haible Date: Sat, 22 Jun 2024 10:15:32 +0000 (+0200) Subject: zsnprintf: New module. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=c85fa7f8a128437cca30f396df8392af1c1b7632;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index 28833003fb..458561b9fe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2024-06-22 Bruno Haible + + 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 vsnprintf: Use vzsnprintf. diff --git a/lib/stdio.in.h b/lib/stdio.in.h index 8d6cd21fd5..4a942a92ed 100644 --- a/lib/stdio.in.h +++ b/lib/stdio.in.h @@ -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 index 0000000000..894631d16a --- /dev/null +++ b/lib/zsnprintf.c @@ -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 . */ + +#include + +/* Specification. */ +#include + +#include +#include +#include +#include +#include + +#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; +} diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4 index 9c631f172f..28d1c96c74 100644 --- a/m4/stdio_h.m4 +++ b/m4/stdio_h.m4 @@ -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]) diff --git a/modules/stdio b/modules/stdio index 63ea9a908e..9e676681dd 100644 --- a/modules/stdio +++ b/modules/stdio @@ -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 index 0000000000..40fd329e0e --- /dev/null +++ b/modules/zsnprintf @@ -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: + + +License: +LGPLv2+ + +Maintainer: +all