summaryrefslogtreecommitdiff
path: root/src/stdio/fwrite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdio/fwrite.c')
-rw-r--r--src/stdio/fwrite.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/stdio/fwrite.c b/src/stdio/fwrite.c
new file mode 100644
index 00000000..23974fe1
--- /dev/null
+++ b/src/stdio/fwrite.c
@@ -0,0 +1,51 @@
+#include "stdio_impl.h"
+
+size_t __fwritex(const unsigned char *s, size_t l, FILE *f)
+{
+ size_t i = 0;
+ size_t k = f->wend - f->wpos;
+
+ /* Handle line-buffered mode by breaking into 2 parts */
+ if (f->lbf >= 0) {
+ /* Match /^(.*\n|)/ */
+ for (i=l; i && s[i-1] != '\n'; i--);
+ if (i) {
+ f->lbf = EOF;
+ __fwritex(s, i, f);
+ f->lbf = '\n';
+ __oflow(f);
+ return ferror(f) ? 0 : i + __fwritex(s+i, l-i, f);
+ }
+ }
+
+ /* Buffer initial segment */
+ if (k > l) k = l;
+ memcpy(f->wpos, s, k);
+ f->wpos += k;
+ if (f->wpos < f->wend) return l;
+
+ /* If there's work left to do, flush buffer */
+ __oflow(f);
+ if (ferror(f)) return 0;
+
+ /* If the remainder will not fit in buffer, write it directly */
+ if (l - k >= f->wend - f->wpos)
+ return k + f->write(f, s+k, l-k);
+
+ /* Otherwise, buffer the remainder */
+ memcpy(f->wpos, s+k, l-k);
+ f->wpos += l-k;
+ return l;
+}
+
+size_t fwrite(const void *src, size_t size, size_t nmemb, FILE *f)
+{
+ size_t l = size*nmemb;
+ if (!l) return l;
+ FLOCK(f);
+ l = __fwritex(src, l, f);
+ FUNLOCK(f);
+ return l/size;
+}
+
+weak_alias(fwrite, fwrite_unlocked);