summaryrefslogtreecommitdiff
path: root/src/stdio/fwrite.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
committerRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
commit0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 (patch)
tree6eaef0d8a720fa3da580de87b647fff796fe80b3 /src/stdio/fwrite.c
downloadmusl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.gz
musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.bz2
musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.xz
musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.zip
initial check-in, version 0.5.0v0.5.0
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);