1 #include "stdio_impl.h"
2 #include <string.h>
3
__fwritex(const unsigned char * restrict s,size_t l,FILE * restrict f)4 size_t __fwritex(const unsigned char *restrict s, size_t l, FILE *restrict f)
5 {
6 size_t i=0;
7
8 if (!f->wend && __towrite(f)) return 0;
9
10 if (l > f->wend - f->wpos) return f->write(f, s, l);
11
12 if (f->lbf >= 0) {
13 /* Match /^(.*\n|)/ */
14 for (i=l; i && s[i-1] != '\n'; i--);
15 if (i) {
16 size_t n = f->write(f, s, i);
17 if (n < i) return n;
18 s += i;
19 l -= i;
20 }
21 }
22
23 memcpy(f->wpos, s, l);
24 f->wpos += l;
25 return l+i;
26 }
27
fwrite(const void * restrict src,size_t size,size_t nmemb,FILE * restrict f)28 size_t fwrite(const void *restrict src, size_t size, size_t nmemb, FILE *restrict f)
29 {
30 size_t k, l = size*nmemb;
31 if (!size) nmemb = 0;
32 FLOCK(f);
33 k = __fwritex(src, l, f);
34 FUNLOCK(f);
35 return k==l ? nmemb : k/size;
36 }
37
38 weak_alias(fwrite, fwrite_unlocked);
39