2024-06-22 Bruno Haible <bruno@clisp.org>
+ vsnprintf: Use vzsnprintf.
+ * lib/stdio.in.h (vsnprintf): Move specification to here.
+ * lib/vsnprintf.c: Don't include <stdlib.h>, <string.h>, vasnprintf.h.
+ Include <stdint.h>.
+ (vsnprintf): Implement based on vzsnprintf.
+ * modules/vsnprintf (Depends-on): Add stdint, vzsnprintf. Remove
+ vasnprintf.
+
vzsnprintf: New module.
* lib/stdio.in.h (vzsnprintf): New declaration, based on
lib/vsnprintf.c.
#endif
#if @GNULIB_VSNPRINTF@
+/* Prints formatted output to string STR. Similar to vsprintf, 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 a negative value. */
# if @REPLACE_VSNPRINTF@
# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
# define vsnprintf rpl_vsnprintf
/* Formatted output to strings.
Copyright (C) 2004, 2006-2024 Free Software Foundation, Inc.
- Written by Simon Josefsson and Yoann Vandoorselaere <yoann@prelude-ids.org>.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
-#include <stdlib.h>
-#include <string.h>
+#include <stdint.h>
-#include "vasnprintf.h"
-
-/* Print formatted output to string STR. Similar to vsprintf, but
- additional length SIZE limit how much is written into STR. Returns
- string length of formatted string (which may be larger than SIZE).
- STR may be NULL, in which case nothing will be written. On error,
- return a negative value. */
int
vsnprintf (char *str, size_t size, const char *format, va_list args)
{
- char *output;
- size_t len;
- size_t lenbuf = size;
-
- output = vasnprintf (str, &lenbuf, format, args);
- len = lenbuf;
-
- 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);
- }
+ ptrdiff_t ret = vzsnprintf (str, size, format, args);
- if (len > INT_MAX)
+#if PTRDIFF_MAX > INT_MAX
+ if (ret > INT_MAX)
{
errno = EOVERFLOW;
return -1;
}
+#endif
- return len;
+ return ret;
}
Depends-on:
stdio
-vasnprintf [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1]
errno [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1]
+stdint [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1]
+vzsnprintf [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1]
configure.ac:
gl_FUNC_VSNPRINTF
LGPLv2+
Maintainer:
-Yoann Vandoorselaere
+all