Browse Source

LibC: Make fwrite() buffered.

This is a really naive implementation that makes fwrite() call fputc()
internally, but it still performs a lot better due to avoiding the write()
syscall every time.
Andreas Kling 6 years ago
parent
commit
d3fb0a56ed
1 changed files with 7 additions and 7 deletions
  1. 7 7
      LibC/stdio.cpp

+ 7 - 7
LibC/stdio.cpp

@@ -254,13 +254,13 @@ size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
 size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream)
 {
     assert(stream);
-    int rc = fflush(stream);
-    if (rc == EOF)
-        return 0;
-    ssize_t nwritten = write(stream->fd, ptr, nmemb * size);
-    if (nwritten < 0) {
-        stream->error = errno;
-        return 0;
+    auto* bytes = (const byte*)ptr;
+    ssize_t nwritten = 0;
+    for (size_t i = 0; i < (size * nmemb); ++i) {
+        int rc = fputc(bytes[i], stream);
+        if (rc == EOF)
+            break;
+        ++nwritten;
     }
     return nwritten / size;
 }