blob: 02908c4b9ad39176d4b5f6591a0b3d72518d20d5 [file] [log] [blame]
Rich Felker0b44a032011-02-12 00:22:29 -05001#include "stdio_impl.h"
2
3size_t __fwritex(const unsigned char *s, size_t l, FILE *f)
4{
Rich Felkere3cd6c52011-03-28 01:14:44 -04005 size_t i=0;
Rich Felker0b44a032011-02-12 00:22:29 -05006
Rich Felkere3cd6c52011-03-28 01:14:44 -04007 if (!f->wend && __towrite(f)) return 0;
8
9 if (l > f->wend - f->wpos) return f->write(f, s, l);
10
Rich Felker0b44a032011-02-12 00:22:29 -050011 if (f->lbf >= 0) {
12 /* Match /^(.*\n|)/ */
13 for (i=l; i && s[i-1] != '\n'; i--);
14 if (i) {
Rich Felkere3cd6c52011-03-28 01:14:44 -040015 if (f->write(f, s, i) < i)
16 return i;
17 s += i;
18 l -= i;
Rich Felker0b44a032011-02-12 00:22:29 -050019 }
20 }
21
Rich Felkere3cd6c52011-03-28 01:14:44 -040022 memcpy(f->wpos, s, l);
23 f->wpos += l;
24 return l+i;
Rich Felker0b44a032011-02-12 00:22:29 -050025}
26
27size_t fwrite(const void *src, size_t size, size_t nmemb, FILE *f)
28{
Rich Felkere3cd6c52011-03-28 01:14:44 -040029 size_t k, l = size*nmemb;
Rich Felker0b44a032011-02-12 00:22:29 -050030 if (!l) return l;
31 FLOCK(f);
Rich Felkere3cd6c52011-03-28 01:14:44 -040032 k = __fwritex(src, l, f);
Rich Felker0b44a032011-02-12 00:22:29 -050033 FUNLOCK(f);
Rich Felkere3cd6c52011-03-28 01:14:44 -040034 return k==l ? nmemb : l/size;
Rich Felker0b44a032011-02-12 00:22:29 -050035}
36
37weak_alias(fwrite, fwrite_unlocked);