From 70d42c559229eb99ae41914ccca4f02c75d5a0bf Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 22 Jun 2024 12:21:46 +0200 Subject: [PATCH] c-vazsprintf: New module. * lib/c-vasprintf.h: Change license to LGPLv2+. Include . (c_azsprintf, c_vazsprintf): New declarations. * lib/c-azsprintf.c: New file, based on lib/azsprintf.c. * lib/c-vazsprintf.c: New file, based on lib/vazsprintf.c. * modules/c-vazsprintf: New file. --- ChangeLog | 10 ++++++++++ lib/c-azsprintf.c | 34 ++++++++++++++++++++++++++++++++ lib/c-vasprintf.h | 34 +++++++++++++++++++++++++------- lib/c-vazsprintf.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ modules/c-vazsprintf | 26 +++++++++++++++++++++++++ 5 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 lib/c-azsprintf.c create mode 100644 lib/c-vazsprintf.c create mode 100644 modules/c-vazsprintf diff --git a/ChangeLog b/ChangeLog index fcc60208f5..7a2dd7375c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2024-06-22 Bruno Haible + + c-vazsprintf: New module. + * lib/c-vasprintf.h: Change license to LGPLv2+. + Include . + (c_azsprintf, c_vazsprintf): New declarations. + * lib/c-azsprintf.c: New file, based on lib/azsprintf.c. + * lib/c-vazsprintf.c: New file, based on lib/vazsprintf.c. + * modules/c-vazsprintf: New file. + 2024-06-22 Bruno Haible c-snprintf: Use c-vzsnprintf. diff --git a/lib/c-azsprintf.c b/lib/c-azsprintf.c new file mode 100644 index 0000000000..78bc973ef5 --- /dev/null +++ b/lib/c-azsprintf.c @@ -0,0 +1,34 @@ +/* Formatted output to strings in C locale. + Copyright (C) 1999-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 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 "c-vasprintf.h" + +#include + +ptrdiff_t +c_azsprintf (char **resultp, const char *format, ...) +{ + va_list args; + ptrdiff_t result; + + va_start (args, format); + result = c_vazsprintf (resultp, format, args); + va_end (args); + return result; +} diff --git a/lib/c-vasprintf.h b/lib/c-vasprintf.h index ca8fa63e35..7eae757e3a 100644 --- a/lib/c-vasprintf.h +++ b/lib/c-vasprintf.h @@ -1,17 +1,17 @@ /* vasprintf and asprintf, in C locale. Copyright (C) 2002-2004, 2006-2024 Free Software Foundation, Inc. - This program 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 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 program is distributed in the hope that it will be useful, + 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. + GNU Lesser General Public License for more details. - You should have received a copy of the GNU General Public License + You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #ifndef _C_VASPRINTF_H @@ -25,6 +25,9 @@ /* Get va_list. */ #include +/* Get ptrdiff_t. */ +#include + /* Get _GL_ATTRIBUTE_SPEC_PRINTF_STANDARD. */ #include @@ -32,6 +35,23 @@ extern "C" { #endif +/* Prints formatted output to a string dynamically allocated with malloc(). + If the memory allocation succeeds, it stores the address of the string in + *RESULT and returns the number of resulting bytes, excluding the trailing + NUL. Upon memory allocation error, or some other error, it 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. + + Formatting takes place in the C locale, that is, the decimal point + used in floating-point formatting directives is always '.'. */ +ptrdiff_t c_azsprintf (char **resultp, const char *format, ...) + _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, 2, 3)); +ptrdiff_t c_vazsprintf (char **resultp, const char *format, va_list args) + _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, 2, 0)); + /* asprintf() and vasprintf(), but formatting takes place in the C locale, that is, the decimal point used in floating-point formatting directives is always '.'. */ diff --git a/lib/c-vazsprintf.c b/lib/c-vazsprintf.c new file mode 100644 index 0000000000..88ab938489 --- /dev/null +++ b/lib/c-vazsprintf.c @@ -0,0 +1,46 @@ +/* Formatted output to strings in C locale. + Copyright (C) 1999-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 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 "c-vasprintf.h" + +#include +#include +#include + +#include "c-vasnprintf.h" + +ptrdiff_t +c_vazsprintf (char **resultp, const char *format, va_list args) +{ + size_t length; + char *result = c_vasnprintf (NULL, &length, format, args); + if (result == NULL) + return -1; + + if (length > PTRDIFF_MAX) + { + free (result); + errno = ENOMEM; + return -1; + } + + *resultp = result; + /* Return the number of resulting bytes, excluding the trailing NUL. */ + return length; +} diff --git a/modules/c-vazsprintf b/modules/c-vazsprintf new file mode 100644 index 0000000000..5cea385926 --- /dev/null +++ b/modules/c-vazsprintf @@ -0,0 +1,26 @@ +Description: +azsprintf() and vazsprintf() in C locale + +Files: +lib/c-vasprintf.h +lib/c-azsprintf.c +lib/c-vazsprintf.c + +Depends-on: +stdint +stdio +c-vasnprintf + +configure.ac: + +Makefile.am: +lib_SOURCES += c-azsprintf.c c-vazsprintf.c + +Include: +"c-vasprintf.h" + +License: +LGPLv2+ + +Maintainer: +all -- 2.39.5