]> Savannah Git Hosting - gnulib.git/commitdiff
c-vazsprintf: New module.
authorBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 10:21:46 +0000 (12:21 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 10:21:46 +0000 (12:21 +0200)
* lib/c-vasprintf.h: Change license to LGPLv2+.
Include <stddef.h>.
(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
lib/c-azsprintf.c [new file with mode: 0644]
lib/c-vasprintf.h
lib/c-vazsprintf.c [new file with mode: 0644]
modules/c-vazsprintf [new file with mode: 0644]

index fcc60208f597b58f8cd8b3bc09a586d2bbaeb6c3..7a2dd7375cd7b68045989506359447d7620937db 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2024-06-22  Bruno Haible  <bruno@clisp.org>
+
+       c-vazsprintf: New module.
+       * lib/c-vasprintf.h: Change license to LGPLv2+.
+       Include <stddef.h>.
+       (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  <bruno@clisp.org>
 
        c-snprintf: Use c-vzsnprintf.
diff --git a/lib/c-azsprintf.c b/lib/c-azsprintf.c
new file mode 100644 (file)
index 0000000..78bc973
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include "c-vasprintf.h"
+
+#include <stdarg.h>
+
+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;
+}
index ca8fa63e35b9ab8070e982af96b7ed576c36c330..7eae757e3a643edef445784488c4c4322cb0665c 100644 (file)
@@ -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 <https://www.gnu.org/licenses/>.  */
 
 #ifndef _C_VASPRINTF_H
@@ -25,6 +25,9 @@
 /* Get va_list.  */
 #include <stdarg.h>
 
+/* Get ptrdiff_t.  */
+#include <stddef.h>
+
 /* Get _GL_ATTRIBUTE_SPEC_PRINTF_STANDARD.  */
 #include <stdio.h>
 
 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 (file)
index 0000000..88ab938
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include "c-vasprintf.h"
+
+#include <errno.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#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 (file)
index 0000000..5cea385
--- /dev/null
@@ -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