]> Savannah Git Hosting - gnulib.git/commitdiff
fprintf-posix: Use vfzprintf.
authorBruno Haible <bruno@clisp.org>
Sun, 30 Jun 2024 17:47:26 +0000 (19:47 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 30 Jun 2024 17:47:26 +0000 (19:47 +0200)
* 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.

ChangeLog
lib/fprintf.c
lib/stdio.in.h
modules/fprintf-posix

index bcbc7bc7c59040f538791862b8c499b4ceedb008..8492ed83146985481d6bdabd031bee37ee7f4789 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 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.
index da4609d8d6a0100ab230ae2fffee8a8a94bef27b..d55f0bafcc519eaefd27724564b030149e0153d5 100644 (file)
 #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;
 }
index 4ce51a743c92fac31c9a10edd3208581eb973fa0..1c48954d81eb0c2e5af5f4358adc90c53f0a84ce 100644 (file)
@@ -583,6 +583,9 @@ _GL_CXXALIAS_SYS (fzprintf, off64_t,
 #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)
index 6cda1ccef76da2d70411ae879241243805461448..3ccb9be175e19974149578f7f3df0a8cbf963a4c 100644 (file)
@@ -19,10 +19,10 @@ nocrash
 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