From b216546a9d3e3fd6097d9a8ab3dd1e67a8a68434 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 22 Jun 2024 21:31:46 +0200 Subject: [PATCH] obstack-zprintf: New module. * lib/stdio.in.h (obstack_zprintf, obstack_vzprintf): New declarations. (obstack_printf, obstack_vprintf): Tweak comment. * lib/obstack_printf.c: Parameterize. (RESULT_TYPE, OBSTACK_PRINTF, OBSTACK_VPRINTF): New macros. * lib/obstack_zprintf.c: New file. * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize GNULIB_OBSTACK_ZPRINTF. * modules/stdio (Makefile.am): Substitute GNULIB_OBSTACK_ZPRINTF. * modules/obstack-zprintf: New file. --- ChangeLog | 13 +++++++++++++ lib/obstack_printf.c | 18 ++++++++++++------ lib/obstack_zprintf.c | 20 ++++++++++++++++++++ lib/stdio.in.h | 34 ++++++++++++++++++++++++++++++---- m4/stdio_h.m4 | 3 ++- modules/obstack-zprintf | 26 ++++++++++++++++++++++++++ modules/stdio | 1 + 7 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 lib/obstack_zprintf.c create mode 100644 modules/obstack-zprintf diff --git a/ChangeLog b/ChangeLog index 8d28459959..156dd40138 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2024-06-22 Bruno Haible + + obstack-zprintf: New module. + * lib/stdio.in.h (obstack_zprintf, obstack_vzprintf): New declarations. + (obstack_printf, obstack_vprintf): Tweak comment. + * lib/obstack_printf.c: Parameterize. + (RESULT_TYPE, OBSTACK_PRINTF, OBSTACK_VPRINTF): New macros. + * lib/obstack_zprintf.c: New file. + * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize + GNULIB_OBSTACK_ZPRINTF. + * modules/stdio (Makefile.am): Substitute GNULIB_OBSTACK_ZPRINTF. + * modules/obstack-zprintf: New file. + 2024-06-22 Bruno Haible physmem: Fix typo. diff --git a/lib/obstack_printf.c b/lib/obstack_printf.c index 57979e3509..ed996dd180 100644 --- a/lib/obstack_printf.c +++ b/lib/obstack_printf.c @@ -26,20 +26,26 @@ #include #include +#ifndef RESULT_TYPE +# define RESULT_TYPE int +# define OBSTACK_PRINTF obstack_printf +# define OBSTACK_VPRINTF obstack_vprintf +#endif + /* Grow an obstack with formatted output. Return the number of bytes added to OBS. No trailing nul byte is added, and the object should be closed with obstack_finish before use. Upon memory allocation error, call obstack_alloc_failed_handler. Upon other error, return -1. */ -int -obstack_printf (struct obstack *obs, const char *format, ...) +RESULT_TYPE +OBSTACK_PRINTF (struct obstack *obs, const char *format, ...) { va_list args; - int result; + RESULT_TYPE result; va_start (args, format); - result = obstack_vprintf (obs, format, args); + result = OBSTACK_VPRINTF (obs, format, args); va_end (args); return result; } @@ -50,8 +56,8 @@ obstack_printf (struct obstack *obs, const char *format, ...) Upon memory allocation error, call obstack_alloc_failed_handler. Upon other error, return -1. */ -int -obstack_vprintf (struct obstack *obs, const char *format, va_list args) +RESULT_TYPE +OBSTACK_VPRINTF (struct obstack *obs, const char *format, va_list args) { /* If we are close to the end of the current obstack chunk, use a stack-allocated buffer and copy, to reduce the likelihood of a diff --git a/lib/obstack_zprintf.c b/lib/obstack_zprintf.c new file mode 100644 index 0000000000..b802f4dc26 --- /dev/null +++ b/lib/obstack_zprintf.c @@ -0,0 +1,20 @@ +/* Formatted output to obstacks. + Copyright (C) 2008-2024 Free Software Foundation, Inc. + + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#define RESULT_TYPE ptrdiff_t +#define OBSTACK_PRINTF obstack_zprintf +#define OBSTACK_VPRINTF obstack_vzprintf +#include "obstack_printf.c" diff --git a/lib/stdio.in.h b/lib/stdio.in.h index 4844ea1ebf..cf2d8c999b 100644 --- a/lib/stdio.in.h +++ b/lib/stdio.in.h @@ -1075,13 +1075,39 @@ _GL_CXXALIASWARN (getw); # endif #endif +#if @GNULIB_OBSTACK_ZPRINTF@ +struct obstack; +/* Grows an obstack with formatted output. Returns the number of + bytes added to OBS. No trailing nul byte is added, and the + object should be closed with obstack_finish before use. + Upon memory allocation error, calls obstack_alloc_failed_handler. + Upon other error, 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 through + obstack_alloc_failed_handler. */ +_GL_FUNCDECL_SYS (obstack_zprintf, ptrdiff_t, + (struct obstack *obs, const char *format, ...) + _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 3) + _GL_ARG_NONNULL ((1, 2))); +_GL_CXXALIAS_SYS (obstack_zprintf, ptrdiff_t, + (struct obstack *obs, const char *format, ...)); +_GL_FUNCDECL_SYS (obstack_vzprintf, ptrdiff_t, + (struct obstack *obs, const char *format, va_list args) + _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 0) + _GL_ARG_NONNULL ((1, 2))); +_GL_CXXALIAS_SYS (obstack_vzprintf, ptrdiff_t, + (struct obstack *obs, const char *format, va_list args)); +#endif + #if @GNULIB_OBSTACK_PRINTF@ || @GNULIB_OBSTACK_PRINTF_POSIX@ struct obstack; -/* Grow an obstack with formatted output. Return the number of +/* Grows an obstack with formatted output. Returns the number of bytes added to OBS. No trailing nul byte is added, and the - object should be closed with obstack_finish before use. Upon - memory allocation error, call obstack_alloc_failed_handler. Upon - other error, return -1. */ + object should be closed with obstack_finish before use. + Upon memory allocation error, calls obstack_alloc_failed_handler. + Upon other error, returns -1. */ # if @REPLACE_OBSTACK_PRINTF@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) # define obstack_printf rpl_obstack_printf diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4 index aa06d77027..10e1fbb8aa 100644 --- a/m4/stdio_h.m4 +++ b/m4/stdio_h.m4 @@ -1,5 +1,5 @@ # stdio_h.m4 -# serial 68 +# serial 69 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, @@ -159,6 +159,7 @@ AC_DEFUN([gl_STDIO_H_REQUIRE_DEFAULTS], gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_GETLINE]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_OBSTACK_PRINTF]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_OBSTACK_PRINTF_POSIX]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_OBSTACK_ZPRINTF]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_PCLOSE]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_PERROR]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_POPEN]) diff --git a/modules/obstack-zprintf b/modules/obstack-zprintf new file mode 100644 index 0000000000..d78196f4d5 --- /dev/null +++ b/modules/obstack-zprintf @@ -0,0 +1,26 @@ +Description: +Formatted printing into an obstack (without INT_MAX limitation). + +Files: +lib/obstack_zprintf.c +lib/obstack_printf.c + +Depends-on: +obstack +stdio +vasnprintf + +configure.ac: +gl_STDIO_MODULE_INDICATOR([obstack-zprintf]) + +Makefile.am: +lib_SOURCES += obstack_zprintf.c + +Include: + + +License: +GPL + +Maintainer: +all diff --git a/modules/stdio b/modules/stdio index b0cf4ea207..64fdc5fb60 100644 --- a/modules/stdio +++ b/modules/stdio @@ -94,6 +94,7 @@ stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) -e 's/@''GNULIB_GETLINE''@/$(GNULIB_GETLINE)/g' \ -e 's/@''GNULIB_OBSTACK_PRINTF''@/$(GNULIB_OBSTACK_PRINTF)/g' \ -e 's/@''GNULIB_OBSTACK_PRINTF_POSIX''@/$(GNULIB_OBSTACK_PRINTF_POSIX)/g' \ + -e 's/@''GNULIB_OBSTACK_ZPRINTF''@/$(GNULIB_OBSTACK_ZPRINTF)/g' \ -e 's/@''GNULIB_PCLOSE''@/$(GNULIB_PCLOSE)/g' \ -e 's/@''GNULIB_PERROR''@/$(GNULIB_PERROR)/g' \ -e 's/@''GNULIB_POPEN''@/$(GNULIB_POPEN)/g' \ -- 2.39.5