* lib/stdio.in.h (fprintf): Add specification.
* lib/fprintf.c: Don't include <stdlib.h>, vasnprintf.h.
Include intprops.h.
(fprintf): Implement based on vfzprintf.
* modules/fprintf-posix (Depends-on): Add vfzprintf, intprops. Remove
vasnprintf, free-posix.
2024-06-30 Bruno Haible <bruno@clisp.org>
+ fprintf-posix: Use vfzprintf.
+ * lib/stdio.in.h (fprintf): Add specification.
+ * lib/fprintf.c: Don't include <stdlib.h>, vasnprintf.h.
+ Include intprops.h.
+ (fprintf): Implement based on vfzprintf.
+ * modules/fprintf-posix (Depends-on): Add vfzprintf, intprops. Remove
+ vasnprintf, free-posix.
+
fzprintf: New module.
* lib/stdio.in.h (fzprintf): New declaration.
* lib/fzprintf.c: New file, based on lib/vfzprintf.c.
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
-#include <stdlib.h>
#include "fseterr.h"
-#include "vasnprintf.h"
+#include "intprops.h"
-/* Print formatted output to the stream FP.
- Return string length of formatted string. On error, return a negative
- value. */
int
fprintf (FILE *fp, const char *format, ...)
{
- char buf[2000];
- char *output;
- size_t len;
- size_t lenbuf = sizeof (buf);
va_list args;
+ off64_t ret;
va_start (args, format);
- output = vasnprintf (buf, &lenbuf, format, args);
- len = lenbuf;
+ ret = vfzprintf (fp, format, args);
va_end (args);
- if (!output)
+ if (TYPE_MAXIMUM (off64_t) > INT_MAX && ret > INT_MAX)
{
fseterr (fp);
- return -1;
- }
-
- if (fwrite (output, 1, len, fp) < len)
- {
- if (output != buf)
- free (output);
- return -1;
- }
-
- if (output != buf)
- free (output);
-
- if (len > INT_MAX)
- {
errno = EOVERFLOW;
- fseterr (fp);
return -1;
}
- return len;
+ return ret;
}
#endif
#if @GNULIB_FPRINTF_POSIX@ || @GNULIB_FPRINTF@
+/* Prints formatted output to stream FP.
+ Returns the number of bytes written to the stream. Upon failure,
+ returns a negative value with the stream's error indicator set. */
# if (@GNULIB_FPRINTF_POSIX@ && @REPLACE_FPRINTF@) \
|| (@GNULIB_FPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && (@GNULIB_STDIO_H_NONBLOCKING@ || @GNULIB_STDIO_H_SIGPIPE@))
# if !(defined __cplusplus && defined GNULIB_NAMESPACE)
printf-safe
errno
multiarch
-fseterr [test $REPLACE_FPRINTF = 1]
-vasnprintf [test $REPLACE_FPRINTF = 1]
+vfzprintf [test $REPLACE_FPRINTF = 1]
mixin/printf-posix [test $REPLACE_FPRINTF = 1]
-free-posix [test $REPLACE_FPRINTF = 1]
+intprops [test $REPLACE_FPRINTF = 1]
+fseterr [test $REPLACE_FPRINTF = 1]
configure.ac:
gl_FUNC_FPRINTF_POSIX